Skip to content

🎥 Make videos programmatically with React

implement warpPath()

Jonny Burger0c88445b0748
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
index a267cb2bdd..0adbe67735 100644
--- a/packages/paths/src/index.ts
+++ b/packages/paths/src/index.ts
@@ -21,3 +21,4 @@ export {resetPath} from './reset-path';
export {reversePath} from './reverse-path';
export {serializeInstructions} from './serialize-instructions';
export {translatePath} from './translate-path';
+export {warpPath} from './warp-path';
diff --git a/packages/paths/src/warp-path/index.ts b/packages/paths/src/warp-path/index.ts
new file mode 100644
index 0000000000..bddb7ff2f3
--- /dev/null
+++ b/packages/paths/src/warp-path/index.ts
@@ -0,0 +1,21 @@
+import {parsePath} from '../parse-path';
+import {reduceInstructions} from '../reduce-instructions';
+import {serializeInstructions} from '../serialize-instructions';
+import type {PointTransformer} from './warp-helpers';
+import {
+ fixZInstruction,
+ svgPathInterpolate,
+ warpTransform,
+} from './warp-helpers';
+
+export const warpPath = (
+ path: string,
+ transformer: PointTransformer,
+ interpolationThreshold: number
+): string => {
+ const reduced = reduceInstructions(parsePath(path));
+ const withZFix = fixZInstruction(reduced);
+ const interpolated = svgPathInterpolate(withZFix, interpolationThreshold);
+
+ return serializeInstructions(warpTransform(interpolated, transformer));
+};
diff --git a/packages/paths/src/warp-path/warp-helpers.ts b/packages/paths/src/warp-path/warp-helpers.ts
new file mode 100644
index 0000000000..ddc9527113
--- /dev/null
+++ b/packages/paths/src/warp-path/warp-helpers.ts
@@ -0,0 +1,308 @@
+import type {ReducedInstruction} from '../helpers/types';
+
+export type PointTransformer = (point: {x: number; y: number}) => {
+ x: number;
+ y: number;
+};
+
+type TTransformer = (
+ segment: ReducedInstruction,
+ i: number,
+ path: ReducedInstruction[],
+ newPath: ReducedInstruction[]
+) => ReducedInstruction[] | false;
+
+function pathTransform(
+ path: ReducedInstruction[],
+ transformer: TTransformer
+): ReducedInstruction[] {
+ const newPath = [];
+
+ for (let i = 0; i < path.length; i++) {
+ const result = transformer(path[i], i, path, newPath);
+
+ if (result) {
+ newPath.push(...result);
+ }
+ }
+
+ return newPath;
+}
+
+export function svgPathInterpolate(
+ path: ReducedInstruction[],
+ threshold: number
+): ReducedInstruction[] {
+ let didWork = false;
+
+ const deltaFunction: DeltaFunction = (points) => {
+ const linearPoints = [
+ points[0].slice(0, 2),
+ points[points.length - 1].slice(0, 2),
+ ] as [number, number][];
+
+ const delta = euclideanDistance(linearPoints);
+ didWork = didWork || delta > threshold;
+
+ return delta;
+ };
+
+ return warpInterpolate(path, threshold, deltaFunction);
+}
+
+type DeltaFunction = (points: [number, number][]) => number;
+
+function warpInterpolate(
+ path: ReducedInstruction[],
+ threshold: number,
+ deltaFunction: DeltaFunction
+) {
+ let prexX = 0;
+ let prexY = 0;
+
+ return pathTransform(path, (segment) => {
+ const points: [number, number][] = [[prexX, prexY]];
+
+ if (segment.type !== 'Z') {
+ prexX = segment.x;
+ prexY = segment.y;
+ }
+
+ if (segment.type === 'C') {
+ points.push([segment.cp1x, segment.cp1y]);
+ points.push([segment.cp2x, segment.cp2y]);
+ points.push([segment.x, segment.y]);
+ }
+
+ if (segment.type === 'L') {
+ points.push([segment.x, segment.y]);
+ }
+
+ if (segment.type === 'Q') {
+ points.push([segment.cpx, segment.cpy]);
+ points.push([segment.x, segment.y]);
+ }
+
+ if (segment.type === 'C' || segment.type === 'Q' || segment.type === 'L') {
+ return interpolateUntil(points, threshold, deltaFunction).map(
+ (rawSegment) => createLineSegment(rawSegment)
+ );
+ }
+
+ return [segment];
+ });
+}
+
+export const warpTransform = (
+ path: ReducedInstruction[],
+ transformer: PointTransformer
+) => {
+ return pathTransform(path, (segment): ReducedInstruction[] => {
+ if (segment.type === 'L') {
+ const {x, y} = transformer({x: segment.x, y: segment.y});
+ return [
+ {
+ type: 'L',
+ x,
+ y,
+ },
+ ];
+ }
+
+ if (segment.type === 'Q') {
+ const {x, y} = transformer({x: segment.x, y: segment.y});
+ const {x: cpx, y: cpy} = transformer({
+ x: segment.cpx,
+ y: segment.cpy,
+ });
+ return [
+ {
+ type: 'Q',
+ x,
+ y,
+ cpx,
+ cpy,
+ },
+ ];
+ }
+
+ if (segment.type === 'C') {
+ const {x, y} = transformer({x: segment.x, y: segment.y});
+ const {x: cp1x, y: cp1y} = transformer({
+ x: segment.cp1x,
+ y: segment.cp1y,
+ });
+ const {x: cp2x, y: cp2y} = transformer({
+ x: segment.cp2x,
+ y: segment.cp2y,
+ });
+
+ return [
+ {
+ type: 'C',
+ x,
+ y,
+ cp1x,
+ cp1y,
+ cp2x,
+ cp2y,
+ },
+ ];
+ }
+
+ if (segment.type === 'M') {
+ const {x, y} = transformer({x: segment.x, y: segment.y});
+ return [
+ {
+ type: 'M',
+ x,
+ y,
+ },
+ ];
+ }
+
+ return [segment];
+ });
+};
+
+// Add a line from second to last point to last point and then keep Z so it can be transformed as well
+export const fixZInstruction = (
+ instructions: ReducedInstruction[]
+): ReducedInstruction[] => {
+ let prevX = 0;
+ let prevY = 0;
+
+ return instructions
+ .map((instruction): ReducedInstruction[] => {
+ if (instruction.type === 'Z') {
+ return [
+ {
+ type: 'L',
+ x: prevX,
+ y: prevY,
+ },
+ {
+ type: 'Z',
+ },
+ ];
+ }
+
+ if (instruction.type === 'M') {
+ prevX = instruction.x;
+ prevY = instruction.y;
+ }
+
+ return [instruction];
+ })
+ .flat(1);
+};
+
+const euclideanDistance = (points: [number, number][]) => {
+ const startPoint = points[0];
+ const endPoint = points[points.length - 1];
+ let d2 = 0;
+
+ for (let i = 0; i < startPoint.length; i++) {
+ const d = endPoint[i] - startPoint[i];
+ d2 += d ** 2;
+ }
+
+ return Math.sqrt(d2);
+};
+
+export function interpolateUntil(
+ points: [number, number][],
+ threshold: number,
+ deltaFunction = euclideanDistance
+) {
+ const stack = [points];
+ const segments = [];
+
+ while (stack.length > 0) {
+ const currentPoints = stack.pop() as [number, number][];
+
+ if (deltaFunction(currentPoints) > threshold) {
+ const newPoints = split(currentPoints);
+
+ // Add new segments backwards so they end up in correct order
+ for (let i = newPoints.length - 1; i >= 0; i--) {
+ stack.push(newPoints[i] as [number, number][]);
+ }
+ } else {
+ segments.push(currentPoints);
+ }
+ }
+
+ return segments;
+}
+
+export function split(p: number[][], t = 0.5) {
+ const seg0 = [];
+ const seg1 = [];
+ const orders = [p];
+
+ while (orders.length < p.length) {
+ const q = orders[orders.length - 1];
+ const r = [];
+
+ for (let i = 1; i < q.length; i++) {
+ const q0 = q[i - 1];
+ const q1 = q[i];
+ const s = [];
+ const dim = Math.max(q0.length, q1.length);
+
+ for (let j = 0; j < dim; j++) {
+ const s0 = q0[j] || 0;
+ const s1 = q1[j] || 0;
+
+ s.push(s0 + (s1 - s0) * t);
+ }
+
+ r.push(s);
+ }
+
+ orders.push(r);
+ }
+
+ for (let i = 0; i < orders.length; i++) {
+ seg0.push(orders[i][0]);
+ seg1.push(orders[orders.length - 1 - i][i]);
+ }
+
+ return [seg0, seg1];
+}
+
+export function createLineSegment(points: number[][]): ReducedInstruction {
+ switch (points.length) {
+ case 2:
+ return {
+ type: 'L',
+ x: points[1][0],
+ y: points[1][1],
+ };
+
+ case 3:
+ return {
+ type: 'Q',
+ cpx: points[1][0],
+ cpy: points[1][1],
+ x: points[2][0],
+ y: points[2][1],
+ };
+
+ case 4:
+ return {
+ type: 'C',
+ cp1x: points[1][0],
+ cp1y: points[1][1],
+ cp2x: points[2][0],
+ cp2y: points[2][1],
+ x: points[3][0],
+ y: points[3][1],
+ };
+ default:
+ throw new Error(
+ 'Expected 2, 3 or 4 points for a line segment, got ' + points.length
+ );
+ }
+}