v160 · dom · svg
Segment at length
getPointAtLength() has always told you where a distance lands. getPathSegmentAtLength() tells you which command it lands in — the question behind progress-along-a-route UIs, hit-testing a click to a segment, or labelling the leg of a path animation. Scrub the distance, compare the native answer with the cumulative-length workaround everyone wrote before, then try the edges the spec defines.
Route
native getPathSegmentAtLength()
pre-API workaround (cumulative getTotalLength)
native vs. the old way
// native (Chrome 160+, Firefox 137+)
const seg = path.getPathSegmentAtLength(d); // {type, values} or null
// distance is clamped to [0, getTotalLength()]; on an exact boundary the
// EARLIER segment wins; NaN/Infinity throw a TypeError (IDL "float").
// the workaround: measure every prefix of the path
const segs = path.getPathData();
let i = 0;
for (; i < segs.length; i++) {
probe.setPathData(segs.slice(0, i + 1));
if (probe.getTotalLength() >= d) break; // O(n) layouts per query
}
The segment returned is as authored (not normalised) and comes from the d attribute’s base value, the same list getPathData() gives you. That makes it easy to go from “the user clicked here” to “edit this command”.