🎥 Make videos programmatically with React
instruction system
Jonny Burger2ffc34d79ef0
diff --git a/packages/shapes/src/utils/instructions.ts b/packages/shapes/src/utils/instructions.tsnew file mode 100644index 0000000000..29ed12be59--- /dev/null+++ b/packages/shapes/src/utils/instructions.ts@@ -0,0 +1,65 @@+export type Instruction =+ | {+ type: 'M';+ x: number;+ y: number;+ }+ | {+ type: 'L';+ x: number;+ y: number;+ }+ | {+ type: 'C';+ cp1x: number;+ cp1y: number;+ cp2x: number;+ cp2y: number;+ x: number;+ y: number;+ }+ | {+ type: 'm';+ x: number;+ y: number;+ }+ | {+ type: 'a';+ rx: number;+ ry: number;+ xAxisRotation: number;+ largeArcFlag: boolean;+ sweepFlag: boolean;+ x: number;+ y: number;+ };++export const serializeInstructions = (instructions: Instruction[]) => {+ return instructions.map(serializeInstruction).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 === 'm') {+ return `m ${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}`;+ }+};diff --git a/packages/shapes/src/utils/join-points.ts b/packages/shapes/src/utils/join-points.tsindex d28ed48c99..946072d030 100644--- a/packages/shapes/src/utils/join-points.ts+++ b/packages/shapes/src/utils/join-points.ts@@ -1,41 +1,59 @@+import type {Instruction} from './instructions';+export const joinPoints = (points: [number, number][],{squircleFactor}: {squircleFactor: number}) => {- const pointsString = points- .map(([x, y], i) => {- const prevPoint = i === 0 ? points[points.length - 2] : points[i - 1];- const nextPoint = i === points.length - 1 ? points[1] : points[i + 1];- const middleOfLine = [(x + prevPoint[0]) / 2, (y + prevPoint[1]) / 2];-- if (i === 0) {- if (squircleFactor > 0) {- return `M ${middleOfLine[0]} ${middleOfLine[1]}`;- }-- return `M ${x} ${y}`;- }-- if (squircleFactor === 0) {- return `L ${x} ${y}`;+ return points.map(([x, y], i): Instruction => {+ const prevPoint = i === 0 ? points[points.length - 2] : points[i - 1];+ const nextPoint = i === points.length - 1 ? points[1] : points[i + 1];+ const middleOfLine = [(x + prevPoint[0]) / 2, (y + prevPoint[1]) / 2];++ if (i === 0) {+ if (squircleFactor > 0) {+ return {+ type: 'M',+ x: middleOfLine[0],+ y: middleOfLine[1],+ };}- const prevVector = [x - prevPoint[0], y - prevPoint[1]] as const;- const nextVector = [nextPoint[0] - x, nextPoint[1] - y] as const;-- const controlPoint1 = [- prevPoint[0] + prevVector[0] * squircleFactor * 0.5,- prevPoint[1] + prevVector[1] * squircleFactor * 0.5,- ] as const;-- const controlPoint2 = [- x - nextVector[0] * squircleFactor * 0.5,- y - nextVector[1] * squircleFactor * 0.5,- ] as const;-- return `C ${controlPoint1[0]} ${controlPoint1[1]} ${controlPoint2[0]} ${controlPoint2[1]} ${middleOfLine[0]} ${middleOfLine[1]}`;- })- .join(' ');-- return pointsString;+ return {+ type: 'M',+ x,+ y,+ };+ }++ if (squircleFactor === 0) {+ return {+ type: 'L',+ x,+ y,+ };+ }++ const prevVector = [x - prevPoint[0], y - prevPoint[1]] as const;+ const nextVector = [nextPoint[0] - x, nextPoint[1] - y] as const;++ const controlPoint1 = [+ prevPoint[0] + prevVector[0] * squircleFactor * 0.5,+ prevPoint[1] + prevVector[1] * squircleFactor * 0.5,+ ] as const;++ const controlPoint2 = [+ x - nextVector[0] * squircleFactor * 0.5,+ y - nextVector[1] * squircleFactor * 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],+ };+ });};diff --git a/packages/shapes/src/utils/make-circle.ts b/packages/shapes/src/utils/make-circle.tsindex 416406c532..e38a668b95 100644--- a/packages/shapes/src/utils/make-circle.ts+++ b/packages/shapes/src/utils/make-circle.ts@@ -1,3 +1,5 @@+import type {Instruction} from './instructions';+import {serializeInstructions} from './instructions';import type {ShapeInfo} from './shape-info';export type MakeCircleProps = {@@ -5,12 +7,46 @@ export type MakeCircleProps = {};export const makeCircle = ({radius}: MakeCircleProps): ShapeInfo => {+ const instructions: Instruction[] = [+ {+ type: 'M',+ x: radius,+ y: radius,+ },+ {+ type: 'm',+ x: -radius,+ y: 0,+ },+ {+ type: 'a',+ rx: radius,+ ry: radius,+ xAxisRotation: 0,+ largeArcFlag: true,+ sweepFlag: false,+ x: radius * 2,+ y: 0,+ },+ {+ type: 'a',+ rx: radius,+ ry: radius,+ xAxisRotation: 0,+ largeArcFlag: true,+ sweepFlag: false,+ x: -radius * 2,+ y: 0,+ },+ ];++ const path = serializeInstructions(instructions);+return {height: radius * 2,width: radius * 2,- path: `M ${radius} ${radius} m -${radius} 0 a ${radius} ${radius} 0 1 0 ${- (radius as number) * 2- } 0 ${radius} ${radius} 0 1 0 ${-radius * 2} 0`,+ path,+ instructions,transformOrigin: `${radius} ${radius}`,};};diff --git a/packages/shapes/src/utils/make-ellipse.ts b/packages/shapes/src/utils/make-ellipse.tsindex 84b96a440f..5da4c98d31 100644--- a/packages/shapes/src/utils/make-ellipse.ts+++ b/packages/shapes/src/utils/make-ellipse.ts@@ -1,3 +1,5 @@+import type {Instruction} from './instructions';+import {serializeInstructions} from './instructions';import type {ShapeInfo} from './shape-info';export type MakeEllipseOptions = {@@ -6,10 +8,31 @@ export type MakeEllipseOptions = {};export const makeEllipse = ({rx, ry}: MakeEllipseOptions): ShapeInfo => {+ const instructions: Instruction[] = [+ {+ type: 'M',+ x: rx,+ y: 0,+ },+ {+ type: 'a',+ rx,+ ry,+ xAxisRotation: 0,+ largeArcFlag: true,+ sweepFlag: false,+ x: 1,+ y: 0,+ },+ ];++ const path = serializeInstructions(instructions);+return {width: rx * 2,height: ry * 2,- path: `M ${rx} ${0} a ${rx} ${ry} 0 1 0 1 0`,+ path,+ instructions,transformOrigin: `${rx} ${ry}`,};};diff --git a/packages/shapes/src/utils/make-rect.ts b/packages/shapes/src/utils/make-rect.tsindex 4ffe47f906..0dcea522c8 100644--- a/packages/shapes/src/utils/make-rect.ts+++ b/packages/shapes/src/utils/make-rect.ts@@ -1,3 +1,4 @@+import {serializeInstructions} from './instructions';import {joinPoints} from './join-points';import type {ShapeInfo} from './shape-info';@@ -8,7 +9,7 @@ export type MakeRectOptions = {export const makeRect = ({width, height}: MakeRectOptions): ShapeInfo => {const transformOrigin: [number, number] = [width / 2, height / 2];- const path = joinPoints(+ const instructions = joinPoints([[0, 0],[width, 0],@@ -18,10 +19,12 @@ export const makeRect = ({width, height}: MakeRectOptions): ShapeInfo => {],{squircleFactor: 1});+ const path = serializeInstructions(instructions);return {width,height,+ instructions,path,transformOrigin: transformOrigin.join(' '),};diff --git a/packages/shapes/src/utils/make-triangle.ts b/packages/shapes/src/utils/make-triangle.tsindex dcf1e02858..ff7f075c38 100644--- a/packages/shapes/src/utils/make-triangle.ts+++ b/packages/shapes/src/utils/make-triangle.ts@@ -1,5 +1,6 @@// Copied from https://stackblitz.com/edit/react-triangle-svg?file=index.js+import {serializeInstructions} from './instructions';import {joinPoints} from './join-points';import type {ShapeInfo} from './shape-info';@@ -58,8 +59,12 @@ export const makeTriangle = ({right: longerDimension / 2,}[direction];+ const instructions = joinPoints(points[direction], {squircleFactor: 1});+ const path = serializeInstructions(instructions);+return {- path: joinPoints(points[direction], {squircleFactor: 1}),+ path,+ instructions,width: direction === 'top' || direction === 'bottom' ? length : shorterSize,height:direction === 'top' || direction === 'bottom' ? shorterSize : length,diff --git a/packages/shapes/src/utils/shape-info.ts b/packages/shapes/src/utils/shape-info.tsindex e47d0547d4..431084a180 100644--- a/packages/shapes/src/utils/shape-info.ts+++ b/packages/shapes/src/utils/shape-info.ts@@ -1,6 +1,9 @@+import type {Instruction} from './instructions';+export type ShapeInfo = {path: string;width: number;height: number;transformOrigin: string;+ instructions: Instruction[];};