Skip to content

🎥 Make videos programmatically with React

implement scalePath()

Jonny Burger3ce20e97b2b4
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
index 990825ab56..4bdb2d580b 100644
--- a/packages/paths/src/index.ts
+++ b/packages/paths/src/index.ts
@@ -19,6 +19,7 @@ export {parsePath} from './parse-path';
export {reduceInstructions} from './reduce-instructions';
export {resetPath} from './reset-path';
export {reversePath} from './reverse-path';
+export {scalePath} from './scale-path';
export {serializeInstructions} from './serialize-instructions';
export {translatePath} from './translate-path';
export {warpPath, WarpPathFn} from './warp-path';
diff --git a/packages/paths/src/scale-path.ts b/packages/paths/src/scale-path.ts
new file mode 100644
index 0000000000..d1de5db70c
--- /dev/null
+++ b/packages/paths/src/scale-path.ts
@@ -0,0 +1,193 @@
+import {getBoundingBoxFromInstructions} from './get-bounding-box';
+import type {Instruction} from './helpers/types';
+import {parsePath} from './parse-path';
+import {reduceInstructions} from './reduce-instructions';
+import {serializeInstructions} from './serialize-instructions';
+import {translateSegments} from './translate-path';
+
+export const scalePath = (d: string, scaleX: number, scaleY: number) => {
+ const reduced = reduceInstructions(parsePath(d));
+
+ const bounded = getBoundingBoxFromInstructions(reduced);
+ const zeroed = translateSegments(reduced, -bounded.x1, -bounded.y1);
+
+ const mapped = zeroed.map((instruction): Instruction => {
+ if (instruction.type === 'L') {
+ return {
+ type: 'L',
+ x: scaleX * instruction.x,
+ y: scaleY * instruction.y,
+ };
+ }
+
+ if (instruction.type === 'C') {
+ return {
+ type: 'C',
+ x: scaleX * instruction.x,
+ y: scaleY * instruction.y,
+ cp1x: scaleX * instruction.cp1x,
+ cp1y: scaleY * instruction.cp1y,
+ cp2x: scaleX * instruction.cp2x,
+ cp2y: scaleY * instruction.cp2y,
+ };
+ }
+
+ if (instruction.type === 'M') {
+ return {
+ type: 'M',
+ x: scaleX * instruction.x,
+ y: scaleY * instruction.y,
+ };
+ }
+
+ if (instruction.type === 'Q') {
+ return {
+ type: 'Q',
+ x: scaleX * instruction.x,
+ y: scaleY * instruction.y,
+ cpx: scaleX * instruction.cpx,
+ cpy: scaleY * instruction.cpy,
+ };
+ }
+
+ if (instruction.type === 'Z') {
+ return {
+ type: 'Z',
+ };
+ }
+
+ if (instruction.type === 'A') {
+ return {
+ type: 'A',
+ largeArcFlag: instruction.largeArcFlag,
+ rx: scaleX * instruction.rx,
+ ry: scaleY * instruction.ry,
+ sweepFlag: instruction.sweepFlag,
+ xAxisRotation: instruction.xAxisRotation,
+ x: scaleX * instruction.x,
+ y: scaleY * instruction.y,
+ };
+ }
+
+ if (instruction.type === 'H') {
+ return {
+ type: 'H',
+ x: scaleX * instruction.x,
+ };
+ }
+
+ if (instruction.type === 'S') {
+ return {
+ type: 'S',
+ cpx: scaleX * instruction.cpx,
+ cpy: scaleY * instruction.cpy,
+ x: scaleX * instruction.x,
+ y: scaleY * instruction.y,
+ };
+ }
+
+ if (instruction.type === 'T') {
+ return {
+ type: 'T',
+ x: scaleX * instruction.x,
+ y: scaleY * instruction.y,
+ };
+ }
+
+ if (instruction.type === 'V') {
+ return {
+ type: 'V',
+ y: scaleY * instruction.y,
+ };
+ }
+
+ if (instruction.type === 'a') {
+ return {
+ type: 'a',
+ dx: scaleX * instruction.dx,
+ dy: scaleY * instruction.dy,
+ largeArcFlag: instruction.largeArcFlag,
+ rx: scaleX * instruction.rx,
+ ry: scaleY * instruction.ry,
+ sweepFlag: instruction.sweepFlag,
+ xAxisRotation: instruction.xAxisRotation,
+ };
+ }
+
+ if (instruction.type === 'c') {
+ return {
+ type: 'c',
+ cp1dx: scaleX * instruction.cp1dx,
+ cp1dy: scaleY * instruction.cp1dy,
+ cp2dx: scaleX * instruction.cp2dx,
+ cp2dy: scaleY * instruction.cp2dy,
+ dx: scaleX * instruction.dx,
+ dy: scaleY * instruction.dy,
+ };
+ }
+
+ if (instruction.type === 'h') {
+ return {
+ type: 'h',
+ dx: scaleX * instruction.dx,
+ };
+ }
+
+ if (instruction.type === 'l') {
+ return {
+ type: 'l',
+ dx: scaleX * instruction.dx,
+ dy: scaleY * instruction.dy,
+ };
+ }
+
+ if (instruction.type === 'm') {
+ return {
+ type: 'm',
+ dx: scaleX * instruction.dx,
+ dy: scaleY * instruction.dy,
+ };
+ }
+
+ if (instruction.type === 'q') {
+ return {
+ type: 'q',
+ cpdx: scaleX * instruction.cpdx,
+ cpdy: scaleY * instruction.cpdy,
+ dx: scaleX * instruction.dx,
+ dy: scaleY * instruction.dy,
+ };
+ }
+
+ if (instruction.type === 's') {
+ return {
+ type: 's',
+ cpdx: scaleX * instruction.cpdx,
+ cpdy: scaleY * instruction.cpdy,
+ dx: scaleX * instruction.dx,
+ dy: scaleY * instruction.dy,
+ };
+ }
+
+ if (instruction.type === 't') {
+ return {
+ type: 't',
+ dx: scaleX * instruction.dx,
+ dy: scaleY * instruction.dy,
+ };
+ }
+
+ if (instruction.type === 'v') {
+ return {
+ type: 'v',
+ dy: scaleY * instruction.dy,
+ };
+ }
+
+ throw new Error('unexpected function');
+ });
+
+ return serializeInstructions(
+ translateSegments(mapped, bounded.x1, bounded.y1)
+ );
+};
diff --git a/packages/paths/src/translate-path.ts b/packages/paths/src/translate-path.ts
index fc20159a43..bc91046f51 100644
--- a/packages/paths/src/translate-path.ts
+++ b/packages/paths/src/translate-path.ts
@@ -3,7 +3,11 @@ import type {Instruction} from './helpers/types';
import {parsePath} from './parse-path';
import {serializeInstructions} from './serialize-instructions';
-const translateSegments = (segments: Instruction[], x: number, y: number) => {
+export const translateSegments = (
+ segments: Instruction[],
+ x: number,
+ y: number
+) => {
return segments.map((segment): Instruction => {
// Shift coords only for commands with absolute values
if (