Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.