Skip to content

🎥 Make videos programmatically with React

get it done

Jonny Burger20cc1b17cb94
diff --git a/packages/example/src/Paths/ShapesMorph.tsx b/packages/example/src/Paths/ShapesMorph.tsx
index 36c04adb21..d183cb9262 100644
--- a/packages/example/src/Paths/ShapesMorph.tsx
+++ b/packages/example/src/Paths/ShapesMorph.tsx
@@ -1,4 +1,4 @@
-import {debugPath, interpolatePath} from '@remotion/paths';
+import {PathInternals, interpolatePath} from '@remotion/paths';
import {makeCircle, makePolygon} from '@remotion/shapes';
import {AbsoluteFill, interpolate, useCurrentFrame} from 'remotion';
@@ -10,7 +10,7 @@ export const ShapesMorph: React.FC = () => {
});
const {path: polygonpath} = makePolygon({
- points: 5,
+ points: 15,
radius: 80,
});
@@ -20,7 +20,7 @@ export const ShapesMorph: React.FC = () => {
});
const interpolated = interpolatePath(interpolation, polygonpath, circlepath);
- const debugged = debugPath(interpolated);
+ const debugged = PathInternals.debugPath(interpolated);
return (
<AbsoluteFill className="bg-gray-100 items-center justify-center">
diff --git a/packages/paths/src/get-end-position.ts b/packages/paths/src/get-end-position.ts
new file mode 100644
index 0000000000..c328904550
--- /dev/null
+++ b/packages/paths/src/get-end-position.ts
@@ -0,0 +1,81 @@
+import type {Instruction, Point} from './helpers/types';
+
+export const getEndPosition = (instructions: Instruction[]): Point => {
+ let x = 0;
+ let y = 0;
+ let moveX = 0;
+ let moveY = 0;
+
+ for (let i = 0; i < instructions.length; i++) {
+ const instruction = instructions[i];
+ if (instruction.type === 'M') {
+ moveX = instruction.x;
+ moveY = instruction.y;
+ } else if (instruction.type === 'm') {
+ moveX += instruction.dx;
+ moveY += instruction.dy;
+ }
+
+ if (
+ instruction.type === 'A' ||
+ instruction.type === 'C' ||
+ instruction.type === 'L' ||
+ instruction.type === 'M' ||
+ instruction.type === 'Q' ||
+ instruction.type === 'S' ||
+ instruction.type === 'T'
+ ) {
+ x = instruction.x;
+ y = instruction.y;
+ continue;
+ }
+
+ if (
+ instruction.type === 'a' ||
+ instruction.type === 'c' ||
+ instruction.type === 'l' ||
+ instruction.type === 'm' ||
+ instruction.type === 'q' ||
+ instruction.type === 's' ||
+ instruction.type === 't'
+ ) {
+ x += instruction.dx;
+ y += instruction.dy;
+ continue;
+ }
+
+ if (instruction.type === 'H') {
+ x = instruction.x;
+ continue;
+ }
+
+ if (instruction.type === 'V') {
+ y = instruction.y;
+ continue;
+ }
+
+ if (instruction.type === 'Z') {
+ x = moveX;
+ y = moveY;
+ continue;
+ }
+
+ if (instruction.type === 'h') {
+ x += instruction.dx;
+ continue;
+ }
+
+ if (instruction.type === 'v') {
+ y += instruction.dy;
+ continue;
+ }
+
+ // @ts-expect-error
+ throw new Error('Unknown instruction type: ' + instruction.type);
+ }
+
+ return {
+ x,
+ y,
+ };
+};
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
index c8ad09d315..d1dd6afef2 100644
--- a/packages/paths/src/index.ts
+++ b/packages/paths/src/index.ts
@@ -1,6 +1,6 @@
import {getBoundingBoxFromInstructions} from './get-bounding-box';
-export {debugPath} from './debug-path';
+import {debugPath} from './debug-path';
export {evolvePath} from './evolve-path';
export {extendViewBox} from './extend-viewbox';
export {getBoundingBox} from './get-bounding-box';
@@ -26,6 +26,8 @@ export {scalePath} from './scale-path';
export {serializeInstructions} from './serialize-instructions';
export {translatePath} from './translate-path';
export {WarpPathFn, warpPath} from './warp-path';
+
export const PathInternals = {
getBoundingBoxFromInstructions,
+ debugPath,
};
diff --git a/packages/paths/src/interpolate-path/convert-to-same-instruction-type.ts b/packages/paths/src/interpolate-path/convert-to-same-instruction-type.ts
index b36255c9e5..5c59bfb6a7 100644
--- a/packages/paths/src/interpolate-path/convert-to-same-instruction-type.ts
+++ b/packages/paths/src/interpolate-path/convert-to-same-instruction-type.ts
@@ -1,14 +1,13 @@
import type {
CInstruction,
LInstruction,
+ Point,
QInstruction,
ReducedInstruction,
} from '../helpers/types';
const convertToLCommand = (command: ReducedInstruction): LInstruction => {
if (command.type === 'M' || command.type === 'L' || command.type === 'Z') {
- console.log({command});
-
throw new Error('unexpected');
}
@@ -19,7 +18,10 @@ const convertToLCommand = (command: ReducedInstruction): LInstruction => {
};
};
-const convertToCCommand = (command: ReducedInstruction): CInstruction => {
+const convertToCCommand = (
+ command: ReducedInstruction,
+ currentPoint: Point,
+): CInstruction => {
if (command.type === 'M' || command.type === 'C' || command.type === 'Z') {
throw new Error('unexpected');
}
@@ -27,8 +29,8 @@ const convertToCCommand = (command: ReducedInstruction): CInstruction => {
if (command.type === 'L') {
return {
type: 'C',
- cp1x: command.x,
- cp1y: command.y,
+ cp1x: currentPoint.x,
+ cp1y: currentPoint.y,
cp2x: command.x,
cp2y: command.y,
x: command.x,
@@ -102,6 +104,7 @@ const convertToQCommand = (command: ReducedInstruction): QInstruction => {
export function convertToSameInstructionType(
aCommand: ReducedInstruction,
bCommand: ReducedInstruction,
+ currentPoint: Point,
): ReducedInstruction {
if (aCommand.type === 'M' || bCommand.type === 'M') {
return {...aCommand};
@@ -112,7 +115,7 @@ export function convertToSameInstructionType(
}
if (bCommand.type === 'C') {
- return convertToCCommand(aCommand);
+ return convertToCCommand(aCommand, currentPoint);
}
if (bCommand.type === 'L') {
diff --git a/packages/paths/src/interpolate-path/interpolate-instructions.ts b/packages/paths/src/interpolate-path/interpolate-instructions.ts
index 6c8f16a25d..b697c04fd1 100644
--- a/packages/paths/src/interpolate-path/interpolate-instructions.ts
+++ b/packages/paths/src/interpolate-path/interpolate-instructions.ts
@@ -1,3 +1,4 @@
+import {getEndPosition} from '../get-end-position';
import type {ReducedInstruction} from '../helpers/types';
import {convertToSameInstructionType} from './convert-to-same-instruction-type';
import {extendInstruction} from './extend-command';
@@ -74,7 +75,9 @@ export function interpolateInstructions(
// commands have same length now.
// convert commands in A to the same type as those in B
const aSameType = aCommands.map((aCommand, i) => {
- return convertToSameInstructionType(aCommand, bCommands[i]);
+ const commandsUntilNow = aCommands.slice(0, i);
+ const point = getEndPosition(commandsUntilNow);
+ return convertToSameInstructionType(aCommand, bCommands[i], point);
});
const interpolatedCommands: ReducedInstruction[] = [];
diff --git a/packages/paths/src/interpolate-path/interpolate-path.ts b/packages/paths/src/interpolate-path/interpolate-path.ts
index 78e10b22b6..59ffe664a3 100644
--- a/packages/paths/src/interpolate-path/interpolate-path.ts
+++ b/packages/paths/src/interpolate-path/interpolate-path.ts
@@ -69,7 +69,5 @@ export const interpolatePath = (
const commandInterpolator = interpolateInstructions(aCommands, bCommands);
- const interpolatedCommands = commandInterpolator(value);
-
- return serializeInstructions(interpolatedCommands);
+ return serializeInstructions(commandInterpolator(value));
};
diff --git a/packages/paths/src/interpolate-path/split-curve-as-points.ts b/packages/paths/src/interpolate-path/split-curve-as-points.ts
index 927a0e89f3..0641c3ae8c 100644
--- a/packages/paths/src/interpolate-path/split-curve-as-points.ts
+++ b/packages/paths/src/interpolate-path/split-curve-as-points.ts
@@ -9,10 +9,8 @@ import {decasteljau} from './de-casteljau';
*/
export function splitCurveAsPoints(
points: number[][],
- segmentCount: number,
+ segmentCount = 2,
): number[][][] {
- segmentCount = segmentCount || 2;
-
const segments = [];
let remainingCurve = points;
const tIncrement = 1 / segmentCount;