Skip to content

🎥 Make videos programmatically with React

draft changes

Alex Fernandeza4b8a3c177b1
diff --git a/packages/paths/src/parse-path.ts b/packages/paths/src/parse-path.ts
new file mode 100644
index 0000000000..bf6c2ae288
--- /dev/null
+++ b/packages/paths/src/parse-path.ts
@@ -0,0 +1,52 @@
+// Copied partially from https://github.com/jkroso/parse-svg-path/blob/master/index.js
+const length = {
+ a: 7,
+ c: 6,
+ h: 1,
+ l: 2,
+ m: 2,
+ q: 4,
+ s: 4,
+ t: 2,
+ v: 1,
+ z: 0,
+} as const;
+const segment = /([astvzqmhlc])([^astvzqmhlc]*)/gi;
+const number = /-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/gi;
+
+export const parsePath = (path: string) => {
+ const data: number[][] = [];
+ path.replace(segment, (_, command, argnts) => {
+ let type = command.toLowerCase();
+ const dataArgs = parseValues(argnts);
+
+ // overloaded moveTo
+ if (type === 'm' && dataArgs.length > 2) {
+ data.push([command].concat(dataArgs.splice(0, 2)));
+ type = 'l';
+ command = command === 'm' ? 'l' : 'L';
+ }
+
+ let i = 0;
+ while (i < dataArgs.length) {
+ const key = type as keyof typeof length;
+ if (dataArgs.length === length[key]) {
+ dataArgs.unshift(command);
+ data.push(dataArgs);
+ break;
+ }
+
+ if (dataArgs.length < length[key]) throw new Error('malformed path data');
+ data.push([command].concat(dataArgs.splice(0, length[key])));
+ i++;
+ }
+
+ return '';
+ });
+ return data;
+};
+
+const parseValues = (args: string) => {
+ const numbers = args.match(number);
+ return numbers ? numbers.map(Number) : [];
+};
diff --git a/packages/paths/src/test/parse-path.test.ts b/packages/paths/src/test/parse-path.test.ts
new file mode 100644
index 0000000000..932bf5f212
--- /dev/null
+++ b/packages/paths/src/test/parse-path.test.ts
@@ -0,0 +1,16 @@
+import {expect, test} from 'vitest';
+import {parsePath} from '../parse-path';
+import {translatePath} from '../translate-path';
+test('Should be able to parse path', () => {
+ const data = parsePath('M10 10 L15 15');
+
+ expect(data).toEqual([
+ ['M', 10, 10],
+ ['L', 15, 15],
+ ]);
+
+ expect(translatePath(data, 10)).toEqual([
+ ['M', 20, 10],
+ ['L', 25, 15],
+ ]);
+});
diff --git a/packages/paths/src/translate-path.ts b/packages/paths/src/translate-path.ts
new file mode 100644
index 0000000000..d686b05ec7
--- /dev/null
+++ b/packages/paths/src/translate-path.ts
@@ -0,0 +1,40 @@
+// Copied partially from https://github.com/michaelrhodes/translate-svg-path/blob/master/index.js
+
+export const translatePath = (segments: string[][], x: number, y?: number) => {
+ // y is optional
+ y = y || 0;
+
+ return segments.map((segment) => {
+ const cmd = segment[0];
+
+ // Shift coords only for commands with absolute values
+ if ('ACHLMRQSTVZ'.indexOf(cmd) === -1) {
+ return segment;
+ }
+
+ const name = cmd.toLowerCase();
+
+ // V is the only command, with shifted coords parity
+ if (name === 'v') {
+ segment[1] += y;
+ return segment;
+ }
+
+ // ARC is: ['A', rx, ry, x-axis-rotation, large-arc-flag, sweep-flag, x, y]
+ // touch x, y only
+ if (name === 'a') {
+ segment[6] += x;
+ segment[7] += y;
+ return segment;
+ }
+
+ // All other commands have [cmd, x1, y1, x2, y2, x3, y3, ...] format
+ return segment.map((val, i) => {
+ if (!i) {
+ return val;
+ }
+
+ return i % 2 ? val + x : val + y;
+ });
+ });
+};