Add plugin simplifyPaths to reduce the number of points in paths and polylines while maintaining approximately the same overall shape #1698
+312
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #411
This pull request adds a new plugin that can be used to approximate paths and polylines to further optimize SVGs. This takes advantage of Paper.js, as suggested by @jeromew.
I not find a construct in Paper.js to represent
<polyline>
s, but I found a way to convert these into paths. Then, the samepath.simplify()
method can be used.The available parameters are as follows:
This plugin can have big savings even if
path.simplify()
is not usesimplifyThreshold = null
. This is because<path>
is often more compact than<polyline>
and because Paper.js prints thed
attribute very compactly.With this plugin, I was able to get one of my personal SVGs from 188KiB down to 40KiB. I also added some tests in the
test/plugins
folder.I did notice that this plugin must come before
mergePaths
. Even the following code with totally mess up an SVG if it comes aftermergePaths
. All this code is doing is parsing a<path>
'sd
attribute with Paper.js, and printing it in Paper.js's format. It seems like that format is not compatible withmergePaths
. To be honest, I'm not entirely sure what's going on here.This plugin also depends on
paper
andpaper-jsdom
. I'm not sure how to handle that either.