Skip to content

🎥 Make videos programmatically with React

draft an interpolatePath function

Jonny Burger0ddb440c2f49
diff --git a/packages/paths/src/helpers/split-curve.ts b/packages/paths/src/helpers/split-curve.ts
new file mode 100644
index 0000000000..909c14381f
--- /dev/null
+++ b/packages/paths/src/helpers/split-curve.ts
@@ -0,0 +1,201 @@
+/**
+ * de Casteljau's algorithm for drawing and splitting bezier curves.
+ * Inspired by https://pomax.github.io/bezierinfo/
+ *
+ * @param {Number[][]} points Array of [x,y] points: [start, control1, control2, ..., end]
+ * The original segment to split.
+ * @param {Number} t Where to split the curve (value between [0, 1])
+ * @return {Object} An object { left, right } where left is the segment from 0..t and
+ * right is the segment from t..1.
+ */
+function decasteljau(
+ points: number[][],
+ t: number
+): {left: number[][]; right: number[][]} {
+ const left: number[][] = [];
+ const right: number[][] = [];
+
+ function decasteljauRecurse(_points: number[][], _t: number) {
+ if (_points.length === 1) {
+ left.push(_points[0]);
+ right.push(_points[0]);
+ } else {
+ const newPoints = Array(_points.length - 1);
+
+ for (let i = 0; i < newPoints.length; i++) {
+ if (i === 0) {
+ left.push(_points[0]);
+ }
+
+ if (i === newPoints.length - 1) {
+ right.push(_points[i + 1]);
+ }
+
+ newPoints[i] = [
+ (1 - _t) * _points[i][0] + _t * _points[i + 1][0],
+ (1 - _t) * _points[i][1] + _t * _points[i + 1][1],
+ ];
+ }
+
+ decasteljauRecurse(newPoints, _t);
+ }
+ }
+
+ if (points.length) {
+ decasteljauRecurse(points, t);
+ }
+
+ return {left, right: right.reverse()};
+}
+
+/**
+ * List of params for each command type in a path `d` attribute
+ */
+export const typeMap = {
+ M: ['x', 'y'],
+ L: ['x', 'y'],
+ H: ['x'],
+ V: ['y'],
+ C: ['x1', 'y1', 'x2', 'y2', 'x', 'y'],
+ S: ['x2', 'y2', 'x', 'y'],
+ Q: ['x1', 'y1', 'x', 'y'],
+ T: ['x', 'y'],
+ A: ['rx', 'ry', 'xAxisRotation', 'largeArcFlag', 'sweepFlag', 'x', 'y'],
+ Z: [],
+ m: ['x', 'y'],
+ l: ['x', 'y'],
+ h: ['x'],
+ v: ['y'],
+ c: ['x1', 'y1', 'x2', 'y2', 'x', 'y'],
+ s: ['x2', 'y2', 'x', 'y'],
+ q: ['x1', 'y1', 'x', 'y'],
+ t: ['x', 'y'],
+ a: ['rx', 'ry', 'xAxisRotation', 'largeArcFlag', 'sweepFlag', 'x', 'y'],
+ z: [],
+};
+
+export type Command = {
+ x2?: number | undefined;
+ y2?: number | undefined;
+ x1?: number | undefined;
+ y1?: number | undefined;
+ x?: number;
+ y?: number;
+ xAxisRotate?: number;
+ largeArcFlag?: boolean;
+ sweepFlag?: boolean;
+ type: keyof typeof typeMap;
+};
+
+/**
+ * Convert segments represented as points back into a command object
+ *
+ * @param {Number[][]} points Array of [x,y] points: [start, control1, control2, ..., end]
+ * Represents a segment
+ * @return {Object} A command object representing the segment.
+ */
+function pointsToCommand(points: number[][]): Command {
+ let x2: number | undefined;
+ let y2: number | undefined;
+ let x1: number | undefined;
+ let y1: number | undefined;
+
+ if (points.length === 4) {
+ x2 = points[2][0];
+ y2 = points[2][1];
+ }
+
+ if (points.length >= 3) {
+ x1 = points[1][0];
+ y1 = points[1][1];
+ }
+
+ const x = points[points.length - 1][0];
+ const y = points[points.length - 1][1];
+
+ let type: 'C' | 'Q' | 'L' = 'L';
+
+ if (points.length === 4) {
+ // start, control1, control2, end
+ type = 'C';
+ } else if (points.length === 3) {
+ // start, control, end
+ type = 'Q';
+ }
+
+ return {x2, y2, x1, y1, x, y, type};
+}
+
+/**
+ * Runs de Casteljau's algorithm enough times to produce the desired number of segments.
+ *
+ * @param {Number[][]} points Array of [x,y] points for de Casteljau (the initial segment to split)
+ * @param {Number} segmentCount Number of segments to split the original into
+ * @return {Number[][][]} Array of segments
+ */
+function splitCurveAsPoints(
+ points: number[][],
+ segmentCount: number
+): number[][][] {
+ segmentCount = segmentCount || 2;
+
+ const segments = [];
+ let remainingCurve = points;
+ const tIncrement = 1 / segmentCount;
+
+ // x-----x-----x-----x
+ // t= 0.33 0.66 1
+ // x-----o-----------x
+ // r= 0.33
+ // x-----o-----x
+ // r= 0.5 (0.33 / (1 - 0.33)) === tIncrement / (1 - (tIncrement * (i - 1))
+
+ // x-----x-----x-----x----x
+ // t= 0.25 0.5 0.75 1
+ // x-----o----------------x
+ // r= 0.25
+ // x-----o----------x
+ // r= 0.33 (0.25 / (1 - 0.25))
+ // x-----o----x
+ // r= 0.5 (0.25 / (1 - 0.5))
+
+ for (let i = 0; i < segmentCount - 1; i++) {
+ const tRelative = tIncrement / (1 - tIncrement * i);
+ const split = decasteljau(remainingCurve, tRelative);
+ segments.push(split.left);
+ remainingCurve = split.right;
+ }
+
+ // last segment is just to the end from the last point
+ segments.push(remainingCurve);
+
+ return segments;
+}
+
+/**
+ * Convert command objects to arrays of points, run de Casteljau's algorithm on it
+ * to split into to the desired number of segments.
+ *
+ * @param {Object} commandStart The start command object
+ * @param {Object} commandEnd The end command object
+ * @param {Number} segmentCount The number of segments to create
+ * @return {Object[]} An array of commands representing the segments in sequence
+ */
+export const splitCurve = (
+ commandStart: Command,
+ commandEnd: Command,
+ segmentCount: number
+): Command[] => {
+ const points = [[commandStart.x, commandStart.y]] as number[][];
+ if (commandEnd.x1 !== null && commandEnd.x1 !== undefined) {
+ points.push([commandEnd.x1, commandEnd.y1 as number]);
+ }
+
+ if (commandEnd.x2 !== null && commandEnd.x2 !== undefined) {
+ points.push([commandEnd.x2, commandEnd.y2 as number]);
+ }
+
+ points.push([commandEnd.x as number, commandEnd.y as number]);
+
+ return splitCurveAsPoints(points, segmentCount).map(pointsToCommand);
+};
diff --git a/packages/paths/src/interpolate-path.ts b/packages/paths/src/interpolate-path.ts
new file mode 100644
index 0000000000..fa46341ae3
--- /dev/null
+++ b/packages/paths/src/interpolate-path.ts
@@ -0,0 +1,429 @@
+import type {Command} from './helpers/split-curve';
+import {splitCurve, typeMap} from './helpers/split-curve';
+
+const commandTokenRegex = /[MLCSTQAHVZmlcstqahv]|-?[\d.e+-]+/g;
+
+function arrayOfLength<T>(length: number, value: T): T[] {
+ const array = Array(length);
+ for (let i = 0; i < length; i++) {
+ array[i] = value;
+ }
+
+ return array;
+}
+
+/**
+ * Converts a command object to a string to be used in a `d` attribute
+ * @param {Object} command A command object
+ * @return {String} The string for the `d` attribute
+ */
+function commandToString(command: Command) {
+ return `${command.type}${typeMap[command.type]
+ .map((p) => command[p as keyof Command])
+ .join(',')}`;
+}
+
+/**
+ * Converts command A to have the same type as command B.
+ *
+ * e.g., L0,5 -> C0,5,0,5,0,5
+ *
+ * Uses these rules:
+ * x1 <- x
+ * x2 <- x
+ * y1 <- y
+ * y2 <- y
+ * rx <- 0
+ * ry <- 0
+ * xAxisRotation <- read from B
+ * largeArcFlag <- read from B
+ * sweepflag <- read from B
+ *
+ * @param {Object} aCommand Command object from path `d` attribute
+ * @param {Object} bCommand Command object from path `d` attribute to match against
+ * @return {Object} aCommand converted to type of bCommand
+ */
+function convertToSameType(aCommand: Command, bCommand: Command) {
+ const conversionMap = {
+ x1: 'x',
+ y1: 'y',
+ x2: 'x',
+ y2: 'y',
+ } as const;
+
+ const readFromBKeys = ['xAxisRotation', 'largeArcFlag', 'sweepFlag'];
+
+ // convert (but ignore M types)
+ if (aCommand.type !== bCommand.type && bCommand.type.toUpperCase() !== 'M') {
+ const aConverted: Command = {
+ type: bCommand.type,
+ };
+ Object.keys(bCommand).forEach((bKey) => {
+ const bValue = bCommand[bKey as keyof Command];
+ // first read from the A command
+ let aValue = aCommand[bKey as keyof Command];
+
+ // if it is one of these values, read from B no matter what
+ if (aValue === undefined) {
+ if (readFromBKeys.includes(bKey)) {
+ aValue = bValue;
+ } else {
+ // if it wasn't in the A command, see if an equivalent was
+ if (
+ aValue === undefined &&
+ conversionMap[bKey as keyof typeof conversionMap]
+ ) {
+ aValue =
+ aCommand[conversionMap[bKey as keyof typeof conversionMap]];
+ }
+
+ // if it doesn't have a converted value, use 0
+ if (aValue === undefined) {
+ aValue = 0;
+ }
+ }
+ }
+
+ // @ts-expect-error
+ aConverted[bKey as keyof Command] = aValue;
+ });
+
+ // update the type to match B
+ aConverted.type = bCommand.type;
+ aCommand = aConverted;
+ }
+
+ return aCommand;
+}
+
+/**
+ * Interpolate between command objects commandStart and commandEnd segmentCount times.
+ * If the types are L, Q, or C then the curves are split as per de Casteljau's algorithm.
+ * Otherwise we just copy commandStart segmentCount - 1 times, finally ending with commandEnd.
+ *
+ * @param {Object} commandStart Command object at the beginning of the segment
+ * @param {Object} commandEnd Command object at the end of the segment
+ * @param {Number} segmentCount The number of segments to split this into. If only 1
+ * Then [commandEnd] is returned.
+ * @return {Object[]} Array of ~segmentCount command objects between commandStart and
+ * commandEnd. (Can be segmentCount+1 objects if commandStart is type M).
+ */
+function splitSegment(
+ commandStart: Command,
+ commandEnd: Command,
+ segmentCount: number
+) {
+ let segments: Command[] = [];
+
+ // line, quadratic bezier, or cubic bezier
+ if (
+ commandEnd.type === 'L' ||
+ commandEnd.type === 'Q' ||
+ commandEnd.type === 'C'
+ ) {
+ segments = segments.concat(
+ splitCurve(commandStart, commandEnd, segmentCount)
+ );
+
+ // general case - just copy the same point
+ } else {
+ const copyCommand = {...commandStart};
+
+ // convert M to L
+ if (copyCommand.type === 'M') {
+ copyCommand.type = 'L';
+ }
+
+ segments = segments.concat(
+ arrayOfLength(segmentCount - 1, undefined).map(() => copyCommand)
+ );
+ segments.push(commandEnd);
+ }
+
+ return segments;
+}
+
+/**
+ * Extends an array of commandsToExtend to the length of the referenceCommands by
+ * splitting segments until the number of commands match. Ensures all the actual
+ * points of commandsToExtend are in the extended array.
+ *
+ * @param {Object[]} commandsToExtend The command object array to extend
+ * @param {Object[]} referenceCommands The command object array to match in length
+ * @return {Object[]} The extended commandsToExtend array
+ */
+function extend(commandsToExtend: Command[], referenceCommands: Command[]) {
+ // compute insertion points:
+ // number of segments in the path to extend
+ const numSegmentsToExtend = commandsToExtend.length - 1;
+
+ // number of segments in the reference path.
+ const numReferenceSegments = referenceCommands.length - 1;
+
+ // this value is always between [0, 1].
+ const segmentRatio = numSegmentsToExtend / numReferenceSegments;
+
+ // create a map, mapping segments in referenceCommands to how many points
+ // should be added in that segment (should always be >= 1 since we need each
+ // point itself).
+ // 0 = segment 0-1, 1 = segment 1-2, n-1 = last vertex
+ const countPointsPerSegment = arrayOfLength<undefined>(
+ numReferenceSegments,
+ undefined
+ ).reduce((accum, _d, i) => {
+ const insertIndex = Math.floor(segmentRatio * i);
+
+ accum[insertIndex] = (accum[insertIndex] || 0) + 1;
+
+ return accum;
+ }, [] as (undefined | number)[]);
+
+ // extend each segment to have the correct number of points for a smooth interpolation
+ const extended = countPointsPerSegment.reduce(
+ (_extended, segmentCount, i) => {
+ // if last command, just add `segmentCount` number of times
+ if (i === commandsToExtend.length - 1) {
+ const lastCommandCopies = arrayOfLength(segmentCount as number, {
+ ...commandsToExtend[commandsToExtend.length - 1],
+ });
+
+ // convert M to L
+ if (lastCommandCopies[0].type === 'M') {
+ lastCommandCopies.forEach((d) => {
+ d.type = 'L';
+ });
+ }
+
+ return _extended.concat(lastCommandCopies);
+ }
+
+ // otherwise, split the segment segmentCount times.
+ return _extended.concat(
+ splitSegment(
+ commandsToExtend[i],
+ commandsToExtend[i + 1],
+ segmentCount as number
+ )
+ );
+ },
+ [] as Command[]
+ );
+
+ // add in the very first point since splitSegment only adds in the ones after it
+ extended.unshift(commandsToExtend[0]);
+
+ return extended;
+}
+
+/**
+ * Takes a path `d` string and converts it into an array of command
+ * objects. Drops the `Z` character.
+ *
+ * @param {String|null} d A path `d` string
+ */
+function pathCommandsFromString(d: string | null) {
+ // split into valid tokens
+ const tokens = (d || '').match(commandTokenRegex) || [];
+ const commands = [];
+ let commandArgs: string[];
+ let command: Command;
+
+ // iterate over each token, checking if we are at a new command
+ // by presence in the typeMap
+ for (let i = 0; i < tokens.length; ++i) {
+ commandArgs = typeMap[tokens[i] as keyof typeof typeMap];
+
+ // new command found:
+ if (commandArgs) {
+ command = {
+ type: tokens[i] as Command['type'],
+ };
+
+ // add each of the expected args for this command:
+ for (let a = 0; a < commandArgs.length; ++a) {
+ // @ts-expect-error
+ command[commandArgs[a] as keyof Command] = Number(tokens[i + a + 1]);
+ }
+
+ // need to increment our token index appropriately since
+ // we consumed token args
+ i += commandArgs.length;
+
+ commands.push(command);
+ }
+ }
+
+ return commands;
+}
+
+/**
+ * Interpolate from A to B by extending A and B during interpolation to have
+ * the same number of points. This allows for a smooth transition when they
+ * have a different number of points.
+ *
+ * Ignores the `Z` command in paths unless both A and B end with it.
+ *
+ * This function works directly with arrays of command objects instead of with
+ * path `d` strings (see interpolatePath for working with `d` strings).
+ *
+ * @param {Object[]} aCommandsInput Array of path commands
+ * @param {Object[]} bCommandsInput Array of path commands
+ * @returns {Function} Interpolation function that maps t ([0, 1]) to an array of path commands.
+ */
+function interpolatePathCommands(
+ aCommandsInput: Command[],
+ bCommandsInput: Command[]
+) {
+ // make a copy so we don't mess with the input arrays
+ let aCommands =
+ aCommandsInput === null || aCommandsInput === undefined
+ ? []
+ : aCommandsInput.slice();
+ let bCommands =
+ bCommandsInput === null || bCommandsInput === undefined
+ ? []
+ : bCommandsInput.slice();
+
+ // both input sets are empty, so we don't interpolate
+ if (!aCommands.length && !bCommands.length) {
+ return function () {
+ return [];
+ };
+ }
+
+ // do we add Z during interpolation? yes if both have it. (we'd expect both to have it or not)
+ const addZ =
+ (aCommands.length === 0 || aCommands[aCommands.length - 1].type === 'Z') &&
+ (bCommands.length === 0 || bCommands[bCommands.length - 1].type === 'Z');
+
+ // we temporarily remove Z
+ if (aCommands.length > 0 && aCommands[aCommands.length - 1].type === 'Z') {
+ aCommands.pop();
+ }
+
+ if (bCommands.length > 0 && bCommands[bCommands.length - 1].type === 'Z') {
+ bCommands.pop();
+ }
+
+ // if A is empty, treat it as if it used to contain just the first point
+ // of B. This makes it so the line extends out of from that first point.
+ if (!aCommands.length) {
+ aCommands.push(bCommands[0]);
+
+ // otherwise if B is empty, treat it as if it contains the first point
+ // of A. This makes it so the line retracts into the first point.
+ } else if (!bCommands.length) {
+ bCommands.push(aCommands[0]);
+ }
+
+ // extend to match equal size
+ const numPointsToExtend = Math.abs(bCommands.length - aCommands.length);
+
+ if (numPointsToExtend !== 0) {
+ // B has more points than A, so add points to A before interpolating
+ if (bCommands.length > aCommands.length) {
+ aCommands = extend(aCommands, bCommands);
+
+ // else if A has more points than B, add more points to B
+ } else if (bCommands.length < aCommands.length) {
+ bCommands = extend(bCommands, aCommands);
+ }
+ }
+
+ // commands have same length now.
+ // convert commands in A to the same type as those in B
+ aCommands = aCommands.map((aCommand, i) =>
+ convertToSameType(aCommand, bCommands[i])
+ );
+
+ // create mutable interpolated command objects
+ const interpolatedCommands = aCommands.map((aCommand) => ({...aCommand}));
+
+ if (addZ) {
+ interpolatedCommands.push({type: 'Z'});
+ aCommands.push({type: 'Z'}); // required for when returning at t == 0
+ }
+
+ return function (t: number) {
+ // at 1 return the final value without the extensions used during interpolation
+ if (t === 1) {
+ return bCommandsInput === null || bCommandsInput === undefined
+ ? []
+ : bCommandsInput;
+ }
+
+ // work with aCommands directly since interpolatedCommands are mutated
+ if (t === 0) {
+ return aCommands;
+ }
+
+ // interpolate the commands using the mutable interpolated command objs
+ for (let i = 0; i < interpolatedCommands.length; ++i) {
+ // if (interpolatedCommands[i].type === 'Z') continue;
+
+ const aCommand = aCommands[i];
+ const bCommand = bCommands[i];
+ const interpolatedCommand = interpolatedCommands[i];
+ for (const arg of typeMap[
+ interpolatedCommand.type as keyof typeof typeMap
+ ]) {
+ // @ts-expect-error
+ interpolatedCommand[arg] =
+ (1 - t) * (aCommand[arg as keyof Command] as number) +
+ t * (bCommand[arg as keyof Command] as number);
+
+ // do not use floats for flags (#27), round to integer
+ if (arg === 'largeArcFlag' || arg === 'sweepFlag') {
+ // @ts-expect-error
+ interpolatedCommand[arg] = Math.round(interpolatedCommand[arg]);
+ }
+ }
+ }
+
+ return interpolatedCommands;
+ };
+}
+
+/**
+ * Interpolate from A to B by extending A and B during interpolation to have
+ * the same number of points. This allows for a smooth transition when they
+ * have a different number of points.
+ *
+ * Ignores the `Z` character in paths unless both A and B end with it.
+ *
+ * @param {String} a The `d` attribute for a path
+ * @param {String} b The `d` attribute for a path
+ * @returns {Function} Interpolation function that maps t ([0, 1]) to a path `d` string.
+ */
+export const interpolatePath = (value: number, a: string, b: string) => {
+ // at 1 return the final value without the extensions used during interpolation
+ if (value === 1) {
+ return b;
+ }
+
+ if (value === 0) {
+ return a;
+ }
+
+ const aCommands = pathCommandsFromString(a);
+ if (aCommands.length === 0) {
+ throw new TypeError(`SVG Path "${a}" is not valid`);
+ }
+
+ const bCommands = pathCommandsFromString(b);
+ if (bCommands.length === 0) {
+ throw new TypeError(`SVG Path "${b}" is not valid`);
+ }
+
+ const commandInterpolator = interpolatePathCommands(aCommands, bCommands);
+
+ const interpolatedCommands = commandInterpolator(value);
+
+ // convert to a string (fastest concat: https://jsperf.com/join-concat/150)
+ let interpolatedString = '';
+ for (const interpolatedCommand of interpolatedCommands) {
+ interpolatedString += commandToString(interpolatedCommand);
+ }
+
+ return interpolatedString;
+};
diff --git a/packages/paths/src/test/interpolate-path.test.ts b/packages/paths/src/test/interpolate-path.test.ts
new file mode 100644
index 0000000000..a0365b2598
--- /dev/null
+++ b/packages/paths/src/test/interpolate-path.test.ts
@@ -0,0 +1,14 @@
+import {expect, test} from 'vitest';
+import {interpolatePath} from '../interpolate-path';
+
+test('Should be able to interpolate path', () => {
+ expect(interpolatePath(0.5, 'M 0 0 L 100 0', 'M 100 0 L 0 0')).toBe(
+ 'M50,0L50,0'
+ );
+ expect(interpolatePath(0, 'M 0 0 L 100 0', 'M 100 0 L 0 0')).toBe(
+ 'M 0 0 L 100 0'
+ );
+ expect(interpolatePath(1, 'M 0 0 L 100 0', 'M 100 0 L 0 0')).toBe(
+ 'M 100 0 L 0 0'
+ );
+});