Skip to content

Instantly share code, notes, and snippets.

@stuartpb
Last active May 5, 2020 21:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartpb/e4b415eb7035a4f38fe0 to your computer and use it in GitHub Desktop.
Save stuartpb/e4b415eb7035a4f38fe0 to your computer and use it in GitHub Desktop.
JavaScript to simplify uniform SVG paths
// run on http://mourner.github.io/simplify-js/
var tolerance = 5;
function simplifyMLZPath(path) {
var polys = JSON.parse('[['+path
.replace(/[LM](\d+(?:\.\d+)?),(\d+(?:\.\d+)?)/g,'{"x":$1,"y":$2},')
.replace(/,Z/g,'],[')
.slice(0,-2)+']');
var segments = [];
for (var i = 0; i < polys.length; i++) {
var points = simplify(polys[i], tolerance, true);
if (points.length > 2) {
for (var j = 0; j < points.length; j++) {
points[j] = points[j].x + ',' + points[j].y;
}
segments[segments.length] = 'M'+points.join('L')+'Z';
}
}
return segments.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment