🎥 Make videos programmatically with React
@remotion/shapes: Add Arrow shape
copilot-swe-agent[bot]aa44bbd7619c
diff --git a/packages/docs/components/shapes/shapes-info.tsx b/packages/docs/components/shapes/shapes-info.tsxindex f0a3428c15..6ce2e29978 100644--- a/packages/docs/components/shapes/shapes-info.tsx+++ b/packages/docs/components/shapes/shapes-info.tsx@@ -1,4 +1,5 @@import {+ makeArrow,makeCircle,makeEllipse,makeHeart,@@ -28,6 +29,37 @@ export type ShapeComponent = {};export const shapeComponents: ShapeComponent[] = [+ {+ shape: 'Arrow',+ fn: makeArrow,+ params: [+ {+ name: 'length',+ type: 'number',+ description: 'The total length of the arrow along its direction axis.',+ },+ {+ name: 'headWidth',+ type: 'number',+ description: 'The width of the arrowhead at its widest point.',+ },+ {+ name: 'headLength',+ type: 'number',+ description: 'The length of the arrowhead portion.',+ },+ {+ name: 'shaftWidth',+ type: 'number',+ description: 'The width of the arrow shaft.',+ },+ {+ name: 'direction',+ type: '"left" | "right" | "up" | "down"',+ description: 'The direction the arrow points. Default right.',+ },+ ],+ },{shape: 'Rect',fn: makeRect,@@ -316,7 +348,8 @@ export const ShapeOptions: React.FC<{);})}{all &&- (shapeComponent.shape === 'Rect' ||+ (shapeComponent.shape === 'Arrow' ||+ shapeComponent.shape === 'Rect' ||shapeComponent.shape === 'Triangle' ||shapeComponent.shape === 'Polygon') ? (<>diff --git a/packages/docs/docs/shapes/arrow.mdx b/packages/docs/docs/shapes/arrow.mdxnew file mode 100644index 0000000000..394102b786--- /dev/null+++ b/packages/docs/docs/shapes/arrow.mdx@@ -0,0 +1,46 @@+---+title: <Arrow />+crumb: "@remotion/shapes"+---++import {ShapeSeeAlso, ShapeOptions} from "../../components/shapes/shapes-info"++_Part of the [` @remotion/shapes`](/docs/shapes) package._++Renders an SVG element containing an arrow shape.++## Example++```tsx twoslash title="src/Arrow.tsx"+import { Arrow } from "@remotion/shapes";+import { AbsoluteFill } from "remotion";++export const MyComposition = () => {+ return (+ <AbsoluteFill+ style={{+ backgroundColor: "white",+ justifyContent: "center",+ alignItems: "center",+ }}+ >+ <Arrow+ length={200}+ headWidth={80}+ headLength={80}+ shaftWidth={40}+ fill="red"+ direction="right"+ />+ </AbsoluteFill>+ );+};+```++## Props++<ShapeOptions shape="arrow" all />++## See also++<ShapeSeeAlso shape="arrow"/>diff --git a/packages/docs/docs/shapes/make-arrow.mdx b/packages/docs/docs/shapes/make-arrow.mdxnew file mode 100644index 0000000000..c9c377dc55--- /dev/null+++ b/packages/docs/docs/shapes/make-arrow.mdx@@ -0,0 +1,41 @@+---+title: makeArrow()+crumb: "@remotion/shapes"+---++import {MakeShapeSeeAlso, ShapeOptions, MakeShapeReturnType} from "../../components/shapes/shapes-info"++_Part of the [`@remotion/shapes`](/docs/shapes) package._++Generates an SVG path for an arrow shape.++## Example++```tsx twoslash title="arrow.ts"+import { makeArrow } from "@remotion/shapes";++const { path, width, height, transformOrigin } = makeArrow({+ length: 200,+ headWidth: 80,+ headLength: 80,+ shaftWidth: 40,+ direction: "right",+});++console.log(path); // M 0 20 L 120 20 L 120 0 L 200 40 L 120 80 L 120 60 L 0 60 L 0 20 Z+console.log(width); // 200+console.log(height); // 80+console.log(transformOrigin); // '100 40'+```++## Arguments++<ShapeOptions shape="arrow"/>++## Return type++<MakeShapeReturnType shape="arrow"/>++## See also++<MakeShapeSeeAlso shape="arrow"/>diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.tsindex eb6b9e16ba..2b2cc52308 100644--- a/packages/docs/sidebars.ts+++ b/packages/docs/sidebars.ts@@ -601,6 +601,7 @@ const sidebars: SidebarsConfig = {id: 'shapes/index',},items: [+ 'shapes/arrow','shapes/rect','shapes/triangle','shapes/circle',@@ -609,6 +610,7 @@ const sidebars: SidebarsConfig = {'shapes/star','shapes/pie','shapes/polygon',+ 'shapes/make-arrow','shapes/make-rect','shapes/make-triangle','shapes/make-circle',diff --git a/packages/shapes/src/components/arrow.tsx b/packages/shapes/src/components/arrow.tsxnew file mode 100644index 0000000000..1c0b388682--- /dev/null+++ b/packages/shapes/src/components/arrow.tsx@@ -0,0 +1,44 @@+import React from 'react';+import type {MakeArrowProps} from '../utils/make-arrow';+import {makeArrow} from '../utils/make-arrow';+import type {AllShapesProps} from './render-svg';+import {RenderSvg} from './render-svg';++export type ArrowProps = MakeArrowProps & AllShapesProps;++/**+ * @description Renders an SVG element containing an arrow shape.+ * @param {Number} length The total length of the arrow along its direction axis.+ * @param {Number} headWidth The width of the arrowhead at its widest point.+ * @param {Number} headLength The length of the arrowhead portion.+ * @param {Number} shaftWidth The width of the arrow shaft.+ * @param {string} direction The direction the arrow points. Default 'right'.+ * @param {Number} cornerRadius Rounds the corner using an arc. Similar to CSS's border-radius. Cannot be used together with edgeRoundness.+ * @param {null|Number} edgeRoundness Allows to modify the shape by rounding the edges using bezier curves. Default null.+ * @see [Documentation](https://www.remotion.dev/docs/shapes/arrow)+ */+export const Arrow: React.FC<ArrowProps> = ({+ length,+ headWidth,+ headLength,+ shaftWidth,+ direction,+ cornerRadius,+ edgeRoundness,+ ...props+}) => {+ return (+ <RenderSvg+ {...makeArrow({+ length,+ headWidth,+ headLength,+ shaftWidth,+ direction,+ cornerRadius,+ edgeRoundness,+ })}+ {...props}+ />+ );+};diff --git a/packages/shapes/src/index.ts b/packages/shapes/src/index.tsindex 17e4ea75ac..06a420f3e5 100644--- a/packages/shapes/src/index.ts+++ b/packages/shapes/src/index.ts@@ -1,4 +1,5 @@export type {Instruction} from '@remotion/paths';+export {Arrow, ArrowProps} from './components/arrow';export {Circle, CircleProps} from './components/circle';export {Ellipse, EllipseProps} from './components/ellipse';export {Heart, HeartProps} from './components/heart';@@ -7,6 +8,7 @@ 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 {MakeArrowProps, makeArrow} from './utils/make-arrow';export {MakeCircleProps, makeCircle} from './utils/make-circle';export {MakeEllipseOptions, makeEllipse} from './utils/make-ellipse';export {MakeHeartProps, makeHeart} from './utils/make-heart';diff --git a/packages/shapes/src/test/make-arrow.test.ts b/packages/shapes/src/test/make-arrow.test.tsnew file mode 100644index 0000000000..b98ae877e1--- /dev/null+++ b/packages/shapes/src/test/make-arrow.test.ts@@ -0,0 +1,102 @@+import {expect, test} from 'bun:test';+import {makeArrow} from '../utils/make-arrow';++test('Should be able to make a right-pointing arrow path', () => {+ const arrow = makeArrow({+ length: 200,+ headWidth: 80,+ headLength: 80,+ shaftWidth: 40,+ });++ expect(arrow).toEqual({+ width: 200,+ height: 80,+ path: 'M 0 20 L 120 20 L 120 0 L 200 40 L 120 80 L 120 60 L 0 60 L 0 20 Z',+ transformOrigin: '100 40',+ instructions: [+ {type: 'M', x: 0, y: 20},+ {type: 'L', x: 120, y: 20},+ {type: 'L', x: 120, y: 0},+ {type: 'L', x: 200, y: 40},+ {type: 'L', x: 120, y: 80},+ {type: 'L', x: 120, y: 60},+ {type: 'L', x: 0, y: 60},+ {type: 'L', x: 0, y: 20},+ {type: 'Z'},+ ],+ });+});++test('Should be able to make a left-pointing arrow path', () => {+ const arrow = makeArrow({+ length: 200,+ headWidth: 80,+ headLength: 80,+ shaftWidth: 40,+ direction: 'left',+ });++ expect(arrow.width).toEqual(200);+ expect(arrow.height).toEqual(80);+ expect(arrow.transformOrigin).toEqual('100 40');+ expect(arrow.path).toEqual(+ 'M 200 20 L 80 20 L 80 0 L 0 40 L 80 80 L 80 60 L 200 60 L 200 20 Z',+ );+});++test('Should be able to make an upward-pointing arrow path', () => {+ const arrow = makeArrow({+ length: 200,+ headWidth: 80,+ headLength: 80,+ shaftWidth: 40,+ direction: 'up',+ });++ expect(arrow.width).toEqual(80);+ expect(arrow.height).toEqual(200);+ expect(arrow.transformOrigin).toEqual('40 100');+ expect(arrow.path).toEqual(+ 'M 20 200 L 60 200 L 60 80 L 80 80 L 40 0 L 0 80 L 20 80 L 20 200 Z',+ );+});++test('Should be able to make a downward-pointing arrow path', () => {+ const arrow = makeArrow({+ length: 200,+ headWidth: 80,+ headLength: 80,+ shaftWidth: 40,+ direction: 'down',+ });++ expect(arrow.width).toEqual(80);+ expect(arrow.height).toEqual(200);+ expect(arrow.transformOrigin).toEqual('40 100');+ expect(arrow.path).toEqual(+ 'M 20 0 L 60 0 L 60 120 L 80 120 L 40 200 L 0 120 L 20 120 L 20 0 Z',+ );+});++test('Should throw if headWidth < shaftWidth', () => {+ expect(() =>+ makeArrow({+ length: 200,+ headWidth: 30,+ headLength: 80,+ shaftWidth: 40,+ }),+ ).toThrow('"headWidth" must be greater than or equal to "shaftWidth"');+});++test('Should throw if headLength > length', () => {+ expect(() =>+ makeArrow({+ length: 50,+ headWidth: 80,+ headLength: 80,+ shaftWidth: 40,+ }),+ ).toThrow('"headLength" must be less than or equal to "length"');+});diff --git a/packages/shapes/src/utils/make-arrow.ts b/packages/shapes/src/utils/make-arrow.tsnew file mode 100644index 0000000000..4159e2c3fa--- /dev/null+++ b/packages/shapes/src/utils/make-arrow.ts@@ -0,0 +1,124 @@+import type {Instruction} from '@remotion/paths';+import {serializeInstructions} from '@remotion/paths';+import {joinPoints} from './join-points';+import type {ShapeInfo} from './shape-info';++type ArrowDirection = 'right' | 'left' | 'up' | 'down';++export type MakeArrowProps = {+ length: number;+ headWidth: number;+ headLength: number;+ shaftWidth: number;+ direction?: ArrowDirection;+ edgeRoundness?: number | null;+ cornerRadius?: number;+};++/**+ * @description Generates an SVG path for an arrow shape.+ * @param {Number} length The total length of the arrow along its direction axis.+ * @param {Number} headWidth The width of the arrowhead at its widest point.+ * @param {Number} headLength The length of the arrowhead portion.+ * @param {Number} shaftWidth The width of the arrow shaft.+ * @param {string} direction The direction the arrow points. Default 'right'.+ * @param {Number} cornerRadius Rounds the corner using an arc. Similar to CSS's border-radius. Cannot be used together with edgeRoundness.+ * @param {null|Number} edgeRoundness Allows to modify the shape by rounding the edges using bezier curves. Default null.+ * @see [Documentation](https://www.remotion.dev/docs/shapes/make-arrow)+ */+export const makeArrow = ({+ length,+ headWidth,+ headLength,+ shaftWidth,+ direction = 'right',+ cornerRadius = 0,+ edgeRoundness = null,+}: MakeArrowProps): ShapeInfo => {+ if (headWidth < shaftWidth) {+ throw new Error(+ `"headWidth" must be greater than or equal to "shaftWidth", got headWidth=${headWidth} and shaftWidth=${shaftWidth}`,+ );+ }++ if (headLength > length) {+ throw new Error(+ `"headLength" must be less than or equal to "length", got headLength=${headLength} and length=${length}`,+ );+ }++ const shaftTop = (headWidth - shaftWidth) / 2;+ const shaftBottom = shaftTop + shaftWidth;+ const shaftEnd = length - headLength;++ // Points for a right-pointing arrow (clockwise from top-left of shaft)+ const rightPoints: [number, number][] = [+ [0, shaftTop],+ [shaftEnd, shaftTop],+ [shaftEnd, 0],+ [length, headWidth / 2],+ [shaftEnd, headWidth],+ [shaftEnd, shaftBottom],+ [0, shaftBottom],+ ];++ let points: [number, number][];+ let width: number;+ let height: number;++ if (direction === 'right') {+ points = rightPoints;+ width = length;+ height = headWidth;+ } else if (direction === 'left') {+ // Mirror x: x -> length - x+ points = rightPoints.map(([x, y]): [number, number] => [length - x, y]);+ width = length;+ height = headWidth;+ } else if (direction === 'down') {+ // Rotate 90° clockwise: swap axes so the tip points down+ points = [+ [shaftTop, 0],+ [shaftBottom, 0],+ [shaftBottom, shaftEnd],+ [headWidth, shaftEnd],+ [headWidth / 2, length],+ [0, shaftEnd],+ [shaftTop, shaftEnd],+ ];+ width = headWidth;+ height = length;+ } else {+ // up: mirror of down (y -> length - y)+ points = [+ [shaftTop, length],+ [shaftBottom, length],+ [shaftBottom, headLength],+ [headWidth, headLength],+ [headWidth / 2, 0],+ [0, headLength],+ [shaftTop, headLength],+ ];+ width = headWidth;+ height = length;+ }++ const instructions: Instruction[] = [+ ...joinPoints([...points, points[0]], {+ edgeRoundness,+ cornerRadius,+ roundCornerStrategy: cornerRadius > 0 ? 'bezier' : 'arc',+ }),+ {type: 'Z'},+ ];++ const path = serializeInstructions(instructions);++ return {+ path,+ instructions,+ width,+ height,+ transformOrigin: `${width / 2} ${height / 2}`,+ };+};