Fast SVG path parser for Javascript.
npm install svg-path-segments[![NPM version][npm-version-image]][npm-link]
[![Tests][tests-image]][tests-link]
[![Coverage status][coverage-image]][coverage-link]
A fast SVG path parser implementation for Javascript. Does not perform checks
on input data, but allows to rebuild original path segments from the result.
To avoid errors, check first that your paths are made up of ASCII characters
supported by the [SVG 1.1 specification][svg11-spec-link].
[![License][license-image]][license-link]
``bash`
npm install svg-path-segments
`js
const parsePath = require("svg-path-segments");
const segments = parsePath("M5 6 7 8l3 4z");
console.log(JSON.stringify(segments, null, 2));
`
`json`
[
{
"start": 0,
"end": 4,
"params": ["M", 5, 6],
"chain": {
"start": 0,
"end": 8
},
},
{
"start": 5,
"end": 8,
"params": ["M", 7, 8],
"chain": {
"start": 0,
"end": 8
},
},
{
"start": 8,
"end": 12,
"params": ["l", 3, 4],
},
{
"start": 12,
"end": 13,
"params": ["z"],
}
]
`bash`
svg-path-segments --pretty "M5 6 7 8l3 4z"
# svgPathParse(d: _string_)
⇒ _Segment[]_
Returns the segments of the SVG path. The result is an array of objects, one
per segment, which contain next properties:
- start (_number_): Index of the first character of the segment.
- end (_number_): Index of the first character after the segment. Note
that you can use d.substring(result.start, result.end) to get the raw stringparams
representation of the segment.
- (_number[]_): Parameters of the segment. The first parameter alwayschain
is the command that draws the segment.
- (_object?_): If present, indicates that the segment is part of astart
chained set of segments.
- (_number_): Index of the first character of the chained set ofend
segments to which the segment belongs.
- (_number_): Index of the first character after the chained set ofd.substring(result.chain.start, result.chain.end)
segments to which the segment belongs. Note that you can use
to get the raw string
representation of the chained set of segments to which the segment belongs.
| | svg-path-segments | svgpath |
|---|---|---|
| Require | require("svg-path-segments") | require("svgpath/lib/path_parse") |R
| Benchmark\* | ~46ms | ~76ms |
| Absolutes | YES | NO |
| Segments indexing | YES | NO |
| Chained commands | YES | NO |
| Catmull-Rom curve commands (/r) | NO | YES |B
| Bearing commands (/b) | NO | NO |m
| First converted to M | NO | YES |M
| Chained /m converted to L/l | NO | YES |m
| Check path should start with /M | NO | YES |0
| Check bad command characters | NO | YES |
| Check missing parameters | NO | YES |
| Check leading zeros in numbers | NO | YES |
| Check arc flag or 1 | NO | YES |
| Check invalid float exponents | NO | YES |
| Unicode support | NO | PARTIAL |
> \* Benchmarks are orientative, if you want to perform your own, see
[scripts/\*-benchmark.js][scripts-link].
`js
const svgpath = require("svgpath");
const parsePath = require("svg-path-segments");
const segments = parsePath(iconPath);
const SVGPath = svgpath("");
SVGPath.segments = segments.map(segment => segment.params);
``
[npm-link]: https://www.npmjs.com/package/svg-path-segments
[npm-version-image]: https://img.shields.io/npm/v/svg-path-segments?logo=NPM
[tests-image]: https://img.shields.io/github/actions/workflow/status/mondeja/svg-path-segments/ci.yml?branch=master&logo=github&label=tests
[tests-link]: https://github.com/mondeja/svg-path-segments/actions?query=workflow%3ACI
[coverage-image]: https://img.shields.io/coveralls/github/mondeja/svg-path-segments?logo=coverallss&color=brightgreen
[coverage-link]: https://coveralls.io/github/mondeja/svg-path-segments?branch=master
[license-image]: https://img.shields.io/npm/l/svg-path-segments?color=blue
[license-link]: https://github.com/mondeja/svg-path-segments/blob/master/LICENSE
[scripts-link]: https://github.com/mondeja/svg-path-segments/tree/master/scripts
[svg11-spec-link]: https://www.w3.org/TR/SVG11/paths.html