Skip to content

Commit

Permalink
New example 7 code and undo of sync load of SVGs because of timing is…
Browse files Browse the repository at this point in the history
…sues
  • Loading branch information
ByronStar committed Feb 11, 2024
1 parent 943bb90 commit d826ac4
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 163 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5502
}
43 changes: 22 additions & 21 deletions classes/PolygonFromSVG.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ let block = new PolygonFromSVG(world, attributes, options)

class PolygonFromSVG extends Block {
/**
* @param {Matter.World} world
* @param {object} attributes
* @param {Matter.IChamferableBodyDefinition} options
*/
* @param {Matter.World} world
* @param {object} attributes
* @param {Matter.IChamferableBodyDefinition} options
*/
constructor(world, attributes, options) {
super(world, attributes, options);
this.attributes.sample = this.attributes.sample || 10;
Expand All @@ -78,8 +78,20 @@ class PolygonFromSVG extends Block {
}
} else {
// use a path in separate SVG file
let that = this;
if (this.attributes.async) {
if (this.attributes.sync) {
const request = new XMLHttpRequest();
request.open("GET", this.attributes.fromFile, false); // `false` makes the request synchronous
request.send(null);
const response = request.responseText;
const parser = new DOMParser();
const svgDoc = parser.parseFromString(response, "image/svg+xml");
const path = svgDoc.querySelector("path");
let vertices = Matter.Svg.pathToVertices(path, this.attributes.sample);
this.addBodyVertices(vertices)
Matter.World.add(this.world, [this.body]);
console.log('DONE')
} else {
let that = this;
httpGet(this.attributes.fromFile, "text", false, function (response) {
const parser = new DOMParser();
const svgDoc = parser.parseFromString(response, "image/svg+xml");
Expand All @@ -88,17 +100,6 @@ class PolygonFromSVG extends Block {
that.addBodyVertices(vertices)
Matter.World.add(that.world, [that.body]);
});
} else {
const request = new XMLHttpRequest();
request.open("GET", this.attributes.fromFile, false); // `false` makes the request synchronous
request.send(null);
const response = request.responseText;
const parser = new DOMParser();
const svgDoc = parser.parseFromString(response, "image/svg+xml");
const path = svgDoc.querySelector("path");
let vertices = Matter.Svg.pathToVertices(path, that.attributes.sample);
that.addBodyVertices(vertices)
Matter.World.add(that.world, [that.body]);
}
}
}
Expand Down Expand Up @@ -127,10 +128,10 @@ class PolygonFromSVG extends Block {
}

/**
* @param {Matter.Vector[]} vertices
* @returns {Matter.Vector}
* @memberof PolygonFromSVG
*/
* @param {Matter.Vector[]} vertices
* @returns {Matter.Vector}
* @memberof PolygonFromSVG
*/
getCenter(vertices) {
let min = { x: 999999, y: 999999 };
let max = { x: -999999, y: -999999 };
Expand Down
3 changes: 3 additions & 0 deletions xx-benno-step7/gondel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d826ac4

Please sign in to comment.