Skip to content

🎥 Make videos programmatically with React

serialize shapes

Jonny Burger54e31d9a1289
diff --git a/packages/paths/src/get-subpaths.ts b/packages/paths/src/get-subpaths.ts
index 993009fa4e..6d4036328d 100644
--- a/packages/paths/src/get-subpaths.ts
+++ b/packages/paths/src/get-subpaths.ts
@@ -1,5 +1,5 @@
import {construct} from './helpers/construct';
-import {serializePath} from './helpers/serialize';
+import {serializeInstructions} from './helpers/serialize';
/**
* Splits a valid SVG path into it's parts.
@@ -10,6 +10,6 @@ export const getSubpaths = (path: string): string[] => {
const {segments} = construct(path);
return segments.map((seg) => {
- return serializePath(seg);
+ return serializeInstructions(seg);
});
};
diff --git a/packages/paths/src/helpers/serialize.ts b/packages/paths/src/helpers/serialize.ts
index c35471c615..3c35798fba 100644
--- a/packages/paths/src/helpers/serialize.ts
+++ b/packages/paths/src/helpers/serialize.ts
@@ -93,7 +93,7 @@ const serializeInstruction = (instruction: Instruction): string => {
throw new Error(`Unknown instruction type: ${instruction.type}`);
};
-export const serializePath = (path: Instruction[]): string => {
+export const serializeInstructions = (path: Instruction[]): string => {
return path
.map((p) => {
return serializeInstruction(p);
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
index 16fba980d5..7b3e371517 100644
--- a/packages/paths/src/index.ts
+++ b/packages/paths/src/index.ts
@@ -6,7 +6,8 @@ export {getParts} from './get-parts';
export {getPointAtLength} from './get-point-at-length';
export {getSubpaths} from './get-subpaths';
export {getTangentAtLength} from './get-tangent-at-length';
-export {BoundingBox, Part} from './helpers/types';
+export {serializeInstructions} from './helpers/serialize';
+export {BoundingBox, Instruction, Part} from './helpers/types';
export {interpolatePath} from './interpolate-path';
export {normalizePath} from './normalize-path';
export {resetPath} from './reset-path';
diff --git a/packages/paths/src/translate-path.ts b/packages/paths/src/translate-path.ts
index 107c240879..332a8910d7 100644
--- a/packages/paths/src/translate-path.ts
+++ b/packages/paths/src/translate-path.ts
@@ -1,6 +1,6 @@
// Copied partially from https://github.com/michaelrhodes/translate-svg-path/blob/master/index.js
import {parsePath} from './helpers/parse';
-import {serializePath} from './helpers/serialize';
+import {serializeInstructions} from './helpers/serialize';
import type {Instruction} from './helpers/types';
const translateSegments = (segments: Instruction[], x: number, y: number) => {
@@ -64,5 +64,5 @@ const translateSegments = (segments: Instruction[], x: number, y: number) => {
};
export const translatePath = (path: string, x: number, y: number) => {
- return serializePath(translateSegments(parsePath(path), x, y));
+ return serializeInstructions(translateSegments(parsePath(path), x, y));
};
diff --git a/packages/shapes/src/components/render-svg.tsx b/packages/shapes/src/components/render-svg.tsx
index 7dc3c244d5..f09769a31b 100644
--- a/packages/shapes/src/components/render-svg.tsx
+++ b/packages/shapes/src/components/render-svg.tsx
@@ -1,5 +1,5 @@
+import type {Instruction} from '@remotion/paths/dist/helpers/types';
import React, {useMemo} from 'react';
-import type {Instruction} from '../utils/instructions';
export type AllShapesProps = Omit<
React.SVGProps<SVGPathElement>,
@@ -61,7 +61,21 @@ export const RenderSvg = ({
index === 0
? instructions[instructions.length - 1]
: instructions[index - 1];
- if (prevInstruction.type === 'z') {
+ if (
+ prevInstruction.type === 'V' ||
+ prevInstruction.type === 'H' ||
+ prevInstruction.type === 'a' ||
+ prevInstruction.type === 'z' ||
+ prevInstruction.type === 'Z' ||
+ prevInstruction.type === 't' ||
+ prevInstruction.type === 'q' ||
+ prevInstruction.type === 'l' ||
+ prevInstruction.type === 'c' ||
+ prevInstruction.type === 'm' ||
+ prevInstruction.type === 'h' ||
+ prevInstruction.type === 's' ||
+ prevInstruction.type === 'v'
+ ) {
return null;
}
diff --git a/packages/shapes/src/index.ts b/packages/shapes/src/index.ts
index a4c9f6a86f..e641faf818 100644
--- a/packages/shapes/src/index.ts
+++ b/packages/shapes/src/index.ts
@@ -1,10 +1,10 @@
+export {Instruction} from '@remotion/paths';
export {Circle, CircleProps} from './components/circle';
export {Ellipse, EllipseProps} from './components/ellipse';
export {Pie, PieProps} from './components/pie';
export {Rect, RectProps} from './components/rect';
export {Star, StarProps} from './components/star';
export {Triangle, TriangleProps} from './components/triangle';
-export {Instruction} from './utils/instructions';
export {makeCircle, MakeCircleProps} from './utils/make-circle';
export {makeEllipse, MakeEllipseOptions} from './utils/make-ellipse';
export {makePie, MakePieProps} from './utils/make-pie';
diff --git a/packages/shapes/src/test/make-circle.test.ts b/packages/shapes/src/test/make-circle.test.ts
index a413758a00..ad398cc243 100644
--- a/packages/shapes/src/test/make-circle.test.ts
+++ b/packages/shapes/src/test/make-circle.test.ts
@@ -17,8 +17,8 @@ test('Should be able to make a circle path', () => {
xAxisRotation: 0,
largeArcFlag: true,
sweepFlag: false,
- x: 100,
- y: 0,
+ dx: 100,
+ dy: 0,
},
{
type: 'a',
@@ -27,8 +27,8 @@ test('Should be able to make a circle path', () => {
xAxisRotation: 0,
largeArcFlag: true,
sweepFlag: false,
- x: -100,
- y: 0,
+ dx: -100,
+ dy: 0,
},
{
type: 'z',
diff --git a/packages/shapes/src/test/make-ellipse.test.ts b/packages/shapes/src/test/make-ellipse.test.ts
index c265175c10..4ccae2181c 100644
--- a/packages/shapes/src/test/make-ellipse.test.ts
+++ b/packages/shapes/src/test/make-ellipse.test.ts
@@ -17,8 +17,8 @@ test('Should be able to make a circle path', () => {
xAxisRotation: 0,
largeArcFlag: true,
sweepFlag: false,
- x: 1,
- y: 0,
+ dx: 1,
+ dy: 0,
},
{
type: 'z',
diff --git a/packages/shapes/src/utils/instructions.ts b/packages/shapes/src/utils/instructions.ts
deleted file mode 100644
index 059055df94..0000000000
--- a/packages/shapes/src/utils/instructions.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-import type {Instruction} from '@remotion/paths';
-
-export const serializeInstructions = (instructions: Instruction[]) => {
- return instructions.map((i) => serializeInstruction(i)).join(' ');
-};
-
-export const serializeInstruction = (instruction: Instruction) => {
- if (instruction.type === 'M') {
- return `M ${instruction.x} ${instruction.y}`;
- }
-
- if (instruction.type === 'L') {
- return `L ${instruction.x} ${instruction.y}`;
- }
-
- if (instruction.type === 'C') {
- return `C ${instruction.cp1x} ${instruction.cp1y} ${instruction.cp2x} ${instruction.cp2y} ${instruction.x} ${instruction.y}`;
- }
-
- if (instruction.type === 'a') {
- return `a ${instruction.rx} ${instruction.ry} ${
- instruction.xAxisRotation
- } ${Number(instruction.largeArcFlag)} ${Number(
- instruction.sweepFlag ? 1 : 0
- )} ${instruction.x} ${instruction.y}`;
- }
-
- if (instruction.type === 'A') {
- return `A ${instruction.rx} ${instruction.ry} ${
- instruction.xAxisRotation
- } ${Number(instruction.largeArcFlag)} ${Number(
- instruction.sweepFlag ? 1 : 0
- )} ${instruction.x} ${instruction.y}`;
- }
-
- if (instruction.type === 'z') {
- return 'z';
- }
-
- throw new Error('not implemented');
-};
diff --git a/packages/shapes/src/utils/join-points.ts b/packages/shapes/src/utils/join-points.ts
index b507ab8d81..ec9959f7e7 100644
--- a/packages/shapes/src/utils/join-points.ts
+++ b/packages/shapes/src/utils/join-points.ts
@@ -1,4 +1,4 @@
-import type {Instruction} from './instructions';
+import type {Instruction} from '@remotion/paths';
const shortenVector = (vector: readonly [number, number], radius: number) => {
const [x, y] = vector;
@@ -30,7 +30,7 @@ export const joinPoints = (
}
) => {
return points
- .map(([x, y], i): Instruction | Instruction[] => {
+ .map(([x, y], i): Instruction[] => {
const prevPointIndex = i === 0 ? points.length - 2 : i - 1;
const prevPoint = points[prevPointIndex];
const nextPointIndex = i === points.length - 1 ? 1 : i + 1;
@@ -43,18 +43,22 @@ export const joinPoints = (
if (i === 0) {
if (edgeRoundness !== null) {
- return {
- type: 'M',
- x: middleOfLine[0],
- y: middleOfLine[1],
- };
+ return [
+ {
+ type: 'M',
+ x: middleOfLine[0],
+ y: middleOfLine[1],
+ },
+ ];
}
- return {
- type: 'M',
- x,
- y,
- };
+ return [
+ {
+ type: 'M',
+ x,
+ y,
+ },
+ ];
}
const prevVector = [x - prevPoint[0], y - prevPoint[1]] as const;
@@ -68,11 +72,13 @@ export const joinPoints = (
if (edgeRoundness === null) {
if (cornerRadius === 0) {
- return {
- type: 'L',
- x,
- y,
- };
+ return [
+ {
+ type: 'L',
+ x,
+ y,
+ },
+ ];
}
const prevVectorMinusRadius = shortenVector(prevVector, cornerRadius);
@@ -99,8 +105,8 @@ export const joinPoints = (
rx: cornerRadius,
ry: cornerRadius,
xAxisRotation: 0,
- x: prevVectorLenght[0] + nextVectorMinusRadius[0],
- y: prevVectorLenght[1] + nextVectorMinusRadius[1],
+ dx: prevVectorLenght[0] + nextVectorMinusRadius[0],
+ dy: prevVectorLenght[1] + nextVectorMinusRadius[1],
largeArcFlag: false,
sweepFlag: true,
}
@@ -128,15 +134,17 @@ export const joinPoints = (
middleOfLine[1] - nextVector[1] * edgeRoundness * 0.5,
] as const;
- return {
- type: 'C',
- cp1x: controlPoint1[0],
- cp1y: controlPoint1[1],
- cp2x: controlPoint2[0],
- cp2y: controlPoint2[1],
- x: middleOfLine[0],
- y: middleOfLine[1],
- };
+ return [
+ {
+ type: 'C',
+ cp1x: controlPoint1[0],
+ cp1y: controlPoint1[1],
+ cp2x: controlPoint2[0],
+ cp2y: controlPoint2[1],
+ x: middleOfLine[0],
+ y: middleOfLine[1],
+ },
+ ];
})
.flat(1);
};
diff --git a/packages/shapes/src/utils/make-circle.ts b/packages/shapes/src/utils/make-circle.ts
index f546233725..cee833ab54 100644
--- a/packages/shapes/src/utils/make-circle.ts
+++ b/packages/shapes/src/utils/make-circle.ts
@@ -1,5 +1,5 @@
-import type {Instruction} from './instructions';
-import {serializeInstructions} from './instructions';
+import type {Instruction} from '@remotion/paths';
+import {serializeInstructions} from '@remotion/paths';
import type {ShapeInfo} from './shape-info';
export type MakeCircleProps = {
@@ -20,8 +20,8 @@ export const makeCircle = ({radius}: MakeCircleProps): ShapeInfo => {
xAxisRotation: 0,
largeArcFlag: true,
sweepFlag: false,
- x: radius * 2,
- y: 0,
+ dx: radius * 2,
+ dy: 0,
},
{
type: 'a',
@@ -30,8 +30,8 @@ export const makeCircle = ({radius}: MakeCircleProps): ShapeInfo => {
xAxisRotation: 0,
largeArcFlag: true,
sweepFlag: false,
- x: -radius * 2,
- y: 0,
+ dx: -radius * 2,
+ dy: 0,
},
{
type: 'z',
diff --git a/packages/shapes/src/utils/make-ellipse.ts b/packages/shapes/src/utils/make-ellipse.ts
index 46f501f4b0..57bf2c9b4f 100644
--- a/packages/shapes/src/utils/make-ellipse.ts
+++ b/packages/shapes/src/utils/make-ellipse.ts
@@ -1,5 +1,5 @@
-import type {Instruction} from './instructions';
-import {serializeInstructions} from './instructions';
+import type {Instruction} from '@remotion/paths';
+import {serializeInstructions} from '@remotion/paths';
import type {ShapeInfo} from './shape-info';
export type MakeEllipseOptions = {
@@ -21,8 +21,8 @@ export const makeEllipse = ({rx, ry}: MakeEllipseOptions): ShapeInfo => {
xAxisRotation: 0,
largeArcFlag: true,
sweepFlag: false,
- x: 1,
- y: 0,
+ dx: 1,
+ dy: 0,
},
{
type: 'z',
diff --git a/packages/shapes/src/utils/make-pie.ts b/packages/shapes/src/utils/make-pie.ts
index 8f4f085f63..0e23e58a0e 100644
--- a/packages/shapes/src/utils/make-pie.ts
+++ b/packages/shapes/src/utils/make-pie.ts
@@ -1,5 +1,5 @@
-import type {Instruction} from './instructions';
-import {serializeInstructions} from './instructions';
+import type {Instruction} from '@remotion/paths';
+import {serializeInstructions} from '@remotion/paths';
export type MakePieProps = {
radius: number;
diff --git a/packages/shapes/src/utils/make-rect.ts b/packages/shapes/src/utils/make-rect.ts
index 191b9d170b..8a3bf45784 100644
--- a/packages/shapes/src/utils/make-rect.ts
+++ b/packages/shapes/src/utils/make-rect.ts
@@ -1,5 +1,5 @@
-import type {Instruction} from './instructions';
-import {serializeInstructions} from './instructions';
+import type {Instruction} from '@remotion/paths';
+import {serializeInstructions} from '@remotion/paths';
import {joinPoints} from './join-points';
import type {ShapeInfo} from './shape-info';
diff --git a/packages/shapes/src/utils/make-star.ts b/packages/shapes/src/utils/make-star.ts
index 17312d4c92..00f5f1df40 100644
--- a/packages/shapes/src/utils/make-star.ts
+++ b/packages/shapes/src/utils/make-star.ts
@@ -1,7 +1,7 @@
// Copied from https://stackblitz.com/edit/svg-star-generator?file=index.js
-import type {Instruction} from './instructions';
-import {serializeInstructions} from './instructions';
+import type {Instruction} from '@remotion/paths';
+import {serializeInstructions} from '@remotion/paths';
import {joinPoints} from './join-points';
import type {ShapeInfo} from './shape-info';
diff --git a/packages/shapes/src/utils/make-triangle.ts b/packages/shapes/src/utils/make-triangle.ts
index f6b8fde43c..b64f3ea76b 100644
--- a/packages/shapes/src/utils/make-triangle.ts
+++ b/packages/shapes/src/utils/make-triangle.ts
@@ -1,7 +1,7 @@
// Copied from https://stackblitz.com/edit/react-triangle-svg?file=index.js
-import type {Instruction} from './instructions';
-import {serializeInstructions} from './instructions';
+import type {Instruction} from '@remotion/paths';
+import {serializeInstructions} from '@remotion/paths';
import {joinPoints} from './join-points';
import type {ShapeInfo} from './shape-info';
diff --git a/packages/shapes/src/utils/shape-info.ts b/packages/shapes/src/utils/shape-info.ts
index 431084a180..29d1719aec 100644
--- a/packages/shapes/src/utils/shape-info.ts
+++ b/packages/shapes/src/utils/shape-info.ts
@@ -1,4 +1,4 @@
-import type {Instruction} from './instructions';
+import type {Instruction} from '@remotion/paths';
export type ShapeInfo = {
path: string;