Skip to content

🎥 Make videos programmatically with React

Add <Polygon/> shape

neo773915c2da07e46
diff --git a/packages/docs/components/demos/Polygon.tsx b/packages/docs/components/demos/Polygon.tsx
new file mode 100644
index 0000000000..49f69798d0
--- /dev/null
+++ b/packages/docs/components/demos/Polygon.tsx
@@ -0,0 +1,28 @@
+import { Polygon } from "@remotion/shapes";
+import React from "react";
+import { AbsoluteFill } from "remotion";
+
+export const PolygonDemo: React.FC<{
+ darkMode: boolean;
+ points: number;
+ radius: number;
+ cornerRadius: number;
+ edgeRoundness: number | null;
+}> = ({ points, radius, darkMode, cornerRadius, edgeRoundness }) => {
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: "center",
+ alignItems: "center",
+ }}
+ >
+ <Polygon
+ fill={darkMode ? "white" : "var(--ifm-link-color)"}
+ points={points}
+ radius={radius}
+ cornerRadius={cornerRadius}
+ edgeRoundness={edgeRoundness}
+ />
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/docs/components/demos/index.tsx b/packages/docs/components/demos/index.tsx
index 34d1351ae7..9915b56ba7 100644
--- a/packages/docs/components/demos/index.tsx
+++ b/packages/docs/components/demos/index.tsx
@@ -15,6 +15,7 @@ import {
noiseDemo,
opacityDemo,
pieDemo,
+ polygonDemo,
rectDemo,
rotateDemo,
scaleDemo,
@@ -42,6 +43,7 @@ const demos: DemoType[] = [
circleDemo,
ellipseDemo,
starDemo,
+ polygonDemo,
pieDemo,
translateDemo,
skewDemo,
diff --git a/packages/docs/components/demos/types.ts b/packages/docs/components/demos/types.ts
index ba866bbd9d..30af4f1c03 100644
--- a/packages/docs/components/demos/types.ts
+++ b/packages/docs/components/demos/types.ts
@@ -11,6 +11,7 @@ import { CircleDemo } from "./Circle";
import { EllipseDemo } from "./Ellipse";
import { NoiseComp } from "./NoiseDemo";
import { PieDemo } from "./Pie";
+import { PolygonDemo } from "./Polygon";
import { RectDemo } from "./Rect";
import { StarDemo } from "./Star";
import {
@@ -382,6 +383,53 @@ export const ellipseDemo: DemoType = {
},
],
};
+export const polygonDemo: DemoType = {
+ comp: PolygonDemo,
+ compHeight: 720,
+ compWidth: 1280,
+ durationInFrames: 150,
+ fps: 30,
+ id: "polygon",
+ autoPlay: false,
+ options: [
+ {
+ default: 3,
+ max: 12,
+ step: 1,
+ min: 3,
+ type: "numeric",
+ name: "points",
+ optional: "no",
+ },
+ {
+ default: 100,
+ max: 400,
+ step: 5,
+ min: 1,
+ type: "numeric",
+ name: "radius",
+ optional: "no",
+ },
+ {
+ name: "cornerRadius",
+ default: 0,
+ max: 140,
+ min: 0,
+ step: 1,
+ type: "numeric",
+ optional: "no",
+ },
+ {
+ name: "edgeRoundness",
+ default: 0,
+ max: 2,
+ min: -2,
+ step: 0.01,
+ type: "numeric",
+ optional: "default-disabled",
+ },
+ ],
+};
export const starDemo: DemoType = {
comp: StarDemo,
diff --git a/packages/docs/components/shapes/shapes-info.tsx b/packages/docs/components/shapes/shapes-info.tsx
index 5a463dfcdf..2c36ea7a0c 100644
--- a/packages/docs/components/shapes/shapes-info.tsx
+++ b/packages/docs/components/shapes/shapes-info.tsx
@@ -2,6 +2,7 @@ import {
makeCircle,
makeEllipse,
makePie,
+ makePolygon,
makeRect,
makeStar,
makeTriangle,
@@ -153,6 +154,34 @@ export const shapeComponents: ShapeComponent[] = [
},
],
},
+ {
+ shape: "Polygon",
+ fn: makePolygon,
+ params: [
+ {
+ name: "points",
+ type: "number",
+ description: "The number of points in the polygon.",
+ },
+ {
+ name: "radius",
+ type: "number",
+ description: "The radius of the polygon.",
+ },
+ {
+ name: "edgeRoundness",
+ type: "number | null",
+ description:
+ "Allows to modify the shape by rounding the edges using bezier curves. Default null.",
+ },
+ {
+ name: "cornerRadius",
+ type: "number",
+ description:
+ "Rounds the corner using an arc. Similar to CSS's border-radius. Cannot be used together with edgeRoundness.",
+ },
+ ],
+ },
];
const globalParams: Param[] = [
@@ -232,7 +261,7 @@ export const ShapeOptions: React.FC<{
all: boolean;
}> = ({ shape, all }) => {
const shapeComponent = shapeComponents.find(
- (c) => c.shape.toLowerCase() === shape.toLowerCase()
+ (c) => c.shape.toLowerCase() === shape.toLowerCase(),
);
return (
@@ -255,7 +284,8 @@ export const ShapeOptions: React.FC<{
})}
{all &&
(shapeComponent.shape === "Rect" ||
- shapeComponent.shape === "Triangle") ? (
+ shapeComponent.shape === "Triangle" ||
+ shapeComponent.shape === "Polygon") ? (
<>
<h3>
<code>cornerRadius</code>
@@ -295,7 +325,7 @@ export const MakeShapeReturnType: React.FC<{
shape: string;
}> = ({ shape }) => {
const shapeComponent = shapeComponents.find(
- (c) => c.shape.toLowerCase() === shape.toLowerCase()
+ (c) => c.shape.toLowerCase() === shape.toLowerCase(),
);
return (
@@ -353,7 +383,7 @@ export const MakeShapeSeeAlso: React.FC<{
shape: string;
}> = ({ shape }) => {
const shapeComponent = shapeComponents.find(
- (c) => c.shape.toLowerCase() === shape.toLowerCase()
+ (c) => c.shape.toLowerCase() === shape.toLowerCase(),
);
return (
@@ -383,7 +413,7 @@ export const ShapeSeeAlso: React.FC<{
shape: string;
}> = ({ shape }) => {
const shapeComponent = shapeComponents.find(
- (c) => c.shape.toLowerCase() === shape.toLowerCase()
+ (c) => c.shape.toLowerCase() === shape.toLowerCase(),
);
return (
diff --git a/packages/docs/docs/shapes/make-polygon.md b/packages/docs/docs/shapes/make-polygon.md
new file mode 100644
index 0000000000..9af0cd1970
--- /dev/null
+++ b/packages/docs/docs/shapes/make-polygon.md
@@ -0,0 +1,40 @@
+---
+image: /generated/articles-docs-shapes-make-polygon.png
+title: makePolygon()
+crumb: "@remotion/shapes"
+---
+
+import {MakeShapeSeeAlso, ShapeOptions, MakeShapeReturnType} from "../../components/shapes/shapes-info"
+
+_Part of the [`@remotion/shapes`](/docs/shapes) package._
+
+Generates a polygon SVG path.
+
+## Example
+
+```tsx twoslash title="polygon.ts"
+import { makePolygon } from "@remotion/shapes";
+
+const { path, width, height, transformOrigin, instructions } = makePolygon({
+ points: 5,
+ radius: 80,
+});
+
+console.log(path); // M 76.08452130361228 0 L 152.16904260722458 55.278640450004204 L 123.10734148701013 144.72135954999578 L 29.061701120214437 144.72135954999578 L 0 55.27864045000418
+console.log(width); // 160
+console.log(height); // 160
+console.log(transformOrigin); // '80 80'
+console.log(instructions); // '[{type: "M"}, ...]'
+```
+
+## Arguments
+
+<ShapeOptions shape="polygon"/>
+
+## Return type
+
+<MakeShapeReturnType shape="polygon"/>
+
+## See also
+
+<MakeShapeSeeAlso shape="polygon"/>
diff --git a/packages/docs/docs/shapes/polygon.md b/packages/docs/docs/shapes/polygon.md
new file mode 100644
index 0000000000..84d5044254
--- /dev/null
+++ b/packages/docs/docs/shapes/polygon.md
@@ -0,0 +1,46 @@
+---
+image: /generated/articles-docs-shapes-polygon.png
+title: <Polygon />
+crumb: "@remotion/shapes"
+---
+
+import {ShapeSeeAlso, ShapeOptions} from "../../components/shapes/shapes-info"
+
+_Part of the [` @remotion/shapes`](/docs/shapes) package._
+
+Renders an SVG element containing a polygon.
+
+## Explorer
+
+<Demo type="polygon"/>
+
+## Example
+
+```tsx twoslash title="src/Polygon.tsx"
+import { Polygon } from "@remotion/shapes";
+import { AbsoluteFill } from "remotion";
+
+export const MyComposition = () => {
+ return (
+ <AbsoluteFill
+ style={{
+ backgroundColor: "white",
+ justifyContent: "center",
+ alignItems: "center",
+ }}
+ >
+ <Polygon points={5} radius={80} />
+ </AbsoluteFill>
+ );
+};
+```
+
+## Props
+
+<ShapeOptions shape="polygon" all />
+
+## See also
+
+<ShapeSeeAlso shape="polygon"/>
+
+
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index e862b12c04..1c14e5aae4 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -2248,6 +2248,13 @@ export const articles = [
compId: "articles-docs-shapes-make-triangle",
crumb: "@remotion/shapes",
},
+ {
+ id: "shapes/make-polygon",
+ title: "makePolygon()",
+ relativePath: "docs/shapes/make-polygon.md",
+ compId: "articles-docs-shapes-make-polygon",
+ crumb: "@remotion/shapes",
+ },
{
id: "shapes/pie",
title: "<Pie />",
@@ -2276,6 +2283,13 @@ export const articles = [
compId: "articles-docs-shapes-triangle",
crumb: "@remotion/shapes",
},
+ {
+ id: "shapes/polygon",
+ title: "<Polygon />",
+ relativePath: "docs/shapes/polyon.md",
+ compId: "articles-docs-shapes-polyon",
+ crumb: "@remotion/shapes",
+ },
{
id: "skia/enable-skia",
title: "enableSkia()",
diff --git a/packages/shapes/README.md b/packages/shapes/README.md
index f9f5c49996..982a7f0fef 100644
--- a/packages/shapes/README.md
+++ b/packages/shapes/README.md
@@ -12,10 +12,12 @@ No dependencies are needed, meaning this package can be used independently of Re
- [`makeCircle()`](https://remotion.dev/docs/shapes/make-circle)
- [`makeTriangle()`](https://remotion.dev/docs/shapes/make-triangle)
- [`makeEllipse()`](https://remotion.dev/docs/shapes/make-ellipse)
+- [`makePolygon()`](https://remotion.dev/docs/shapes/make-polygon)
- [`<Circle />`](https://remotion.dev/docs/shapes/circle)
- [`<Rect />`](https://remotion.dev/shapes/shapes/rect)
- [`<Triangle />`](https://remotion.dev/shapes/shapes/triangle)
- [`<Ellipse />`](https://remotion.dev/shapes/shapes/ellipse)
+- [`<Polygon />`](https://remotion.dev/shapes/shapes/polygon)
## License
diff --git a/packages/shapes/src/components/polygon.tsx b/packages/shapes/src/components/polygon.tsx
new file mode 100644
index 0000000000..6fce783682
--- /dev/null
+++ b/packages/shapes/src/components/polygon.tsx
@@ -0,0 +1,27 @@
+import React from 'react';
+import type {MakePolygonProps} from '../utils/make-polygon';
+import {makePolygon} from '../utils/make-polygon';
+import type {AllShapesProps} from './render-svg';
+import {RenderSvg} from './render-svg';
+
+export type PolygonProps = MakePolygonProps & Omit<AllShapesProps, 'points'>;
+
+export const Polygon: React.FC<PolygonProps> = ({
+ points,
+ radius,
+ cornerRadius,
+ edgeRoundness,
+ ...props
+}) => {
+ return (
+ <RenderSvg
+ {...makePolygon({
+ points,
+ cornerRadius,
+ edgeRoundness,
+ radius,
+ })}
+ {...props}
+ />
+ );
+};
diff --git a/packages/shapes/src/index.ts b/packages/shapes/src/index.ts
index e641faf818..a3392c384d 100644
--- a/packages/shapes/src/index.ts
+++ b/packages/shapes/src/index.ts
@@ -2,12 +2,14 @@ export {Instruction} from '@remotion/paths';
export {Circle, CircleProps} from './components/circle';
export {Ellipse, EllipseProps} from './components/ellipse';
export {Pie, PieProps} from './components/pie';
+export {Polygon, PolygonProps} from './components/polygon';
export {Rect, RectProps} from './components/rect';
export {Star, StarProps} from './components/star';
export {Triangle, TriangleProps} from './components/triangle';
export {makeCircle, MakeCircleProps} from './utils/make-circle';
export {makeEllipse, MakeEllipseOptions} from './utils/make-ellipse';
export {makePie, MakePieProps} from './utils/make-pie';
+export {makePolygon, MakePolygonProps} from './utils/make-polygon';
export {makeRect, MakeRectOptions} from './utils/make-rect';
export {makeStar, MakeStarProps} from './utils/make-star';
export {makeTriangle, MakeTriangleProps} from './utils/make-triangle';
diff --git a/packages/shapes/src/test/make-polygon.test.ts b/packages/shapes/src/test/make-polygon.test.ts
new file mode 100644
index 0000000000..f9cdb1f6f4
--- /dev/null
+++ b/packages/shapes/src/test/make-polygon.test.ts
@@ -0,0 +1,67 @@
+import {expect, test} from 'vitest';
+import {makePolygon} from '../utils/make-polygon';
+
+test('Should be able to make a triangle path', () => {
+ const polygonPath = makePolygon({
+ points: 3,
+ radius: 100,
+ });
+
+ expect(polygonPath).toEqual({
+ path: 'M 86.60254037844385 0 L 173.20508075688772 150 L 0 150.00000000000003 L 86.60254037844385 0',
+ width: 200,
+ height: 200,
+ transformOrigin: '100 100',
+ instructions: [
+ {type: 'M', x: 100, y: 0},
+ {type: 'L', x: 186.60254037844388, y: 150},
+ {type: 'L', x: 13.397459621556152, y: 150.00000000000003},
+ {type: 'L', x: 100, y: 0},
+ ],
+ });
+});
+
+test('Should be able to make a pentagon path', () => {
+ const polygonPath = makePolygon({
+ points: 5,
+ radius: 100,
+ });
+
+ expect(polygonPath).toEqual({
+ path: 'M 95.10565162951536 0 L 190.21130325903073 69.09830056250526 L 153.8841768587627 180.90169943749476 L 36.327126400268064 180.90169943749476 L 0 69.09830056250527 L 95.10565162951536 0',
+ width: 200,
+ height: 200,
+ transformOrigin: '100 100',
+ instructions: [
+ {type: 'M', x: 100, y: 0},
+ {type: 'L', x: 195.10565162951536, y: 69.09830056250526},
+ {type: 'L', x: 158.77852522924732, y: 180.90169943749476},
+ {type: 'L', x: 41.2214747707527, y: 180.90169943749476},
+ {type: 'L', x: 4.894348370484636, y: 69.09830056250527},
+ {type: 'L', x: 100, y: 0},
+ ],
+ });
+});
+
+test('Should be able to make a hexagon path', () => {
+ const polygonPath = makePolygon({
+ points: 6,
+ radius: 100,
+ });
+
+ expect(polygonPath).toEqual({
+ path: 'M 86.6025403784439 0 L 173.20508075688775 50 L 173.20508075688778 150 L 86.6025403784439 200 L 5.684341886080802e-14 150.00000000000003 L 0 50.000000000000064 L 86.6025403784439 0',
+ width: 200,
+ height: 200,
+ transformOrigin: '100 100',
+ instructions: [
+ {type: 'M', x: 100, y: 0},
+ {type: 'L', x: 186.60254037844385, y: 50},
+ {type: 'L', x: 186.60254037844388, y: 150},
+ {type: 'L', x: 100, y: 200},
+ {type: 'L', x: 13.397459621556152, y: 150.00000000000003},
+ {type: 'L', x: 13.397459621556095, y: 50.000000000000064},
+ {type: 'L', x: 100, y: 0},
+ ],
+ });
+});
diff --git a/packages/shapes/src/test/polygon.test.tsx b/packages/shapes/src/test/polygon.test.tsx
new file mode 100644
index 0000000000..d2c0100390
--- /dev/null
+++ b/packages/shapes/src/test/polygon.test.tsx
@@ -0,0 +1,32 @@
+import {expect, test} from 'vitest';
+import {Polygon} from '../components/polygon';
+import {render} from './test-utils';
+
+test('Should be able to make a triangle', () => {
+ const {container} = render(<Polygon radius={100} points={3} />);
+ expect(container.querySelector('path')?.getAttribute('d')).toEqual(
+ 'M 86.60254037844385 0 L 173.20508075688772 150 L 0 150.00000000000003 L 86.60254037844385 0',
+ );
+});
+
+test('Should be able to make a pentagon', () => {
+ const {container} = render(<Polygon radius={100} points={5} />);
+ expect(container.querySelector('path')?.getAttribute('d')).toEqual(
+ 'M 95.10565162951536 0 L 190.21130325903073 69.09830056250526 L 153.8841768587627 180.90169943749476 L 36.327126400268064 180.90169943749476 L 0 69.09830056250527 L 95.10565162951536 0',
+ );
+});
+
+test('Should be able to make a hexagon', () => {
+ const {container} = render(<Polygon radius={100} points={6} />);
+ expect(container.querySelector('path')?.getAttribute('d')).toEqual(
+ 'M 86.6025403784439 0 L 173.20508075688775 50 L 173.20508075688778 150 L 86.6025403784439 200 L 5.684341886080802e-14 150.00000000000003 L 0 50.000000000000064 L 86.6025403784439 0',
+ );
+});
+
+test('Create malformed Polygon, should fail', () => {
+ try {
+ render(<Polygon radius={100} points={2} />);
+ } catch (error) {
+ expect(error).toEqual(new Error(`"points" should be minimum 3, got 2`));
+ }
+});
diff --git a/packages/shapes/src/utils/make-polygon.ts b/packages/shapes/src/utils/make-polygon.ts
new file mode 100644
index 0000000000..aa0290951b
--- /dev/null
+++ b/packages/shapes/src/utils/make-polygon.ts
@@ -0,0 +1,78 @@
+import type {Instruction} from '@remotion/paths';
+import {resetPath, serializeInstructions} from '@remotion/paths';
+import {joinPoints} from './join-points';
+import type {ShapeInfo} from './shape-info';
+
+export type MakePolygonProps = {
+ points: number;
+ radius: number;
+ edgeRoundness?: number | null;
+ cornerRadius?: number;
+};
+
+export type PolygonProps = {
+ centerX: number;
+ centerY: number;
+ points: number;
+ radius: number;
+ edgeRoundness: number | null;
+ cornerRadius: number;
+};
+
+function polygon({
+ points,
+ radius,
+ centerX,
+ centerY,
+ cornerRadius,
+ edgeRoundness,
+}: PolygonProps): Instruction[] {
+ const degreeIncrement = (Math.PI * 2) / points;
+ const d = new Array(points).fill(0).map((_, i) => {
+ const angle = degreeIncrement * i - Math.PI / 2;
+ const point = {
+ x: centerX + radius * Math.cos(angle),
+ y: centerY + radius * Math.sin(angle),
+ };
+ return [point.x, point.y];
+ }) as [number, number][];
+
+ return joinPoints([...d, d[0]], {
+ edgeRoundness,
+ cornerRadius,
+ roundCornerStrategy: cornerRadius > 0 ? 'bezier' : 'arc',
+ });
+}
+
+export const makePolygon = ({
+ points,
+ radius,
+ cornerRadius = 0,
+ edgeRoundness = null,
+}: MakePolygonProps): ShapeInfo => {
+ if (points < 3) {
+ throw new Error(`"points" should be minimum 3, got ${points}`);
+ }
+
+ const width = 2 * radius;
+ const height = 2 * radius;
+
+ const centerX = width / 2;
+ const centerY = height / 2;
+
+ const polygonPathInstructions = polygon({
+ points,
+ radius,
+ centerX,
+ centerY,
+ cornerRadius,
+ edgeRoundness,
+ });
+ return {
+ path: resetPath(serializeInstructions(polygonPathInstructions)),
+ width,
+ height,
+ transformOrigin: `${centerX} ${centerY}`,
+ instructions: polygonPathInstructions,
+ };
+};