Skip to content

Instantly share code, notes, and snippets.

@cppio
Last active February 19, 2021 06:04
Show Gist options
  • Save cppio/9cee96641b49796c310024da70140e2d to your computer and use it in GitHub Desktop.
Save cppio/9cee96641b49796c310024da70140e2d to your computer and use it in GitHub Desktop.
Extract previews from vectormagic.com as SVG files
VecResult.prototype.toSVG = function() {
let paths = [];
this.pieces.forEach(piece => {
let index = 0;
piece.shapes.forEach(shape => {
let commands = [];
shape.lenghts.forEach(length => {
commands.push(`M ${piece.floats[index++]} ${piece.floats[index++]}`);
for (let i = 0; i < (length - 2) / 6; i++) {
commands.push(`C ${piece.floats[index++]} ${piece.floats[index++]} ${piece.floats[index++]} ${piece.floats[index++]} ${piece.floats[index++]} ${piece.floats[index++]}`);
}
commands.push("Z");
});
paths.push(` <path d="${commands.join(" ")}" fill="${shape.color}"/>`);
});
});
return `<svg width="${this.imageWidth}" height="${this.imageHeight}" xmlns="http://www.w3.org/2000/svg">
${paths.join("\n")}
</svg>
`;
};
@nalmeida
Copy link

How to use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment