Skip to content

🎥 Make videos programmatically with React

let's go

Jonny Burger6748df81f7ad
diff --git a/packages/docs/docs/paths/get-instruction-index-at-length.md b/packages/docs/docs/paths/get-instruction-index-at-length.md
new file mode 100644
index 0000000000..2fed7415da
--- /dev/null
+++ b/packages/docs/docs/paths/get-instruction-index-at-length.md
@@ -0,0 +1,64 @@
+---
+title: getInstructionIndexAtLength()
+crumb: "@remotion/paths"
+---
+
+_Part of the [`@remotion/paths`](/docs/paths) package._
+
+_available from v4.0.84_
+
+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()`](/docs/paths/get-length).
+
+An object containing `index` and `lengthIntoInstruction` is returned if the path is valid:
+
+```tsx twoslash title="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
+```
+
+To get the instruction at a specific index, you can use [`parsePath()`](/docs/paths/parse-path):
+
+```tsx twoslash title="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:
+
+```tsx twoslash
+import { getInstructionIndexAtLength } from "@remotion/paths";
+// ---cut---
+getInstructionIndexAtLength("remotion", 50); // Error: Malformed path data: ...
+```
+
+The function **will throw** if the sample length is bigger than the [length](/docs/paths/get-length) of the path:
+
+```tsx twoslash
+import { getInstructionIndexAtLength } from "@remotion/paths";
+// ---cut---
+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
+
+This function was adapted from [svg-path-properties](https://www.npmjs.com/package/svg-path-properties).
+
+## See also
+
+- [`getLength()`](/docs/paths/get-length)
+- [`parsePath()`](/docs/paths/parse-path)
+- [`@remotion/paths`](/docs/paths)
+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/paths/src/get-instruction-index-at-length.ts)
diff --git a/packages/docs/docs/paths/get-point-at-length.md b/packages/docs/docs/paths/get-point-at-length.md
index 85231c5d0b..0a5332b785 100644
--- a/packages/docs/docs/paths/get-point-at-length.md
+++ b/packages/docs/docs/paths/get-point-at-length.md
@@ -7,7 +7,7 @@ crumb: "@remotion/paths"
_Part of the [`@remotion/paths`](/docs/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()`](/docs/paths/get-length).
+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()`](/docs/paths/get-length).
An object containing `x` and `y` is returned if the path is valid:
diff --git a/packages/docs/docs/paths/get-tangent-at-length.md b/packages/docs/docs/paths/get-tangent-at-length.md
index 1a3aea82b7..f9d15359d6 100644
--- a/packages/docs/docs/paths/get-tangent-at-length.md
+++ b/packages/docs/docs/paths/get-tangent-at-length.md
@@ -6,7 +6,7 @@ crumb: "@remotion/paths"
_Part of the [`@remotion/paths`](/docs/paths) package._
-Gets tangent values `x` and `y` 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()`](/docs/paths/get-length).
+Gets tangent values `x` and `y` of a point which is on an SVG 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()`](/docs/paths/get-length).
Returns a point if the path is valid:
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 62474a1ff7..8b67189bd4 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -325,6 +325,7 @@ module.exports = {
"paths/get-length",
"paths/get-point-at-length",
"paths/get-tangent-at-length",
+ "paths/get-instruction-index-at-length",
"paths/reverse-path",
"paths/normalize-path",
"paths/interpolate-path",
diff --git a/packages/paths/src/get-instruction-index-at-length.ts b/packages/paths/src/get-instruction-index-at-length.ts
new file mode 100644
index 0000000000..26ee8642c7
--- /dev/null
+++ b/packages/paths/src/get-instruction-index-at-length.ts
@@ -0,0 +1,45 @@
+import type {Constructed} from './helpers/construct';
+import {construct} from './helpers/construct';
+
+type InstructionAtLength = {
+ index: number;
+ lengthIntoInstruction: number;
+};
+
+export const getInstructionIndexAtLengthFromConstructed = (
+ constructed: Constructed,
+ fractionLength: number,
+): InstructionAtLength => {
+ if (fractionLength < 0) {
+ throw new Error('Length less than 0 was passed');
+ }
+
+ if (fractionLength > constructed.totalLength) {
+ fractionLength = constructed.totalLength;
+ }
+
+ let index = constructed.partialLengths.length - 1;
+
+ while (constructed.partialLengths[index] >= fractionLength && index > 0) {
+ index--;
+ }
+
+ return {
+ lengthIntoInstruction: fractionLength - constructed.partialLengths[index],
+ index,
+ };
+};
+
+export const getInstructionIndexAtLength = (
+ path: string,
+ length: number,
+): InstructionAtLength => {
+ const constructed = construct(path);
+ if (length > constructed.totalLength) {
+ throw new Error(
+ `A length of ${length} was passed to getInstructionIndexAtLength() but the total length of the path is only ${constructed.totalLength}`,
+ );
+ }
+
+ return getInstructionIndexAtLengthFromConstructed(constructed, length);
+};
diff --git a/packages/paths/src/get-length.ts b/packages/paths/src/get-length.ts
index 87d8577bbd..f0fd444aab 100644
--- a/packages/paths/src/get-length.ts
+++ b/packages/paths/src/get-length.ts
@@ -9,5 +9,5 @@ import {construct} from './helpers/construct';
*/
export const getLength = (path: string) => {
const constructucted = construct(path);
- return constructucted.length;
+ return constructucted.totalLength;
};
diff --git a/packages/paths/src/get-point-at-length.ts b/packages/paths/src/get-point-at-length.ts
index a3eaf3ff27..2e7c7988ad 100644
--- a/packages/paths/src/get-point-at-length.ts
+++ b/packages/paths/src/get-point-at-length.ts
@@ -1,5 +1,5 @@
+import {getInstructionIndexAtLengthFromConstructed} from './get-instruction-index-at-length';
import {construct} from './helpers/construct';
-import {getInstructionIndexAtLength} from './helpers/get-part-at-length';
/**
* @description Gets the coordinates of a point which is on an SVG path.
@@ -9,11 +9,14 @@ import {getInstructionIndexAtLength} from './helpers/get-part-at-length';
*/
export const getPointAtLength = (path: string, length: number) => {
const constructed = construct(path);
- const fractionPart = getInstructionIndexAtLength(constructed, length);
- const functionAtPart = constructed.functions[fractionPart.index];
+ const fractionPart = getInstructionIndexAtLengthFromConstructed(
+ constructed,
+ length,
+ );
+ const functionAtPart = constructed.functions[fractionPart.index + 1];
if (functionAtPart) {
- return functionAtPart.getPointAtLength(fractionPart.fraction);
+ return functionAtPart.getPointAtLength(fractionPart.lengthIntoInstruction);
}
if (constructed.initialPoint) {
diff --git a/packages/paths/src/get-tangent-at-length.ts b/packages/paths/src/get-tangent-at-length.ts
index fc42993ef3..6832a1cbe6 100644
--- a/packages/paths/src/get-tangent-at-length.ts
+++ b/packages/paths/src/get-tangent-at-length.ts
@@ -1,5 +1,5 @@
+import {getInstructionIndexAtLengthFromConstructed} from './get-instruction-index-at-length';
import {construct} from './helpers/construct';
-import {getInstructionIndexAtLength} from './helpers/get-part-at-length';
/**
* @description Gets tangent values x and y of a point which is on an SVG path
@@ -11,10 +11,15 @@ import {getInstructionIndexAtLength} from './helpers/get-part-at-length';
export const getTangentAtLength = (path: string, length: number) => {
const constructed = construct(path);
- const fractionPart = getInstructionIndexAtLength(constructed, length);
- const functionAtPart = constructed.functions[fractionPart.index];
+ const fractionPart = getInstructionIndexAtLengthFromConstructed(
+ constructed,
+ length,
+ );
+ const functionAtPart = constructed.functions[fractionPart.index + 1];
if (functionAtPart) {
- return functionAtPart.getTangentAtLength(fractionPart.fraction);
+ return functionAtPart.getTangentAtLength(
+ fractionPart.lengthIntoInstruction,
+ );
}
if (constructed.initialPoint) {
diff --git a/packages/paths/src/helpers/construct.ts b/packages/paths/src/helpers/construct.ts
index 365dd3e8cf..a2dfc6bcdd 100644
--- a/packages/paths/src/helpers/construct.ts
+++ b/packages/paths/src/helpers/construct.ts
@@ -9,13 +9,15 @@ import type {Instruction, Point, PointArray, Properties} from './types';
export type Constructed = {
segments: Instruction[][];
initialPoint: Point | null;
- length: number;
+ totalLength: number;
partialLengths: number[];
functions: (Properties | null)[];
};
-export const constructFromInstructions = (instructions: Instruction[]) => {
- let length = 0;
+export const constructFromInstructions = (
+ instructions: Instruction[],
+): Constructed => {
+ let totalLength = 0;
const partialLengths: number[] = [];
const functions: (null | Properties)[] = [];
let initialPoint: null | Point = null;
@@ -61,7 +63,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
}
if (instruction.type === 'L') {
- length += Math.sqrt(
+ totalLength += Math.sqrt(
(cur[0] - instruction.x) ** 2 + (cur[1] - instruction.y) ** 2,
);
functions.push(
@@ -76,7 +78,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
}
if (instruction.type === 'l') {
- length += Math.sqrt(instruction.dx ** 2 + instruction.dy ** 2);
+ totalLength += Math.sqrt(instruction.dx ** 2 + instruction.dy ** 2);
functions.push(
makeLinearPosition({
x0: cur[0],
@@ -89,7 +91,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
}
if (instruction.type === 'H') {
- length += Math.abs(cur[0] - instruction.x);
+ totalLength += Math.abs(cur[0] - instruction.x);
functions.push(
makeLinearPosition({
x0: cur[0],
@@ -102,7 +104,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
}
if (instruction.type === 'h') {
- length += Math.abs(instruction.dx);
+ totalLength += Math.abs(instruction.dx);
functions.push(
makeLinearPosition({
x0: cur[0],
@@ -113,7 +115,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
);
cur[0] = instruction.dx + cur[0];
} else if (instruction.type === 'V') {
- length += Math.abs(cur[1] - instruction.y);
+ totalLength += Math.abs(cur[1] - instruction.y);
functions.push(
makeLinearPosition({
x0: cur[0],
@@ -126,7 +128,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
}
if (instruction.type === 'v') {
- length += Math.abs(instruction.dy);
+ totalLength += Math.abs(instruction.dy);
functions.push(
makeLinearPosition({
x0: cur[0],
@@ -138,7 +140,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
cur[1] = instruction.dy + cur[1];
// Close path
} else if (instruction.type === 'Z') {
- length += Math.sqrt(
+ totalLength += Math.sqrt(
(ringStart[0] - cur[0]) ** 2 + (ringStart[1] - cur[1]) ** 2,
);
functions.push(
@@ -164,7 +166,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
x: instruction.x,
y: instruction.y,
});
- length += curve.getTotalLength();
+ totalLength += curve.getTotalLength();
cur = [instruction.x, instruction.y];
functions.push(curve);
} else if (instruction.type === 'c') {
@@ -179,7 +181,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
y: cur[1] + instruction.dy,
});
if (curve.getTotalLength() > 0) {
- length += curve.getTotalLength();
+ totalLength += curve.getTotalLength();
functions.push(curve);
cur = [instruction.dx + cur[0], instruction.dy + cur[1]];
} else {
@@ -224,7 +226,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
}
if (curve) {
- length += curve.getTotalLength();
+ totalLength += curve.getTotalLength();
cur = [instruction.x, instruction.y];
functions.push(curve);
}
@@ -267,7 +269,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
}
if (curve) {
- length += curve.getTotalLength();
+ totalLength += curve.getTotalLength();
cur = [instruction.dx + cur[0], instruction.dy + cur[1]];
functions.push(curve);
}
@@ -282,7 +284,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
y0: instruction.cpy,
y1: instruction.y,
});
- length += linearCurve.getTotalLength();
+ totalLength += linearCurve.getTotalLength();
functions.push(linearCurve);
} else {
curve = makeQuadratic({
@@ -293,7 +295,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
x: instruction.x,
y: instruction.y,
});
- length += curve.getTotalLength();
+ totalLength += curve.getTotalLength();
functions.push(curve);
}
@@ -309,7 +311,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
y0: cur[1] + instruction.dx,
y1: cur[1] + instruction.dy,
});
- length += linearCurve.getTotalLength();
+ totalLength += linearCurve.getTotalLength();
functions.push(linearCurve);
} else {
curve = makeQuadratic({
@@ -320,7 +322,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
x: cur[0] + instruction.dx,
y: cur[1] + instruction.dy,
});
- length += curve.getTotalLength();
+ totalLength += curve.getTotalLength();
functions.push(curve);
}
@@ -345,7 +347,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
y: instruction.y,
});
functions.push(curve);
- length += curve.getTotalLength();
+ totalLength += curve.getTotalLength();
} else {
const linearCurve = makeLinearPosition({
x0: cur[0],
@@ -354,7 +356,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
y1: instruction.y,
});
functions.push(linearCurve);
- length += linearCurve.getTotalLength();
+ totalLength += linearCurve.getTotalLength();
}
prev_point = [2 * cur[0] - prev_point[0], 2 * cur[1] - prev_point[1]];
@@ -377,7 +379,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
x: cur[0] + instruction.dx,
y: cur[1] + instruction.dy,
});
- length += curve.getTotalLength();
+ totalLength += curve.getTotalLength();
functions.push(curve);
} else {
const linearCurve = makeLinearPosition({
@@ -386,7 +388,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
y0: cur[1],
y1: cur[1] + instruction.dy,
});
- length += linearCurve.getTotalLength();
+ totalLength += linearCurve.getTotalLength();
functions.push(linearCurve);
}
@@ -407,7 +409,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
y1: instruction.y,
});
- length += arcCurve.getTotalLength();
+ totalLength += arcCurve.getTotalLength();
cur = [instruction.x, instruction.y];
functions.push(arcCurve);
}
@@ -425,18 +427,18 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
y1: cur[1] + instruction.dy,
});
- length += arcCurve.getTotalLength();
+ totalLength += arcCurve.getTotalLength();
cur = [cur[0] + instruction.dx, cur[1] + instruction.dy];
functions.push(arcCurve);
}
- partialLengths.push(length);
+ partialLengths.push(totalLength);
}
return {
segments,
initialPoint,
- length,
+ totalLength,
partialLengths,
functions,
};
diff --git a/packages/paths/src/helpers/get-part-at-length.ts b/packages/paths/src/helpers/get-part-at-length.ts
deleted file mode 100644
index 196e46965b..0000000000
--- a/packages/paths/src/helpers/get-part-at-length.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import type {Constructed} from './construct';
-
-type SegmentAtLength = {
- index: number;
- fraction: number;
-};
-
-export const getInstructionIndexAtLength = (
- constructed: Constructed,
- fractionLength: number,
-): SegmentAtLength => {
- if (fractionLength < 0) {
- fractionLength = 0;
- } else if (fractionLength > constructed.length) {
- fractionLength = constructed.length;
- }
-
- let index = constructed.partialLengths.length - 1;
-
- while (constructed.partialLengths[index] >= fractionLength && index > 0) {
- index--;
- }
-
- index++;
- return {
- fraction: fractionLength - constructed.partialLengths[index - 1],
- index,
- };
-};
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
index d564d5c3e2..22b50fdd5d 100644
--- a/packages/paths/src/index.ts
+++ b/packages/paths/src/index.ts
@@ -1,6 +1,7 @@
export {evolvePath} from './evolve-path';
export {extendViewBox} from './extend-viewbox';
export {getBoundingBox} from './get-bounding-box';
+export {getInstructionIndexAtLength} from './get-instruction-index-at-length';
export {getLength} from './get-length';
export {getPointAtLength} from './get-point-at-length';
export {getSubpaths} from './get-subpaths';
@@ -21,4 +22,4 @@ export {reversePath} from './reverse-path';
export {scalePath} from './scale-path';
export {serializeInstructions} from './serialize-instructions';
export {translatePath} from './translate-path';
-export {warpPath, WarpPathFn} from './warp-path';
+export {WarpPathFn, warpPath} from './warp-path';
diff --git a/packages/paths/src/test/get-instruction-index-at-length.test.ts b/packages/paths/src/test/get-instruction-index-at-length.test.ts
new file mode 100644
index 0000000000..4bad263511
--- /dev/null
+++ b/packages/paths/src/test/get-instruction-index-at-length.test.ts
@@ -0,0 +1,21 @@
+import {expect, test} from 'vitest';
+import {getInstructionIndexAtLength} from '../get-instruction-index-at-length';
+import {parsePath} from '../parse-path';
+
+test('getInstructionIndexAtLength() should work', () => {
+ expect(() => getInstructionIndexAtLength('M 0 0', 100)).toThrow(
+ 'A length of 100 was passed to getInstructionIndexAtLength() but the total length of the path is only 0',
+ );
+ expect(getInstructionIndexAtLength('M 0 0', 0)).toEqual({
+ lengthIntoInstruction: 0,
+ index: 0,
+ });
+ expect(getInstructionIndexAtLength('M 0 0 L 100 0 L 200 0', 104)).toEqual({
+ lengthIntoInstruction: 4,
+ index: 1,
+ });
+
+ const path = 'M 0 0 L 100 0 L 200 0';
+ const {index} = getInstructionIndexAtLength(path, 104);
+ expect(parsePath(path)[index]).toEqual({type: 'L', x: 100, y: 0});
+});