v160 · dom · svg
SVG Path Data API
Since Chrome 48 removed SVGPathSegList in 2016, the only way to read or edit one segment of an SVG <path> was to parse the d string yourself or ship a polyfill. Chrome 160 implements the SVG Working Group’s replacement: getPathData() returns plain {type, values} objects, getPathData({normalize: true}) reduces any path to absolute M/L/C/Z, setPathData() writes a segment array back, and getPathSegmentAtLength() tells you which segment sits at a distance along the path. Firefox has shipped it since 137.
concepts
-
Segment inspector
Paste any
dstring (or pick a preset with arcs, quadratics and relative commands) and read it back twice: as authored withgetPathData(), and reduced to absoluteM/L/C/Zwith{normalize: true}. Focus a row to highlight that segment on the drawing. -
Point editor
The product use case: a tiny vector editor. It normalises on import, shows a draggable (and arrow-key movable) handle for every anchor and control point, and writes every move back with
setPathData()— thedstring you see is the browser’s own serialisation. -
Segment at length
Scrub a distance along a path and ask the browser which segment you are on with
getPathSegmentAtLength(), next to the cumulative-getTotalLength()workaround everyone wrote before. Includes the clamping, boundary andNaNedges from the spec. -
Validation & base value
The failure modes: what
setPathData()does with an unknown command, a wrong value count,NaN, a bad arc flag, a missing first moveto or an empty array (valid prefix, not an exception), when it does throw, why CSSdand SMIL never show up ingetPathData(), and how to migrate offSVGPathSegListand the polyfill. -
Path morph
Advanced composition: normalise two unrelated shapes (arcs vs quadratics vs lines), convert lines to cubics, split curves until the segment counts match, and interpolate value-by-value through
setPathData()— the D3-style morph with no string parser.
the API
const path = document.querySelector("path");
path.getPathData();
// [{type: "M", values: [10, 80]}, {type: "q", values: [10, 10, 20, 0]}, …] as authored
path.getPathData({ normalize: true });
// only absolute "M", "L", "C", "Z" — h/v → L, q/t/s → C, arcs → C (approximation)
const segs = path.getPathData();
segs[1].values[0] = 50;
path.setPathData(segs); // plain objects in, serialised into d by the browser
path.getPathSegmentAtLength(42); // {type, values} under that distance, or null if empty
why it shipped
The old SVGPathSegList was a live list of twenty segment classes with a factory method each, and its mutation semantics were never well specified, so Chromium removed it. That left developers parsing d strings by hand or loading path-data-polyfill for editors, data-viz morphing, hit-testing and path animation. The SVG WG designed a smaller replacement around plain dictionaries (w3c/svgwg#1082). Firefox shipped it in 137, and Chrome 160 brings it to Chromium with the same shape as the polyfill, so existing polyfill code works unchanged.