Skip to main content

getInstructionIndexAtLength()

属于 @remotion/paths 软件包的一部分.

🌐 Part of the @remotion/paths package.

从 v4.0.84 起可用

🌐 available from v4.0.84

获取位于路径长度处的指令索引。
第一个参数是 SVG 路径,第二个参数是应采样点的长度。
它必须介于 0getLength() 的返回值之间。

🌐 Gets the index of the instruction that is at the length of the path.
The first argument is an SVG path, the second one is the length at which the point should be sampled.
It must be between 0 and the return value of getLength().

如果路径有效,将返回包含 indexlengthIntoInstruction 的对象:

🌐 An object containing index and lengthIntoInstruction is returned if the path is valid:

Example
import { getInstructionIndexAtLength } from "@remotion/paths"; const { index, lengthIntoInstruction } = getInstructionIndexAtLength( "M 0 0 L 100 0 L 200 0", 105, ); console.log(index); // 1 console.log(lengthIntoInstruction); // 5

要获取特定索引处的指令,你可以使用 parsePath():

🌐 To get the instruction at a specific index, you can use parsePath():

Get instruction
import { getInstructionIndexAtLength, parsePath } from "@remotion/paths"; const path = "M 0 0 L 100 0 L 200 0"; const { index } = getInstructionIndexAtLength(path, 105); const parsed = parsePath(path); const instruction = parsed[index]; // {type: 'L', x: 100, y: 0}

如果路径无效,函数将抛出异常:

🌐 The function will throw if the path is invalid:

getInstructionIndexAtLength("remotion", 50); // Error: Malformed path data: ...

如果样本长度大于路径的length,该函数将抛出异常:

🌐 The function will throw if the sample length is bigger than the length of the path:

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

🌐 This function was adapted from svg-path-properties.

另请参阅

🌐 See also