Skip to content

🎥 Make videos programmatically with React

shapes: Add Heart shape component and documentation

copilot-swe-agent[bot]b908019cde3a
diff --git a/packages/docs/components/shapes/shapes-info.tsx b/packages/docs/components/shapes/shapes-info.tsx
index bf47f51b7c..0a602ce158 100644
--- a/packages/docs/components/shapes/shapes-info.tsx
+++ b/packages/docs/components/shapes/shapes-info.tsx
@@ -1,6 +1,7 @@
import {
makeCircle,
makeEllipse,
+ makeHeart,
makePie,
makePolygon,
makeRect,
@@ -54,6 +55,17 @@ export const shapeComponents: ShapeComponent[] = [
},
],
},
+ {
+ shape: 'Heart',
+ fn: makeHeart,
+ params: [
+ {
+ name: 'size',
+ type: 'number',
+ description: 'The size of the heart.',
+ },
+ ],
+ },
{
shape: 'Pie',
fn: makePie,
diff --git a/packages/docs/docs/shapes/heart.mdx b/packages/docs/docs/shapes/heart.mdx
new file mode 100644
index 0000000000..317a131c58
--- /dev/null
+++ b/packages/docs/docs/shapes/heart.mdx
@@ -0,0 +1,43 @@
+---
+title: <Heart />
+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 heart.
+
+## Explorer
+
+<Demo type="heart" />
+
+## Example
+
+```tsx twoslash title="src/Heart.tsx"
+import {Heart} from '@remotion/shapes';
+import {AbsoluteFill} from 'remotion';
+
+export const MyComposition = () => {
+ return (
+ <AbsoluteFill
+ style={{
+ backgroundColor: 'white',
+ justifyContent: 'center',
+ alignItems: 'center',
+ }}
+ >
+ <Heart size={100} fill="red" stroke="black" strokeWidth={2} />
+ </AbsoluteFill>
+ );
+};
+```
+
+## Props
+
+<ShapeOptions shape="heart" all />
+
+## See also
+
+<ShapeSeeAlso shape="heart" />
\ No newline at end of file
diff --git a/packages/docs/docs/shapes/make-heart.mdx b/packages/docs/docs/shapes/make-heart.mdx
new file mode 100644
index 0000000000..fdaaf580fd
--- /dev/null
+++ b/packages/docs/docs/shapes/make-heart.mdx
@@ -0,0 +1,44 @@
+---
+title: makeHeart()
+crumb: '@remotion/shapes'
+---
+
+import {MakeShapeSeeAlso, MakeShapeOptions} from '../../components/shapes/shapes-info';
+
+_Part of the [`@remotion/shapes`](/docs/shapes) package._
+
+Generates an SVG path for a heart.
+
+## Example
+
+```ts twoslash title="make-heart.ts"
+import {makeHeart} from '@remotion/shapes';
+
+const {path, width, height, transformOrigin, instructions} = makeHeart({
+ size: 100,
+});
+
+console.log(path); // M 100 120 C 40 110 20 70 60 60 C 90 40 110 40 140 60 C 180 70 160 110 100 120 Z
+console.log(width); // 200
+console.log(height); // 160
+console.log(transformOrigin); // "100 80"
+console.log(instructions); // Instruction[]
+```
+
+## Arguments
+
+<MakeShapeOptions shape="heart" />
+
+## Return type
+
+The function returns an object with the following properties:
+
+- `path`: The SVG path string
+- `width`: The width of the heart
+- `height`: The height of the heart
+- `transformOrigin`: The transform origin of the heart
+- `instructions`: An array of path instructions
+
+## See also
+
+<MakeShapeSeeAlso shape="heart" />
\ No newline at end of file
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 4a7222ffc8..3ff308b39e 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -509,6 +509,7 @@ module.exports = {
'shapes/triangle',
'shapes/circle',
'shapes/ellipse',
+ 'shapes/heart',
'shapes/star',
'shapes/pie',
'shapes/polygon',
@@ -516,6 +517,7 @@ module.exports = {
'shapes/make-triangle',
'shapes/make-circle',
'shapes/make-ellipse',
+ 'shapes/make-heart',
'shapes/make-star',
'shapes/make-pie',
'shapes/make-polygon',
diff --git a/packages/shapes/src/components/heart.tsx b/packages/shapes/src/components/heart.tsx
new file mode 100644
index 0000000000..449ab1279f
--- /dev/null
+++ b/packages/shapes/src/components/heart.tsx
@@ -0,0 +1,16 @@
+import React from 'react';
+import type {MakeHeartProps} from '../utils/make-heart';
+import {makeHeart} from '../utils/make-heart';
+import type {AllShapesProps} from './render-svg';
+import {RenderSvg} from './render-svg';
+
+export type HeartProps = MakeHeartProps & AllShapesProps;
+
+/**
+ * @description Renders an SVG element containing a heart.
+ * @param {Number} size The size of the heart.
+ * @see [Documentation](https://www.remotion.dev/docs/shapes/heart)
+ */
+export const Heart: React.FC<HeartProps> = ({size, ...props}) => {
+ return <RenderSvg {...makeHeart({size})} {...props} />;
+};
\ No newline at end of file
diff --git a/packages/shapes/src/index.ts b/packages/shapes/src/index.ts
index b50310a014..17e4ea75ac 100644
--- a/packages/shapes/src/index.ts
+++ b/packages/shapes/src/index.ts
@@ -1,6 +1,7 @@
export type {Instruction} from '@remotion/paths';
export {Circle, CircleProps} from './components/circle';
export {Ellipse, EllipseProps} from './components/ellipse';
+export {Heart, HeartProps} from './components/heart';
export {Pie, PieProps} from './components/pie';
export {Polygon, PolygonProps} from './components/polygon';
export {Rect, RectProps} from './components/rect';
@@ -8,6 +9,7 @@ export {Star, StarProps} from './components/star';
export {Triangle, TriangleProps} from './components/triangle';
export {MakeCircleProps, makeCircle} from './utils/make-circle';
export {MakeEllipseOptions, makeEllipse} from './utils/make-ellipse';
+export {MakeHeartProps, makeHeart} from './utils/make-heart';
export {MakePieProps, makePie} from './utils/make-pie';
export {MakePolygonProps, makePolygon} from './utils/make-polygon';
export {MakeRectOptions, makeRect} from './utils/make-rect';
diff --git a/packages/shapes/src/test/heart.test.tsx b/packages/shapes/src/test/heart.test.tsx
new file mode 100644
index 0000000000..59b852bead
--- /dev/null
+++ b/packages/shapes/src/test/heart.test.tsx
@@ -0,0 +1,14 @@
+import {expect, test} from 'bun:test';
+import {Heart} from '../components/heart';
+import {render} from './test-utils';
+
+test('Should be able to make a heart svg', () => {
+ const {container} = render(<Heart size={100} />);
+
+ expect(
+ container
+ .querySelector('path')
+ ?.getAttribute('d')
+ ?.startsWith('M 100 120 C'),
+ ).toBe(true);
+});
\ No newline at end of file
diff --git a/packages/shapes/src/test/make-heart.test.ts b/packages/shapes/src/test/make-heart.test.ts
new file mode 100644
index 0000000000..e71fa25e09
--- /dev/null
+++ b/packages/shapes/src/test/make-heart.test.ts
@@ -0,0 +1,16 @@
+import {expect, test} from 'bun:test';
+import {makeHeart} from '../utils/make-heart';
+
+test('Should be able to make a heart path', () => {
+ const heartPath = makeHeart({
+ size: 100,
+ });
+
+ expect(heartPath.width).toEqual(200);
+ expect(heartPath.height).toEqual(160);
+ expect(heartPath.transformOrigin).toEqual('100 80');
+ expect(heartPath.instructions.length).toEqual(5);
+ expect(heartPath.instructions[0]).toEqual({type: 'M', x: 100, y: 120});
+ expect(heartPath.instructions[4]).toEqual({type: 'Z'});
+ expect(heartPath.path.startsWith('M 100 120 C')).toBeTruthy();
+});
\ No newline at end of file
diff --git a/packages/shapes/src/utils/make-heart.ts b/packages/shapes/src/utils/make-heart.ts
new file mode 100644
index 0000000000..1150683024
--- /dev/null
+++ b/packages/shapes/src/utils/make-heart.ts
@@ -0,0 +1,73 @@
+import type {Instruction} from '@remotion/paths';
+import {serializeInstructions} from '@remotion/paths';
+import type {ShapeInfo} from './shape-info';
+
+export type MakeHeartProps = {
+ size: number;
+};
+
+/**
+ * @description Generates a heart SVG path.
+ * @param {Number} size The size of the heart.
+ * @see [Documentation](https://www.remotion.dev/docs/shapes/make-heart)
+ */
+export const makeHeart = ({size}: MakeHeartProps): ShapeInfo => {
+ const centerX = size;
+ const centerY = size * 0.8;
+
+ // Create heart shape using bezier curves
+ // Heart is made of two rounded lobes at the top and a point at the bottom
+ const instructions: Instruction[] = [
+ // Start at the bottom point of the heart
+ {
+ type: 'M',
+ x: centerX,
+ y: centerY + size * 0.4,
+ },
+ // Left side of heart - curve up to left lobe
+ {
+ type: 'C',
+ cp1x: centerX - size * 0.6,
+ cp1y: centerY + size * 0.1,
+ cp2x: centerX - size * 0.8,
+ cp2y: centerY - size * 0.3,
+ x: centerX - size * 0.4,
+ y: centerY - size * 0.4,
+ },
+ // Left lobe - rounded top
+ {
+ type: 'C',
+ cp1x: centerX - size * 0.1,
+ cp1y: centerY - size * 0.6,
+ cp2x: centerX + size * 0.1,
+ cp2y: centerY - size * 0.6,
+ x: centerX + size * 0.4,
+ y: centerY - size * 0.4,
+ },
+ // Right lobe - curve back down to bottom point
+ {
+ type: 'C',
+ cp1x: centerX + size * 0.8,
+ cp1y: centerY - size * 0.3,
+ cp2x: centerX + size * 0.6,
+ cp2y: centerY + size * 0.1,
+ x: centerX,
+ y: centerY + size * 0.4,
+ },
+ {
+ type: 'Z',
+ },
+ ];
+
+ const path = serializeInstructions(instructions);
+ const width = size * 2;
+ const height = size * 1.6;
+
+ return {
+ path,
+ width,
+ height,
+ transformOrigin: `${centerX} ${centerY}`,
+ instructions,
+ };
+};
\ No newline at end of file