Convert Path Data Default
Optimize path commands found in <path>
, <glyph>
, and <missing-glyph>
elements. Path commands are the syntax used in the d
attribute, each character represents an instruction to draw paths.
INFO
You can get more context on path commands on MDN Web Docs.
This plugin uses multiple techniques to either reduce the number of instructions or reduce the attribute length:
- Convert between relative or absolute coordinates, whichever is shortest.
- Convert between commands. For example, a bézier curve that behaves like a straight line might as well use a line instruction.
- Remove redundant commands. For example, a command that moves to the current position can be removed.
- Trim redundant delimiters and leading zeros.
- Round numeric values using conventional rounding rules.
You can read more about the plugins capabilities by going through the individual parameters.
Usage
js
module.exports = {
plugins: ["convertPathData"],
};
module.exports = {
plugins: ["convertPathData"],
};
Parameters
applyTransforms
If to apply transforms.applyTransformsStroked
If to apply transforms to paths with a stroke.makeArcs
If to convert from curves to arcs when possible. This is an object with two properties, threshold
and tolerance
.straightCurves
If to convert curve commands that are effectively straight lines to line commands.lineShorthands
If to convert regular lines to an explicit horizontal or vertical line where possible.convertToZ
If to convert lines that go to the start to a z
command.curveSmoothShorthands
If to convert curves to smooth curves where possible.floatPrecision
Number of decimal places to round to, using conventional rounding rules.transformPrecision
Number of decimal places to round to, using conventional rounding rules.removeUseless
Remove redundant path commands that don't draw anything.collapseRepeated
Collapse repeated commands when they can be merged into one.utilizeAbsolute
If to convert between absolute or relative coordinates, whichever is shortest.forceAbsolutePath
If to always convert to absolute coordinates, even if it adds more bytes.Usage example
js
module.exports = {
plugins: [
{
name: "convertPathData",
params: {
applyTransforms: true,
applyTransformsStroked: true,
straightCurves: true,
lineShorthands: true,
convertToZ: true,
curveSmoothShorthands: true,
floatPrecision: 3,
transformPrecision: 5,
removeUseless: true,
collapseRepeated: true,
utilizeAbsolute: true,
negativeExtraSpace: true,
forceAbsolutePath: false
}
}
]
}
module.exports = {
plugins: [
{
name: "convertPathData",
params: {
applyTransforms: true,
applyTransformsStroked: true,
straightCurves: true,
lineShorthands: true,
convertToZ: true,
curveSmoothShorthands: true,
floatPrecision: 3,
transformPrecision: 5,
removeUseless: true,
collapseRepeated: true,
utilizeAbsolute: true,
negativeExtraSpace: true,
forceAbsolutePath: false
}
}
]
}
Demo
Implementation
https://github.com/svg/svgo/blob/main/plugins/convertPathData.js