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.

To run this before Chrome 160: enable chrome://flags/#enable-experimental-web-platform-features (there is no dedicated flag entry), or launch Chrome/Canary with --enable-blink-features=SVGPathDataAPI, --enable-features=SVGPathDataAPI, or --enable-experimental-web-platform-features. In Chrome 153 behind the flag only getPathData/setPathData exist; getPathSegmentAtLength landed later.
Probing this browser…

concepts

  1. Segment inspector

    Paste any d string (or pick a preset with arcs, quadratics and relative commands) and read it back twice: as authored with getPathData(), and reduced to absolute M/L/C/Z with {normalize: true}. Focus a row to highlight that segment on the drawing.

  2. 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() — the d string you see is the browser’s own serialisation.

  3. 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 and NaN edges from the spec.

  4. 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 CSS d and SMIL never show up in getPathData(), and how to migrate off SVGPathSegList and the polyfill.

  5. 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.

references