convertPathData
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.
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
- Basic
- Parameters
module.exports = {
plugins: [
"convertPathData"
]
}
module.exports = {
plugins: [
{
name: "convertPathData",
params: {
applyTransforms: true,
applyTransformsStroked: true,
straightCurves: true,
convertToQ: true,
lineShorthands: true,
convertToZ: true,
curveSmoothShorthands: true,
floatPrecision: 3,
transformPrecision: 5,
smartArcRounding: true,
removeUseless: true,
collapseRepeated: true,
utilizeAbsolute: true,
negativeExtraSpace: true,
forceAbsolutePath: false
}
}
]
}
Parameters
applyTransformsIf to apply transforms.
applyTransformsStrokedIf to apply transforms to paths with a stroke.
makeArcsIf to convert from curves to arcs when possible. This is an object with two properties,
thresholdandtolerance.straightCurvesIf to convert curve commands that are effectively straight lines to line commands.
convertToQIf to convert cubic Bézier curves to quadratic Bézier curves when they effectively are.
lineShorthandsIf to convert regular lines to an explicit horizontal or vertical line where possible.
convertToZIf to convert lines that go to the start to a
zcommand.curveSmoothShorthandsIf to convert curves to smooth curves where possible.
floatPrecisionNumber of decimal places to round to, using conventional rounding rules.
transformPrecisionNumber of decimal places to round to, using conventional rounding rules.
smartArcRoundingRound the radius of circular arcs when the effective change is under the error. The effective change is determined using the sagitta of the arc.
removeUselessRemove redundant path commands that don't draw anything.
collapseRepeatedCollapse repeated commands when they can be merged into one.
utilizeAbsoluteIf to convert between absolute or relative coordinates, whichever is shortest.
forceAbsolutePathIf to always convert to absolute coordinates, even if it adds more bytes.
Demo
const svg = ` <?xml version="1.0" encoding="UTF-8"?> <svg xmlns="http://www.w3.org/2000/svg" viewBox=" 0 0 150 100 " width="150"> <!-- Created with love! --> <defs> <ellipse cx="50" cy="50.0" rx="50.00" ry="auto" fill="black" id="circle"/> </defs> <g> <use href="#circle" transform="skewX(16)"/> <rect id="useless" width="0" height="0" fill="#ff0000"/> </g> </svg> `; const svgoConfig = { js2svg: { indent: 2, pretty: true }, plugins: [ "convertPathData" ] } render(<SvgoPreview svg={svg} svgoConfig={svgoConfig}/>);