Skip to content

Instantly share code, notes, and snippets.

@VivaRado
Last active November 26, 2022 10:01
Show Gist options
  • Save VivaRado/9606e9c0be095241a903dc716c7b596c to your computer and use it in GitHub Desktop.
Save VivaRado/9606e9c0be095241a903dc716c7b596c to your computer and use it in GitHub Desktop.
To get d path to commands (d-path-parser, opentype.js)
//**Trash code included.**
//Include: [d-path-parser](https://github.com/MaxArt2501/d-path-parser)
//Include: [opentype.js](https://github.com/opentypejs/opentype.js)
// path S from AEOLUM
path_d = "M298.0 -352.0L161.0 -352.0C84.0 -352.0 24.0 -414.0 24.0 -491.0C24.0 -564.0 84.0 -625.0 161.0 -625.0L355.0 -625.0L390.0 -589.0L395.0 -589.0L410.0 -601.0L364.0 -650.0L161.0 -650.0C70.0 -650.0 0.0 -578.0 0.0 -491.0C0.0 -397.0 71.0 -327.0 162.0 -327.0L298.0 -327.0C382.0 -327.0 452.0 -262.0 452.0 -178.0C452.0 -94.0 382.0 -25.0 298.0 -25.0L52.0 -25.0L21.0 -62.0L16.0 -62.0L0.0 -49.0L42.0 0.0L298.0 0.0C396.0 0.0 476.0 -80.0 476.0 -178.0C476.0 -276.0 396.0 -352.0 298.0 -352.0";
var commands = parse(path_d);
const aPath = new opentype.Path();
function addGlyphPath(ctx, commands, x, y, s) {
commands.forEach(function(cmd) {
switch(cmd.code) {
case 'M': ctx.moveTo(x + cmd.end.x * s, y + cmd.end.y * s); break;
case 'Q': ctx.quadraticCurveTo(x + cmd.x1 * s, y + cmd.y1 * s, x + cmd.x * s, y + cmd.y * s); break;
case 'C': ctx.bezierCurveTo(x + cmd.cp1.x * s, y + cmd.cp1.y * s, x + cmd.cp2.x * s, y + cmd.cp2.y * s, x + cmd.end.x * s, y + cmd.end.y * s); break;
case 'L': ctx.lineTo(x + cmd.end.x * s, y + cmd.end.y * s); break;
case 'Z':
{
ctx.closePath();
fill ? ctx.fill() : ctx.stroke();
ctx.beginPath();
break;
}
}
});
}
addGlyphPath(aPath, commands, 0, 0, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment