Gathering detailed insights and metrics for svg-parse-path-normalized
Gathering detailed insights and metrics for svg-parse-path-normalized
Parse path data from string including fine-grained normalizing options
npm install svg-parse-path-normalized
Typescript
Module System
Node Version
NPM Version
75.7
Supply Chain
99
Quality
81.1
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
9,555
Last Day
24
Last Week
173
Last Month
1,105
Last Year
9,343
1 Stars
44 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.10
Package Id
svg-parse-path-normalized@1.0.10
Unpacked Size
161.07 kB
Size
36.32 kB
File Count
11
NPM Version
10.2.3
Node Version
20.10.0
Publised On
30 Nov 2024
Cumulative downloads
Total Downloads
Last day
0%
24
Compared to previous day
Last week
-45.1%
173
Compared to previous week
Last month
74%
1,105
Compared to previous month
Last year
4,307.1%
9,343
Compared to previous year
No dependencies detected.
Parses path data from string including fine-grained normalisation and conversion options.
This library aims to provide a robust and versatile yet quite compact (~6KB/3KB minified; gzipped) parser – respecting all minified/shorthand notations as a basis for all kinds of custom path data manipulations. Compatible with the w3C SVGPathData interface draft format recommendations.
While there is no shortage of excellent parsers – unfortunately, the same applies to rather incomplete ones often deployed in libraries due to their appealing lightweight codebase.
A
arcto commands quite often lightweight parsers crash since they can't unravel concatenated largeArc
, sweep
and final on path x values.A
arcs to cubics – your choice – by default the parser will return the least destructive normalization.Usually parsing alone is not enough to get computable path data values – due to relative or shorthand commands or a
arcto commands that may rather complicate further manipulations such as length or area calculations – especially when dealing with elliptical and/or rotated arcs.
Normalization (admittedly a slightly ambigious term) via parsePathDataNormalized(d)
applies by default these conversions:
m 0 0 .5.5.5.5
to M 0 0 l 0.5 0.5 l 0.5 0.5
h
, v
, s
, t
to L
, C
, T
d
attribute – including minification optionsProvided by pathDataConvert.js
: Useful to convert your manipulated/processed path data to all kind of command types/structures
(E.g to get a more compact or special formats like lineto-to-bezier conversions for morphing animations by converting)
getPathData()
or other parsers compliant with the w3C SVGPathData interface draft format recommendations.Update: You can now use a more convenient function name for the parsing - the more verbose/clunky function still works. Since this parser aims to parse path data into a processable data array, "parsePathDataNormalized" is semantically more accurate – I hope you understand my naming delemma =)
/* shorthand notation */
let pathData = parseD(d, options)
/* original notation */
let pathData = parsePathDataNormalized(d, options)
Feel free to post an issue or write a post in the discussion if the recent update messed up anything.
1<script src="https://www.unpkg.com/svg-parse-path-normalized@latest/js/pathDataParseNormalized.js"></script>
Optional: Load minified script via jsDelivr (~6KB/3KB minified; gzipped)
<!--basic parser --->
<script src="https://cdn.jsdelivr.net/npm/svg-parse-path-normalized@latest/js/pathDataParseNormalized.min.js"></script>
<script>
//parse
const d ="m 0 0 .5.5.5.5a 5 10 45 1040 20" ;
/* shorthand notation */
let pathData = parseD(d)
/* verbose notation */
let pathData = parsePathDataNormalized(d)
//stringify to pathdata d string
let minify = false;
/* chainable notation */
let dNew = pathData.toD(decimals, minify);
/* classic function syntax */
let dNew = pathDataToD(pathData, decimals, minify);
console.log(pathData);
console.log(dNew);
</script>
npm install svg-parse-path-normalized
1 2const parsepathData = require('svg-parse-path-normalized'); 3const {parsePathDataNormalized, pathDataToD} = parsepathData; 4 5//parse 6const d ="m 0 0 .5.5.5.5a 5 10 45 1040 20" ; 7let pathData = parsePathDataNormalized(d) 8 9//stringify to pathdata d string 10let minify = false; 11let dNew = pathDataToD(pathData, 1, minify); 12 13console.log(pathData); 14console.log(dNew); 15
This library uses the pathdata format as suggested in the w3C SVGPathData interface draft.
The returned path data parsed from a stringified pathdata d
attribute string is an array representing each command as an object like so:
1const d ="m 0 0 .5.5.5.5a 5 1045 1040 20" 2parsePathDataNormalized(d)
1[ 2 {"type":"M","values":[0,0]}, 3 {"type":"L","values":[0.5, 0.5]}, 4 {"type":"L","values":[1, 1]}, 5 {"type":"A","values":[5, 10, 45, 1, 0, 41, 21]} 6]
The above example illustrates a problem with overly "lightweight" path parsers:
We need an extra check to "unravel" the A
arcto's largeArc
and sweep
flags, which can be concatenated with the subsequent on-path x coordinate value. (See basic example)
parsePathDataNormalized(d, options)
accepts these parameters
let options= {
normalize: null, //shorthand for aggressive normalisation
toAbsolute: true, //necessary for most calculations
unshort: true, //dito
arcToCubic: false, //sometimes necessary
quadraticToCubic: false, //frankly, not necessary most of the time
lineToCubic: false, //handy for morphing animations
debug: false, //handy to detect malformed pathdata retrieved from user inputs
decimals: -1 //-1=no rounding
}
parameter | default | effect |
---|---|---|
toAbsolute | true | convert all to absolute |
unshort | true | convert all shorthands to longhands |
arcToCubic | false | convert arcs A commands to cubic béziers |
quadraticToCubic | false | convert quadratic to cubic béziers |
lineToCubic | false | convert all L linetos to cubic béziers (handy for morphing animations) |
decimals | -1 | round values to floating point decimals. -1=no rounding |
debug | false | reports malformed path data structures via console.log |
normalize | null | shorthand to also convert arcs and quadratic béziers to cubic – similar to the W3C draft's suggested getPathData({normalize:true}) parameter |
Set normalize to false to get the original (not normalized) pathdata – including relative or shorthand commands.
parsePathDataNormalized(d, {normalize:false})
debug:true
can be handy if you need to find errors in malformed pathdata – maybe caused by manual path splittingd
attribute stringOptions:
You can stringify the path data to a d
attribute (or CSS property) by a chained prototype method or the basic function like so:
let d = pathData.toD(decimals, minify)
which is just a wrapper for the actual stringifying function.
let d = pathDataToD(pathData, decimals, minify)
and eventually apply it like so:
path.setAttribute('d', d);
Load pathDataConvert.js
to get more conversion methods. This script is intended to provide various conversions to optimize the path data after processing e.g for a minified path output.
parameter | default | effect |
---|---|---|
toRelative | false | convert all to relative |
toAbsolute | true | convert all to absolute |
toShorthands | false | convert all to to shorthands – if applicable |
toLonghands | true | convert all shorthands to longhands |
arcToCubic | false | convert arcs A commands to cubic béziers |
lineToCubic | false | convert all L linetos to cubic béziers (handy for morphing animations) |
quadraticToCubic | false | convert quadratic to cubic béziers |
cubicToQuadratic | false | convert all cubic to quadratic |
cubicToQuadraticPrecision | 0.1 | cubic to quadratic accuracy |
decimals | -1 | round values to floating point decimals. -1=no rounding |
normalize | null , true, false | shorthand to also convert arcs and quadratic béziers to cubic – similar to the W3C draft's suggested getPathData({normalize:true}) parameter |
optimize | false | shorthand to convert to shorthands, relative and round to 3 decimals for a more compact output |
<script src="https://www.unpkg.com/svg-parse-path-normalized@latest/js/pathDataConvert.js"></script>
Load minified via jsDelivr (13KB/6KB minified)
<!-- optional conversions -->
<script src="https://cdn.jsdelivr.net/npm/svg-parse-path-normalized@latest/js/pathDataConvert.min.js"></script>
let options = {arcToCubic:true, toRelative:true, decimals:0}
let pathDataCon = pathData.convert(options)
Conversion can be applied via
convert(options)
to apply all conversions at oncepathData.toAbsolute()
, pathData.toRelative()
, pathData.toLonghands()
, pathData.toShorthands()
, pathData.round()
, pathData.toQuadratic()
, pathData.toVerbose()
pathDataToAbsolute(pathData)
, pathDataToRelative(pathData)
, pathDataToShorthands(pathData)
, pathDataToShorthands(pathData)
, pathDataToQuadratic(pathData)
, roundPathData(pathData)
getPathData()
Currently, the W3C draft for the SVGPathData interface is not supported by any major browser. Fortunately Jarek Foksa wrote a this great polyfill library and also contributed to the potential spec outcome – most importantly that it should include geometry elements like circle
, rect
, polygon
, line
to retrieve path data.
This polyfill is a "battle-proof" parser! Since the W3C draft doesn't include fine-grained control over the normalisation/conversion process you can use the pathDataConvert.js
script as an addon/plugin alongside with the aforementioned polyfill script. (See Demo/getPathDataAddon.html)
You may already have a set of parsed/abstracted path data retrieved from other libraries or APIs or need a more verbose notation.
In this case you may use these conversion methods.
A lot of libraries – such as snap.svg use a nested array structure for each command like so
[
["M", 0, 0] ,
["L", 0.5, 0.5],
["L", 1, 1],
["A", 5, 10, 45, 1, 0, 41, 21]
]
In case you need to convert these you can use the helper methods (included in pathDataConvert.js) to convert format in both directions
convertArrayPathData(pathDataArray)
revertPathDataToArray(pathData)
Besides you can use pathDataToVerbose(pathData)
to get a more detailed data array including original and absolute point coordinates as well as parametrized arc data rx
and ry
, startAngle
, endAngle
, deltaAngle
(in radians)
let data = [
{
type: "M",
values: [0, 0],
valuesAbsolute: [0, 0],
pFinal: { x: 0, y: 0 },
isRelative: false
},
{
type: "l",
values: [0.5, 0.5],
valuesAbsolute: [0.5, 0.5],
pFinal: { x: 0.5, y: 0.5 },
isRelative: true,
pPrev: { x: 0, y: 0 }
},
{
type: "l",
values: [0.5, 0.5],
valuesAbsolute: [1, 1],
pFinal: { x: 1, y: 1 },
isRelative: true,
pPrev: { x: 0.5, y: 0.5 }
},
{
type: "a",
values: [5, 10, 45, 1, 0, 40, 20],
valuesAbsolute: [5, 10, 45, 1, 0, 41, 21],
pFinal: { x: 41, y: 21 },
isRelative: true,
pPrev: { x: 1, y: 1 },
rx: 21.505813167606572,
ry: 43.011626335213144,
xAxisRotation: 45,
largeArcFlag: 1,
sweepFlag: 0,
startAngle: 2.976443999504017,
endAngle: 6.118036608390327,
deltaAngle: -3.1415926982932767
}
];
pathDataConvert.js
as a addon/plugin for path.getPathData()
(demos/getPathDataAddon.html)This library does only support SVG path data that's actually supported by browsers.
In other words: these "newer" commands are not included:
... and this contextual closepath thing ...
Seriously, there's no point in implementing these features until they're implemented (see the sad story of SVG multi-line text... we'll probably never get it).
Especially the Catmull-ROM syntax extension is spooking around for more than 10 years without any results. Frankly, these features may never be included as they would bomb quite a few renderers and there are visualisation libraries to draw paths in a abstracted way.
All of these helpers are based on the path parser described above (or by-products - although they include stripped down versions for the sake of providing a standalone lib).
No vulnerabilities found.
No security vulnerabilities found.