getPointAtLength()
Part of the @remotion/paths package.
Gets the coordinates of a point which is on an SVG path.
The first argument is an SVG path, the second one is the at which length the point should be sampled. It must be between 0 and the return value of getLength().
An object containing x and y is returned if the path is valid:
tsximport {getPointAtLength } from "@remotion/paths";constpoint =getPointAtLength ("M 0 0 L 100 0", 50);console .log (point ); // { x: 50, y: 0 }
tsximport {getPointAtLength } from "@remotion/paths";constpoint =getPointAtLength ("M 0 0 L 100 0", 50);console .log (point ); // { x: 50, y: 0 }
The function will throw if the path is invalid:
tsxgetPointAtLength ("remotion", 50); // Error: Malformed path data: ...
tsxgetPointAtLength ("remotion", 50); // Error: Malformed path data: ...
Example: Getting the middle point of a path
Use getLength() to get the total length of a path and then multiply it with a number between 0 and 1 to get any point on the path. For example, length * 0.5 to get the coordinates in the middle of the path.
tsximport {getLength ,getPointAtLength } from "@remotion/paths";constpath = "M 0 0 L 100 0";constlength =getLength (path );constpoint =getPointAtLength (path ,length * 0.5);console .log (point ); // { x: 50, y: 0 }
tsximport {getLength ,getPointAtLength } from "@remotion/paths";constpath = "M 0 0 L 100 0";constlength =getLength (path );constpoint =getPointAtLength (path ,length * 0.5);console .log (point ); // { x: 50, y: 0 }
Credits
Source code stems mostly from svg-path-properties.