🎥 Make videos programmatically with React
interpolation of same kind
Jonny Burger83c3a1581c4b
diff --git a/packages/paths/src/helpers/types.ts b/packages/paths/src/helpers/types.tsindex 8784b88dfa..16af387093 100644--- a/packages/paths/src/helpers/types.ts+++ b/packages/paths/src/helpers/types.ts@@ -51,6 +51,12 @@ export type CInstruction = {y: number;};+export type MInstruction = {+ type: 'M';+ x: number;+ y: number;+};+export type QInstruction = {type: 'Q';cpx: number;@@ -59,18 +65,16 @@ export type QInstruction = {y: number;};+export type ZInstruction = {+ type: 'Z';+};+export type ReducedInstruction =- | {- type: 'M';- x: number;- y: number;- }+ | MInstruction| LInstruction| CInstruction| QInstruction- | {- type: 'Z';- };+ | ZInstruction;export type AbsoluteInstruction =| ReducedInstructiondiff --git a/packages/paths/src/interpolate-path/convert-to-same-instruction-type.ts b/packages/paths/src/interpolate-path/convert-to-same-instruction-type.tsindex 7095afd6fb..17042b061b 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@@ -97,7 +97,7 @@ const convertToQCommand = (command: ReducedInstruction): QInstruction => {* @param {Object} bCommand Command object from path `d` attribute to match against* @return {Object} aCommand converted to type of bCommand*/-export function convertToSameType(+export function convertToSameInstructionType(aCommand: ReducedInstruction,bCommand: ReducedInstruction,): ReducedInstruction {diff --git a/packages/paths/src/interpolate-path/interpolate-instruction-of-same-kind.ts b/packages/paths/src/interpolate-path/interpolate-instruction-of-same-kind.tsnew file mode 100644index 0000000000..0d5dccae35--- /dev/null+++ b/packages/paths/src/interpolate-path/interpolate-instruction-of-same-kind.ts@@ -0,0 +1,112 @@+import type {+ CInstruction,+ LInstruction,+ MInstruction,+ QInstruction,+ ReducedInstruction,+ ZInstruction,+} from '../helpers/types';++const interpolateLInstruction = (+ t: number,+ first: LInstruction,+ second: LInstruction,+): LInstruction => {+ return {+ type: 'L',+ x: (1 - t) * first.x + t * second.x,+ y: (1 - t) * first.y + t * second.y,+ };+};++const interpolateCInstructions = (+ t: number,+ first: CInstruction,+ second: CInstruction,+): CInstruction => {+ return {+ type: 'C',+ cp1x: (1 - t) * first.cp1x + t * second.cp1x,+ cp2x: (1 - t) * first.cp2x + t * second.cp2x,+ cp1y: (1 - t) * first.cp1y + t * second.cp1y,+ cp2y: (1 - t) * first.cp2y + t * second.cp2y,+ x: (1 - t) * first.x + t * second.x,+ y: (1 - t) * first.y + t * second.y,+ };+};++const interpolateQInstructions = (+ t: number,+ first: QInstruction,+ second: QInstruction,+): QInstruction => {+ return {+ type: 'Q',+ cpx: (1 - t) * first.cpx + t * second.cpx,+ cpy: (1 - t) * first.cpy + t * second.cpy,+ x: (1 - t) * first.x + t * second.x,+ y: (1 - t) * first.y + t * second.y,+ };+};++const interpolateMInstructions = (+ t: number,+ first: MInstruction,+ second: MInstruction,+): MInstruction => {+ return {+ type: 'M',+ x: (1 - t) * first.x + t * second.x,+ y: (1 - t) * first.y + t * second.y,+ };+};++export const interpolateInstructionOfSameKind = (+ t: number,+ first: ReducedInstruction,+ second: ReducedInstruction,+) => {+ if (first.type === 'L') {+ if (second.type !== 'L') {+ throw new Error('mismatch');+ }++ return interpolateLInstruction(t, first, second);+ }++ if (first.type === 'C') {+ if (second.type !== 'C') {+ throw new Error('mismatch');+ }++ return interpolateCInstructions(t, first, second);+ }++ if (first.type === 'Q') {+ if (second.type !== 'Q') {+ throw new Error('mismatch');+ }++ return interpolateQInstructions(t, first, second);+ }++ if (first.type === 'M') {+ if (second.type !== 'M') {+ throw new Error('mismatch');+ }++ return interpolateMInstructions(t, first, second);+ }++ if (first.type === 'Z') {+ if (second.type !== 'Z') {+ throw new Error('mismatch');+ }++ return {+ type: 'Z',+ } as ZInstruction;+ }++ throw new Error('mismatch');+};diff --git a/packages/paths/src/interpolate-path/interpolate-instructions.ts b/packages/paths/src/interpolate-path/interpolate-instructions.tsnew file mode 100644index 0000000000..73639faa07--- /dev/null+++ b/packages/paths/src/interpolate-path/interpolate-instructions.ts@@ -0,0 +1,123 @@+import type {ReducedInstruction} from '../helpers/types';+import {convertToSameInstructionType} from './convert-to-same-instruction-type';+import {extendInstruction} from './extend-command';+import {interpolateInstructionOfSameKind} from './interpolate-instruction-of-same-kind';++/**+ * 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.+ */+export function interpolateInstructions(+ aCommandsInput: ReducedInstruction[],+ bCommandsInput: ReducedInstruction[],+) {+ // 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 = extendInstruction(aCommands, bCommands);++ // else if A has more points than B, add more points to B+ } else if (bCommands.length < aCommands.length) {+ bCommands = extendInstruction(bCommands, aCommands);+ }+ }++ // commands have same length now.+ // convert commands in A to the same type as those in B+ aCommands = aCommands.map((aCommand, i) =>+ convertToSameInstructionType(aCommand, bCommands[i]),+ );++ // create mutable interpolated command objects+ const interpolatedCommands: ReducedInstruction[] = 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) {+ const aCommand = aCommands[i];+ const bCommand = bCommands[i];+ const interpolatedCommand = interpolateInstructionOfSameKind(+ t,+ aCommand,+ bCommand,+ );+ interpolatedCommands.push(interpolatedCommand);+ }++ return interpolatedCommands;+ };+}