Skip to content

🎥 Make videos programmatically with React

resetPath() helpers

Jonny Burger40cc970bd20f
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
index 87acd99254..16fba980d5 100644
--- a/packages/paths/src/index.ts
+++ b/packages/paths/src/index.ts
@@ -9,5 +9,6 @@ export {getTangentAtLength} from './get-tangent-at-length';
export {BoundingBox, Part} from './helpers/types';
export {interpolatePath} from './interpolate-path';
export {normalizePath} from './normalize-path';
+export {resetPath} from './reset-path';
export {reversePath} from './reverse-path';
export {translatePath} from './translate-path';
diff --git a/packages/paths/src/reset-path.ts b/packages/paths/src/reset-path.ts
new file mode 100644
index 0000000000..7a69c7ef66
--- /dev/null
+++ b/packages/paths/src/reset-path.ts
@@ -0,0 +1,7 @@
+import {getBoundingBox} from './get-bounding-box';
+import {translatePath} from './translate-path';
+
+export const resetPath = (d: string) => {
+ const box = getBoundingBox(d);
+ return translatePath(d, -box.x1, -box.y1);
+};
diff --git a/packages/paths/src/test/reset-path.test.ts b/packages/paths/src/test/reset-path.test.ts
new file mode 100644
index 0000000000..7ae76cbf69
--- /dev/null
+++ b/packages/paths/src/test/reset-path.test.ts
@@ -0,0 +1,12 @@
+import {expect, test} from 'vitest';
+import {resetPath} from '../reset-path';
+
+test('resetPath()', () => {
+ expect(resetPath('M 10 10 L 20 20')).toBe('M 0 0 L 10 10');
+});
+
+test('resetPath() with arcs', () => {
+ expect(resetPath('M 35,50 a 25,25,0,1,1,50,0 a 25,25,0,1,1,-50,0')).toBe(
+ 'M 0 25.000000000000007 a 25 25 0 1 1 50 0 a 25 25 0 1 1 -50 0'
+ );
+});
diff --git a/packages/paths/src/translate-path.ts b/packages/paths/src/translate-path.ts
index c8468c8655..38824f399d 100644
--- a/packages/paths/src/translate-path.ts
+++ b/packages/paths/src/translate-path.ts
@@ -4,7 +4,7 @@ import {parsePath} from './helpers/parse';
const serialize = (path: (Instruction | number[])[]) => {
return path.reduce((str: string, seg: Instruction | number[]) => {
- return (str + ' ' + seg[0] + ' ' + seg.slice(1).join(',')).trim();
+ return (str + ' ' + seg[0] + ' ' + seg.slice(1).join(' ')).trim();
}, '');
};
diff --git a/packages/paths/src/unshort.ts b/packages/paths/src/unshort.ts
index 87b0276c3e..bc87fb17eb 100644
--- a/packages/paths/src/unshort.ts
+++ b/packages/paths/src/unshort.ts
@@ -25,27 +25,31 @@ export const unshort = function (segments: Instruction[]): Instruction[] {
case 'V': {
y = s[1];
- return [s];
+ newSegments[i] = s;
+ break;
}
case 'H': {
x = s[1];
- return [s];
+ newSegments[i] = s;
+ break;
}
case 'C': {
x = s[5];
y = s[6];
- return [s];
+ newSegments[i] = s;
+ break;
}
case 'Q': {
x = s[3];
y = s[4];
- return [s];
+ newSegments[i] = s;
+ break;
}
default: