Skip to content

Instantly share code, notes, and snippets.

@celsowhite
Created June 22, 2021 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save celsowhite/6fbaf7dd7571c36dffcbad78c0dfda44 to your computer and use it in GitHub Desktop.
Save celsowhite/6fbaf7dd7571c36dffcbad78c0dfda44 to your computer and use it in GitHub Desktop.
function createSticker(sticker, xPosition = 100, yPosition = 100) {
fetch(sticker.svg)
.then(res => res.text())
.then(svg => {
// Body Creation
// Save a blank array of vertex sets.
const vertexSets = [];
// Use the dom parser to parse our svg.
const parser = new DOMParser();
const htmlDocument = parser.parseFromString(svg, 'text/html');
// Query the svg paths and loop through each to get the vertices.
const paths = htmlDocument.documentElement.querySelectorAll('path');
[...paths].forEach(path => {
// Convert path into array of vector points.
const points = Svg.pathToVertices(path, 10);
// Push the points into the vertex set.
vertexSets.push(Vertices.scale(points, 0.8, 0.8));
});
// Add the svg to the world.
const stickerBody = Bodies.fromVertices(
xPosition,
yPosition,
vertexSets,
{
restitution: 0.8,
render: {
fillStyle: '#FFFFFF',
strokeStyle: '#FF0000',
lineWidth: 2,
},
},
true,
);
World.add(engine.world, stickerBody);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment