getInstructionIndexAtLength()
属于@remotion/paths
包。
从v4.0.84版本开始可用。
获取路径长度处指令的索引。
第一个参数是SVG路径,第二个参数是应该采样点的长度。
它必须介于0
和getLength()
的返回值之间。
如果路径有效,则返回一个包含index
和lengthIntoInstruction
的对象:
示例tsx
import {getInstructionIndexAtLength } from "@remotion/paths";const {index ,lengthIntoInstruction } =getInstructionIndexAtLength ("M 0 0 L 100 0 L 200 0",105,);console .log (index ); // 1console .log (lengthIntoInstruction ); // 5
示例tsx
import {getInstructionIndexAtLength } from "@remotion/paths";const {index ,lengthIntoInstruction } =getInstructionIndexAtLength ("M 0 0 L 100 0 L 200 0",105,);console .log (index ); // 1console .log (lengthIntoInstruction ); // 5
要获取特定索引处的指令,可以使用parsePath()
:
获取指令tsx
import {getInstructionIndexAtLength ,parsePath } from "@remotion/paths";constpath = "M 0 0 L 100 0 L 200 0";const {index } =getInstructionIndexAtLength (path , 105);constparsed =parsePath (path );constinstruction =parsed [index ]; // {type: 'L', x: 100, y: 0}
获取指令tsx
import {getInstructionIndexAtLength ,parsePath } from "@remotion/paths";constpath = "M 0 0 L 100 0 L 200 0";const {index } =getInstructionIndexAtLength (path , 105);constparsed =parsePath (path );constinstruction =parsed [index ]; // {type: 'L', x: 100, y: 0}
如果路径无效,该函数将抛出异常:
tsx
getInstructionIndexAtLength ("remotion", 50); // Error: Malformed path data: ...
tsx
getInstructionIndexAtLength ("remotion", 50); // Error: Malformed path data: ...
如果采样长度大于路径的长度,该函数将抛出异常:
tsx
getInstructionIndexAtLength ("M 0 0 L 100 0", 105); // Error: A length of 105 was passed to getInstructionIndexAtLength() but the total length of the path is only 100;
tsx
getInstructionIndexAtLength ("M 0 0 L 100 0", 105); // Error: A length of 105 was passed to getInstructionIndexAtLength() but the total length of the path is only 100;
Credits
此函数改编自svg-path-properties。