Skip to content

🎥 Make videos programmatically with React

fixes of the use of translate path

Alex Fernandez1d7add7bd079
diff --git a/packages/docs/components/TableOfContents/paths.tsx b/packages/docs/components/TableOfContents/paths.tsx
index 2ee762c46a..b2f4a2f8f3 100644
--- a/packages/docs/components/TableOfContents/paths.tsx
+++ b/packages/docs/components/TableOfContents/paths.tsx
@@ -55,6 +55,10 @@ export const TableOfContents: React.FC = () => {
<strong>getSubpaths()</strong>
<div>Split SVG path into its parts</div>
</TOCItem>
+ <TOCItem link="/docs/paths/translate-path">
+ <strong>translatePath()</strong>
+ <div>Translates the position of an path against X/Y coordinates</div>
+ </TOCItem>
</Grid>
</div>
);
diff --git a/packages/docs/docs/paths/translate-path.md b/packages/docs/docs/paths/translate-path.md
new file mode 100644
index 0000000000..f9f3d01674
--- /dev/null
+++ b/packages/docs/docs/paths/translate-path.md
@@ -0,0 +1,57 @@
+---
+title: translatePath()
+crumb: "@remotion/paths"
+---
+
+_Part of the [`@remotion/paths`](/docs/paths) package._
+
+Translates the X or Y coordinates of the path
+
+ The function takes three arguments:
+
+- `path`, string SVG paths coordinates.
+- `x`, a number representing path x coordinate
+- `y`, `optional` a number representing path y coordinate
+
+
+Returns a SVG path string if the path is valid:
+
+```tsx twoslash title='Translate path x coordinates'
+import { translatePath } from "@remotion/paths";
+
+const translatedPath = translatePath("M 50 50 L 150 50", 10);
+console.log(translatedPath); // "M 50 50 L 150 50"
+```
+
+
+```tsx twoslash title='Translate path x and y coordinates'
+import { translatePath } from "@remotion/paths";
+
+const translatedPath = translatePath('M10 10 L15 15', 10, 10);
+console.log(translatedPath); // "M 20,20 L 25,25"
+```
+
+
+```tsx twoslash title='Translate circle path x and y coordinates'
+import { translatePath } from "@remotion/paths";
+
+const translatedPath = translatePath('M 35,50 a 25,25,0,1,1,50,0 a 25,25,0,1,1,-50,0', 10, 20);
+console.log(translatedPath); // "M 45,70 a 25,25,0,1,1,50,0 a 25,25,0,1,1,-50,0"
+```
+
+The function will throw if the path is invalid:
+
+```tsx twoslash
+import { translatePath } from "@remotion/paths";
+// ---cut---
+translatePath("remotion", 10); // Malformed path data: "m" ...
+```
+
+## Credits
+
+Source code stems mostly from [translate-svg-path](https://github.com/michaelrhodes/translate-svg-path) and [serialize-svg-path](https://github.com/jkroso/serialize-svg-path).
+
+## See also
+
+- [`@remotion/paths`](/docs/paths)
+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/paths/src/translate-path.ts)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 5a28217aca..66b8da77b4 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -262,6 +262,7 @@ module.exports = {
"paths/evolve-path",
"paths/extend-viewbox",
"paths/get-subpaths",
+ "paths/translate-path",
],
},
{
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
index 9a60dd210e..0806f47db4 100644
--- a/packages/paths/src/index.ts
+++ b/packages/paths/src/index.ts
@@ -9,3 +9,4 @@ export {Part} from './helpers/types';
export {interpolatePath} from './interpolate-path';
export {normalizePath} from './normalize-path';
export {reversePath} from './reverse-path';
+export {translatePath} from './translate-path';
diff --git a/packages/paths/src/parse-path.ts b/packages/paths/src/parse-path.ts
deleted file mode 100644
index bf6c2ae288..0000000000
--- a/packages/paths/src/parse-path.ts
+++ /dev/null
@@ -1,52 +0,0 @@
-// 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
deleted file mode 100644
index 932bf5f212..0000000000
--- a/packages/paths/src/test/parse-path.test.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-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/test/translate-path.test.ts b/packages/paths/src/test/translate-path.test.ts
new file mode 100644
index 0000000000..134fe1ce05
--- /dev/null
+++ b/packages/paths/src/test/translate-path.test.ts
@@ -0,0 +1,45 @@
+import {expect, test} from 'vitest';
+import {translatePath} from '../translate-path';
+
+test('Should be able to translate simple path, update x', () => {
+ const path = translatePath('M10 10 L15 15', 10);
+
+ expect(path).toEqual('M 20,10 L 25,15');
+});
+
+test('Should be able to translate simple path, update x and y', () => {
+ const path = translatePath('M10 10 L15 15', 10, 10);
+
+ expect(path).toEqual('M 20,20 L 25,25');
+});
+
+test('Should be able to translate circle path, update x', () => {
+ const path = translatePath(
+ 'M 35,50 a 25,25,0,1,1,50,0 a 25,25,0,1,1,-50,0',
+ 10
+ );
+
+ expect(path).toEqual('M 45,50 a 25,25,0,1,1,50,0 a 25,25,0,1,1,-50,0');
+});
+
+test('Should be able to translate circle path, update x and y', () => {
+ const path = translatePath(
+ 'M 35,50 a 25,25,0,1,1,50,0 a 25,25,0,1,1,-50,0',
+ 10,
+ 20
+ );
+
+ expect(path).toEqual('M 45,70 a 25,25,0,1,1,50,0 a 25,25,0,1,1,-50,0');
+});
+
+test('Should be able to translate example path, update x', () => {
+ const path = translatePath('M 50 50 L 150 50', 10);
+
+ expect(path).toEqual('M 60,50 L 160,50');
+});
+
+test('Translation should throw error', () => {
+ expect(() => {
+ translatePath('remotion', 10);
+ }).toThrow('Malformed path data: "m" must have 2 elements and has 0: mo');
+});
diff --git a/packages/paths/src/translate-path.ts b/packages/paths/src/translate-path.ts
index d686b05ec7..122fae9a0c 100644
--- a/packages/paths/src/translate-path.ts
+++ b/packages/paths/src/translate-path.ts
@@ -1,11 +1,18 @@
// Copied partially from https://github.com/michaelrhodes/translate-svg-path/blob/master/index.js
+import type {Instruction} from './helpers/parse';
+import {parsePath} from './helpers/parse';
-export const translatePath = (segments: string[][], x: number, y?: number) => {
- // y is optional
- y = y || 0;
+const serialize = (path: (Instruction | number[])[]) => {
+ return path.reduce((str: string, seg: Instruction | number[]) => {
+ return (str + ' ' + seg[0] + ' ' + seg.slice(1).join(',')).trim();
+ }, '');
+};
+
+const translateSegments = (path: string, x: number, y?: number) => {
+ const segments = parsePath(path);
return segments.map((segment) => {
- const cmd = segment[0];
+ const cmd = segment[0] as string;
// Shift coords only for commands with absolute values
if ('ACHLMRQSTVZ'.indexOf(cmd) === -1) {
@@ -16,25 +23,29 @@ export const translatePath = (segments: string[][], x: number, y?: number) => {
// V is the only command, with shifted coords parity
if (name === 'v') {
- segment[1] += y;
+ segment[1] = (segment[1] as number) + (y as number);
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;
+ segment[6] = (segment[6] as number) + x;
+ segment[7] = (segment[7] as number) + (y ?? 0);
return segment;
}
// All other commands have [cmd, x1, y1, x2, y2, x3, y3, ...] format
return segment.map((val, i) => {
if (!i) {
- return val;
+ return val as number;
}
- return i % 2 ? val + x : val + y;
+ return i % 2 ? (val as number) + x : (val as number) + (y ?? 0);
});
});
};
+
+export const translatePath = (path: string, x: number, y?: number) => {
+ return serialize(translateSegments(path, x, y));
+};