Skip to content

🎥 Make videos programmatically with React

split up interpolatePath()

Jonny Burgerbcc18e156557
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
index 36ec2242c0..eee8b447b1 100644
--- a/packages/paths/src/index.ts
+++ b/packages/paths/src/index.ts
@@ -15,7 +15,7 @@ export {
Part,
ReducedInstruction,
} from './helpers/types';
-export {interpolatePath} from './interpolate-path';
+export {interpolatePath} from './interpolate-path/interpolate-path';
export {normalizePath} from './normalize-path';
export {parsePath} from './parse-path';
export {reduceInstructions} from './reduce-instructions';
diff --git a/packages/paths/src/interpolate-path/command-to-string.ts b/packages/paths/src/interpolate-path/command-to-string.ts
new file mode 100644
index 0000000000..9d93120e3a
--- /dev/null
+++ b/packages/paths/src/interpolate-path/command-to-string.ts
@@ -0,0 +1,13 @@
+import type {Command} from './command';
+import {typeMap} from './command';
+
+/**
+ * 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
+ */
+export function commandToString(command: Command) {
+ return `${command.type} ${typeMap[command.type]
+ .map((p) => command[p as keyof Command])
+ .join(' ')}`;
+}
diff --git a/packages/paths/src/interpolate-path/command.ts b/packages/paths/src/interpolate-path/command.ts
new file mode 100644
index 0000000000..e79c48975d
--- /dev/null
+++ b/packages/paths/src/interpolate-path/command.ts
@@ -0,0 +1,37 @@
+/**
+ * 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;
+};
diff --git a/packages/paths/src/interpolate-path.ts b/packages/paths/src/interpolate-path/interpolate-path.ts
similarity index 96%
rename from packages/paths/src/interpolate-path.ts
rename to packages/paths/src/interpolate-path/interpolate-path.ts
index 2d97f79888..492f68fd31 100644
--- a/packages/paths/src/interpolate-path.ts
+++ b/packages/paths/src/interpolate-path/interpolate-path.ts
@@ -31,8 +31,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-import type {Command} from './helpers/split-curve';
-import {splitCurve, typeMap} from './helpers/split-curve';
+import {typeMap, type Command} from './command';
+import {commandToString} from './command-to-string';
+import {splitCurve} from './split-curve';
const commandTokenRegex = /[MLCSTQAHVZmlcstqahv]|-?[\d.e+-]+/g;
@@ -45,17 +46,6 @@ function arrayOfLength<T>(length: number, value: T): T[] {
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.
*
diff --git a/packages/paths/src/helpers/split-curve.ts b/packages/paths/src/interpolate-path/split-curve.ts
similarity index 87%
rename from packages/paths/src/helpers/split-curve.ts
rename to packages/paths/src/interpolate-path/split-curve.ts
index 356f7c8aea..1dccc39086 100644
--- a/packages/paths/src/helpers/split-curve.ts
+++ b/packages/paths/src/interpolate-path/split-curve.ts
@@ -31,6 +31,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+import type {Command} from './command';
+
/**
* de Casteljau's algorithm for drawing and splitting bezier curves.
* Inspired by https://pomax.github.io/bezierinfo/
@@ -81,45 +83,6 @@ function decasteljau(
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
*
diff --git a/packages/paths/src/test/interpolate-path.test.ts b/packages/paths/src/test/interpolate-path.test.ts
index f2084d5080..0200b7ef09 100644
--- a/packages/paths/src/test/interpolate-path.test.ts
+++ b/packages/paths/src/test/interpolate-path.test.ts
@@ -1,5 +1,5 @@
import {expect, test} from 'bun:test';
-import {interpolatePath} from '../interpolate-path';
+import {interpolatePath} from '../interpolate-path/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(