Last active
May 5, 2020 21:08
-
-
Save stuartpb/e4b415eb7035a4f38fe0 to your computer and use it in GitHub Desktop.
JavaScript to simplify uniform SVG paths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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