Skip to content

🎥 Make videos programmatically with React

return instructions

Jonny Burger02f71b3db043
diff --git a/packages/shapes/src/utils/make-star.ts b/packages/shapes/src/utils/make-star.ts
index 4aa3763483..c09e06b02b 100644
--- a/packages/shapes/src/utils/make-star.ts
+++ b/packages/shapes/src/utils/make-star.ts
@@ -1,7 +1,9 @@
// Copied from https://stackblitz.com/edit/svg-star-generator?file=index.js
+import type {Instruction} from './instructions';
+import {serializeInstructions} from './instructions';
+import {joinPoints} from './join-points';
import type {ShapeInfo} from './shape-info';
-import {star} from './star';
export type MakeStarProps = {
points: number;
@@ -11,6 +13,69 @@ export type MakeStarProps = {
cornerRadius: number;
};
+type PolarToCartesianProps = {
+ centerX: number;
+ centerY: number;
+ radius: number;
+ angleInDegrees: number;
+ outerRadius?: number;
+};
+
+const polarToCartesian = ({
+ centerX,
+ centerY,
+ radius,
+ angleInDegrees,
+}: PolarToCartesianProps) => {
+ const angleInRadians = ((angleInDegrees - 90) * Math.PI) / 180.0;
+ return {
+ x: centerX + radius * Math.cos(angleInRadians),
+ y: centerY + radius * Math.sin(angleInRadians),
+ };
+};
+
+export type StarProps = {
+ centerX: number;
+ centerY: number;
+ points: number;
+ innerRadius: number;
+ outerRadius: number;
+ edgeRoundness: number | null;
+ cornerRadius: number;
+};
+
+const star = ({
+ centerX,
+ centerY,
+ points,
+ innerRadius,
+ outerRadius,
+ cornerRadius,
+ edgeRoundness,
+}: StarProps): Instruction[] => {
+ const degreeIncrement = 360 / (points * 2);
+ const d = new Array(points * 2)
+ .fill('true')
+ .map((_p, i): [number, number] => {
+ const radius = i % 2 === 0 ? outerRadius : innerRadius;
+ const degrees = degreeIncrement * i;
+ const point = polarToCartesian({
+ centerX,
+ centerY,
+ radius,
+ angleInDegrees: degrees,
+ });
+
+ return [point.x, point.y];
+ });
+
+ return joinPoints([...d, d[0]], {
+ edgeRoundness,
+ cornerRadius,
+ roundCornerStrategy: 'arc',
+ });
+};
+
export const makeStar = ({
points,
innerRadius,
@@ -24,7 +89,7 @@ export const makeStar = ({
const centerX = width / 2;
const centerY = height / 2;
- const starPath = star({
+ const starPathInstructions = star({
centerX,
centerY,
points,
@@ -35,10 +100,10 @@ export const makeStar = ({
});
return {
- path: starPath,
+ path: serializeInstructions(starPathInstructions),
width,
height,
transformOrigin: `${centerX} ${centerY}`,
- instructions: [],
+ instructions: starPathInstructions,
};
};
diff --git a/packages/shapes/src/utils/star.ts b/packages/shapes/src/utils/star.ts
deleted file mode 100644
index 9a24ca4a21..0000000000
--- a/packages/shapes/src/utils/star.ts
+++ /dev/null
@@ -1,67 +0,0 @@
-import {serializeInstructions} from './instructions';
-import {joinPoints} from './join-points';
-
-type PolarToCartesianProps = {
- centerX: number;
- centerY: number;
- radius: number;
- angleInDegrees: number;
- outerRadius?: number;
-};
-
-const polarToCartesian = ({
- centerX,
- centerY,
- radius,
- angleInDegrees,
-}: PolarToCartesianProps) => {
- const angleInRadians = ((angleInDegrees - 90) * Math.PI) / 180.0;
- return {
- x: centerX + radius * Math.cos(angleInRadians),
- y: centerY + radius * Math.sin(angleInRadians),
- };
-};
-
-export type StarProps = {
- centerX: number;
- centerY: number;
- points: number;
- innerRadius: number;
- outerRadius: number;
- edgeRoundness: number | null;
- cornerRadius: number;
-};
-
-export const star = ({
- centerX,
- centerY,
- points,
- innerRadius,
- outerRadius,
- cornerRadius,
- edgeRoundness,
-}: StarProps): string => {
- const degreeIncrement = 360 / (points * 2);
- const d = new Array(points * 2)
- .fill('true')
- .map((_p, i): [number, number] => {
- const radius = i % 2 === 0 ? outerRadius : innerRadius;
- const degrees = degreeIncrement * i;
- const point = polarToCartesian({
- centerX,
- centerY,
- radius,
- angleInDegrees: degrees,
- });
-
- return [point.x, point.y];
- });
-
- return serializeInstructions(
- joinPoints([...d, d[0]], {
- edgeRoundness,
- cornerRadius,
- roundCornerStrategy: 'arc',
- })
- );
-};