🎥 Make videos programmatically with React
`@remotion/shapes`: Add callout shape
Jonny Burgerd0dafec42967
diff --git a/packages/docs/components/demos/Callout.tsx b/packages/docs/components/demos/Callout.tsxnew file mode 100644index 0000000000..56e0abf529--- /dev/null+++ b/packages/docs/components/demos/Callout.tsx@@ -0,0 +1,49 @@+import {Callout} from '@remotion/shapes';+import React from 'react';+import {AbsoluteFill} from 'remotion';++export const CalloutDemo: React.FC<{+ readonly width: number;+ readonly height: number;+ readonly pointerLength: number;+ readonly pointerBaseWidth: number;+ readonly pointerPosition: number;+ readonly pointerDirection: 'up' | 'down' | 'left' | 'right';+ readonly edgeRoundness: number;+ readonly darkMode: boolean;+ readonly debug: boolean;+ readonly cornerRadius: number;+}> = ({+ width,+ height,+ pointerLength,+ pointerBaseWidth,+ pointerPosition,+ pointerDirection,+ debug,+ edgeRoundness,+ cornerRadius,+ darkMode,+}) => {+ return (+ <AbsoluteFill+ style={{+ justifyContent: 'center',+ alignItems: 'center',+ }}+ >+ <Callout+ fill={darkMode ? 'white' : 'var(--ifm-link-color)'}+ edgeRoundness={edgeRoundness}+ width={width}+ height={height}+ pointerLength={pointerLength}+ pointerBaseWidth={pointerBaseWidth}+ pointerPosition={pointerPosition}+ pointerDirection={pointerDirection}+ debug={debug}+ cornerRadius={cornerRadius}+ />+ </AbsoluteFill>+ );+};diff --git a/packages/docs/components/demos/index.tsx b/packages/docs/components/demos/index.tsxindex 3af8681b40..85ac808ff6 100644--- a/packages/docs/components/demos/index.tsx+++ b/packages/docs/components/demos/index.tsx@@ -14,6 +14,7 @@ import {animationMathDemo,arrowDemo,bookFlipPresentationDemo,+ calloutDemo,circleDemo,clockWipePresentationDemo,crosswarpPresentationDemo,@@ -108,6 +109,7 @@ const demos: DemoType[] = [arrowDemo,triangleDemo,rectDemo,+ calloutDemo,circleDemo,ellipseDemo,heartDemo,diff --git a/packages/docs/components/demos/types.ts b/packages/docs/components/demos/types.tsindex 885f5f0411..014496a5b4 100644--- a/packages/docs/components/demos/types.ts+++ b/packages/docs/components/demos/types.ts@@ -14,6 +14,7 @@ import {} from '../transitions/previews';import {ArrowDemo} from './Arrow';import {BookFlipDocsDemo} from './BookFlipDemo';+import {CalloutDemo} from './Callout';import {CircleDemo} from './Circle';import {CrosswarpDocsDemo} from './CrosswarpDemo';import {CrossZoomDocsDemo} from './CrossZoomDemo';@@ -230,6 +231,96 @@ export const rectDemo: DemoType = {],};+export const calloutDemo: DemoType = {+ comp: CalloutDemo,+ compWidth: 1280,+ compHeight: 400,+ durationInFrames: 150,+ fps: 30,+ id: 'callout',+ autoPlay: false,+ controls: false,+ logLevel: 'info',+ options: [+ {+ default: 500,+ max: 1000,+ step: 5,+ min: 1,+ type: 'numeric',+ name: 'width',+ optional: 'no',+ },+ {+ default: 200,+ max: 1000,+ step: 5,+ min: 1,+ type: 'numeric',+ name: 'height',+ optional: 'no',+ },+ {+ default: 40,+ max: 400,+ step: 5,+ min: 1,+ type: 'numeric',+ name: 'pointerLength',+ optional: 'no',+ },+ {+ default: 60,+ max: 400,+ step: 5,+ min: 1,+ type: 'numeric',+ name: 'pointerBaseWidth',+ optional: 'no',+ },+ {+ default: 0.5,+ max: 1,+ step: 0.01,+ min: 0,+ type: 'numeric',+ name: 'pointerPosition',+ optional: 'no',+ },+ {+ name: 'pointerDirection',+ type: 'enum',+ default: 'down',+ values: ['up', 'down', 'left', 'right'],+ optional: 'no',+ },+ {+ name: 'cornerRadius',+ default: 20,+ max: 100,+ min: 0,+ step: 1,+ type: 'numeric',+ optional: 'no',+ },+ {+ name: 'edgeRoundness',+ default: 1,+ max: 2,+ min: -2,+ step: 0.01,+ type: 'numeric',+ optional: 'default-disabled',+ },+ {+ name: 'debug',+ type: 'boolean',+ optional: 'no',+ default: false,+ },+ ],+};+export const triangleDemo: DemoType = {comp: TriangleDemo,compWidth: 1280,diff --git a/packages/docs/components/shapes/shape-component-drag-data.ts b/packages/docs/components/shapes/shape-component-drag-data.tsindex 3df3d622b9..4d342a60dd 100644--- a/packages/docs/components/shapes/shape-component-drag-data.ts+++ b/packages/docs/components/shapes/shape-component-drag-data.ts@@ -15,6 +15,16 @@ const shapeDefaultProps: Record<ShapeName, readonly ComponentProp[]> = {{name: 'direction', value: 'right'},{name: 'fill', value: '#0b84ff'},],+ Callout: [+ {name: 'width', value: 500},+ {name: 'height', value: 200},+ {name: 'pointerLength', value: 40},+ {name: 'pointerBaseWidth', value: 60},+ {name: 'pointerPosition', value: 0.5},+ {name: 'pointerDirection', value: 'down'},+ {name: 'cornerRadius', value: 20},+ {name: 'fill', value: '#0b84ff'},+ ],Circle: [{name: 'radius', value: 100},{name: 'fill', value: '#0b84ff'},@@ -68,6 +78,7 @@ const shapeDefaultProps: Record<ShapeName, readonly ComponentProp[]> = {const shapeNameByDemoId: Partial<Record<string, ShapeName>> = {arrow: 'Arrow',+ callout: 'Callout',circle: 'Circle',ellipse: 'Ellipse',heart: 'Heart',@@ -87,6 +98,16 @@ const shapeDemoPropNames: Record<ShapeName, readonly string[]> = {'direction','cornerRadius',],+ Callout: [+ 'width',+ 'height',+ 'pointerLength',+ 'pointerBaseWidth',+ 'pointerPosition',+ 'pointerDirection',+ 'cornerRadius',+ 'edgeRoundness',+ ],Circle: ['radius'],Ellipse: ['rx', 'ry'],Heart: [diff --git a/packages/docs/components/shapes/shapes-info.tsx b/packages/docs/components/shapes/shapes-info.tsxindex 90d4a9cdd6..2952fefa41 100644--- a/packages/docs/components/shapes/shapes-info.tsx+++ b/packages/docs/components/shapes/shapes-info.tsx@@ -1,5 +1,6 @@import {makeArrow,+ makeCallout,makeCircle,makeEllipse,makeHeart,@@ -26,6 +27,7 @@ type Param = {export type ShapeName =| 'Arrow'+ | 'Callout'| 'Circle'| 'Ellipse'| 'Heart'@@ -104,6 +106,55 @@ export const shapeComponents: ShapeComponent[] = [},],},+ {+ shape: 'Callout',+ fn: makeCallout,+ params: [+ {+ name: 'width',+ type: 'number',+ description: 'The width of the callout body. Default 500.',+ hiddenFromList: false,+ },+ {+ name: 'height',+ type: 'number',+ description: 'The height of the callout body. Default 200.',+ hiddenFromList: false,+ },+ {+ name: 'pointerLength',+ type: 'number',+ description: 'The length of the pointer. Default 40.',+ hiddenFromList: false,+ },+ {+ name: 'pointerBaseWidth',+ type: 'number',+ description:+ 'The width of the pointer where it meets the body. Default 60.',+ hiddenFromList: false,+ },+ {+ name: 'pointerPosition',+ type: 'number',+ description:+ 'The position of the pointer along its side, from 0 to 1. Default 0.5.',+ hiddenFromList: false,+ },+ {+ name: 'pointerDirection',+ type: '"left" | "right" | "up" | "down"',+ description: 'The direction the pointer points. Default down.',+ },+ {+ name: 'edgeRoundness',+ type: 'number | null',+ description:+ 'Allows to modify the shape by rounding the edges using bezier curves. Default null.',+ },+ ],+ },{shape: 'Circle',fn: makeCircle,@@ -420,6 +471,7 @@ export const ShapeOptions: React.FC<{})}{all &&(shapeComponent.shape === 'Rect' ||+ shapeComponent.shape === 'Callout' ||shapeComponent.shape === 'Triangle' ||shapeComponent.shape === 'Polygon') ? (<>@@ -441,6 +493,7 @@ export const ShapeOptions: React.FC<{{shapeComponent.shape === 'Triangle' ? <TriangleEdgeRoundness /> : null}{all &&(shapeComponent.shape === 'Rect' ||+ shapeComponent.shape === 'Callout' ||shapeComponent.shape === 'Triangle') ? (<DebugOption />) : null}@@ -486,7 +539,8 @@ export const ShapeOptions: React.FC<{export const MakeShapeReturnType: React.FC<{readonly shape: string;-}> = ({shape}) => {+ readonly includeComponentLink?: boolean;+}> = ({shape, includeComponentLink = true}) => {const shapeComponent = shapeComponents.find((c) => c.shape.toLowerCase() === shape.toLowerCase(),);@@ -533,15 +587,23 @@ export const MakeShapeReturnType: React.FC<{A string representing the point of origin if a shape should be rotatedaround itself.</p>- <p>- If you want to rotate the shape around its center, use the{' '}- <code>transform-origin</code> CSS property and pass this value, and also- add <code>transform-box: fill-box</code>. This is the default for{' '}- <a href={`/docs/shapes/${shapeComponent.shape.toLowerCase()}`}>- <code>{`<${shapeComponent.shape} />`}</code>- </a>- .- </p>+ {includeComponentLink ? (+ <p>+ If you want to rotate the shape around its center, use the{' '}+ <code>transform-origin</code> CSS property and pass this value, and+ also add <code>transform-box: fill-box</code>. This is the default for{' '}+ <a href={`/docs/shapes/${shapeComponent.shape.toLowerCase()}`}>+ <code>{`<${shapeComponent.shape} />`}</code>+ </a>+ .+ </p>+ ) : (+ <p>+ If you want to rotate the shape around its center, use the{' '}+ <code>transform-origin</code> CSS property and pass this value, and+ also add <code>transform-box: fill-box</code>.+ </p>+ )}</div>);};diff --git a/packages/docs/docs/shapes/callout.mdx b/packages/docs/docs/shapes/callout.mdxnew file mode 100644index 0000000000..7470833793--- /dev/null+++ b/packages/docs/docs/shapes/callout.mdx@@ -0,0 +1,66 @@+---+image: /generated/articles-docs-shapes-callout.png+title: <Callout />+crumb: "@remotion/shapes"+---++import {ShapeOptions} from "../../components/shapes/shapes-info"++_Part of the [`@remotion/shapes`](/docs/shapes) package._++Renders an SVG element containing a callout shape.++## Explorer++<Demo type="callout"/>++## Example++```tsx twoslash title="src/Callout.tsx"+import { Callout } from "@remotion/shapes";+import { AbsoluteFill } from "remotion";++export const MyComposition = () => {+ return (+ <AbsoluteFill+ style={{+ backgroundColor: "white",+ justifyContent: "center",+ alignItems: "center",+ }}+ >+ <Callout+ width={500}+ height={200}+ pointerLength={40}+ pointerBaseWidth={60}+ pointerDirection="down"+ cornerRadius={20}+ fill="#20232a"+ />+ </AbsoluteFill>+ );+};+```++## Props++<ShapeOptions shape="callout" all />++## See also++<ul>+ <li>+ <a href="/docs/shapes/make-callout"><code>makeCallout()</code></a>+ </li>+ <li>+ <a href="/docs/shapes">+ <code>@remotion/shapes</code>+ </a>+ </li>+ <li>+ <a href="https://github.com/remotion-dev/remotion/blob/main/packages/shapes/src/components/callout.tsx">+ Source code for this component+ </a>+ </li>+</ul>diff --git a/packages/docs/docs/shapes/make-callout.mdx b/packages/docs/docs/shapes/make-callout.mdxnew file mode 100644index 0000000000..87b92c0fba--- /dev/null+++ b/packages/docs/docs/shapes/make-callout.mdx@@ -0,0 +1,57 @@+---+image: /generated/articles-docs-shapes-make-callout.png+title: makeCallout()+crumb: "@remotion/shapes"+---++import {ShapeOptions, MakeShapeReturnType} from "../../components/shapes/shapes-info"++_Part of the [`@remotion/shapes`](/docs/shapes) package._++Generates an SVG path for a callout shape.++## Example++```tsx twoslash title="callout.ts"+import { makeCallout } from "@remotion/shapes";++const { path, width, height, transformOrigin } = makeCallout({+ width: 500,+ height: 200,+ pointerLength: 40,+ pointerBaseWidth: 60,+ pointerDirection: "down",+ cornerRadius: 20,+});++console.log(path);+console.log(width); // 500+console.log(height); // 240+console.log(transformOrigin); // '250 100'+```++## Arguments++<ShapeOptions shape="callout"/>++## Return type++<MakeShapeReturnType shape="callout" includeComponentLink={false}/>++The <code>transformOrigin</code> is the center of the callout body. The pointer+is not included in the calculation.++## See also++<ul>+ <li>+ <a href="/docs/shapes">+ <code>@remotion/shapes</code>+ </a>+ </li>+ <li>+ <a href="https://github.com/remotion-dev/remotion/blob/main/packages/shapes/src/utils/make-callout.ts">+ Source code for this function+ </a>+ </li>+</ul>diff --git a/packages/docs/docs/standalone.mdx b/packages/docs/docs/standalone.mdxindex 832b84a9e4..2c5f81fffe 100644--- a/packages/docs/docs/standalone.mdx+++ b/packages/docs/docs/standalone.mdx@@ -77,6 +77,7 @@ Unbolded APIs are licensed under the MIT license.**@remotion/shapes**:- [`<Rect />`](/docs/shapes/rect)+- [`<Callout />`](/docs/shapes/callout)- [`<Triangle />`](/docs/shapes/triangle)- [`<Circle />`](/docs/shapes/circle)- [`<Ellipse />`](/docs/shapes/ellipse)@@ -84,6 +85,7 @@ Unbolded APIs are licensed under the MIT license.- [`<Pie />`](/docs/shapes/pie)- [`<Polygon />`](/docs/shapes/polygon)- [makeRect\(\)](/docs/shapes/make-rect)+- [makeCallout\(\)](/docs/shapes/make-callout)- [makeTriangle\(\)](/docs/shapes/make-triangle)- [makeCircle\(\)](/docs/shapes/make-circle)- [makeEllipse\(\)](/docs/shapes/make-ellipse)diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.tsindex abe90d3d59..8eed0d4e37 100644--- a/packages/docs/sidebars.ts+++ b/packages/docs/sidebars.ts@@ -691,6 +691,7 @@ const sidebars: SidebarsConfig = {items: ['shapes/arrow','shapes/rect',+ 'shapes/callout','shapes/triangle','shapes/circle','shapes/ellipse',@@ -700,6 +701,7 @@ const sidebars: SidebarsConfig = {'shapes/polygon','shapes/make-arrow','shapes/make-rect',+ 'shapes/make-callout','shapes/make-triangle','shapes/make-circle','shapes/make-ellipse',diff --git a/packages/shapes/src/components/callout.tsx b/packages/shapes/src/components/callout.tsxnew file mode 100644index 0000000000..90f0adfe5c--- /dev/null+++ b/packages/shapes/src/components/callout.tsx@@ -0,0 +1,100 @@+import React from 'react';+import {Internals} from 'remotion';+import type {MakeCalloutProps} from '../utils/make-callout';+import {makeCallout} from '../utils/make-callout';+import type {AllShapesProps} from './render-svg';+import {RenderSvg} from './render-svg';+import {enumField, makeShapeSchema, numberField} from './schema';++export type CalloutProps = MakeCalloutProps & AllShapesProps;++const calloutSchema = makeShapeSchema({+ width: numberField({+ defaultValue: 500,+ description: 'Width',+ min: 1,+ }),+ height: numberField({+ defaultValue: 200,+ description: 'Height',+ min: 1,+ }),+ pointerLength: numberField({+ defaultValue: 40,+ description: 'Pointer Length',+ min: 1,+ }),+ pointerBaseWidth: numberField({+ defaultValue: 60,+ description: 'Pointer Base Width',+ min: 1,+ }),+ pointerPosition: numberField({+ defaultValue: 0.5,+ description: 'Pointer Position',+ min: 0,+ max: 1,+ step: 0.01,+ }),+ pointerDirection: enumField({+ defaultValue: 'down',+ description: 'Pointer Direction',+ variants: ['up', 'down', 'left', 'right'],+ }),+ cornerRadius: numberField({+ defaultValue: 0,+ description: 'Corner Radius',+ min: 0,+ }),+});++/**+ * @description Renders an SVG element containing a callout shape.+ * @param {Number} width The width of the callout body. Default 500.+ * @param {Number} height The height of the callout body. Default 200.+ * @param {Number} pointerLength The length of the pointer. Default 40.+ * @param {Number} pointerBaseWidth The width of the pointer where it meets the body. Default 60.+ * @param {Number} pointerPosition Position of the pointer along its side, from 0 to 1. Default 0.5.+ * @param {string} pointerDirection The direction the pointer points. Default 'down'.+ * @param {null|Number} edgeRoundness Allows to modify the shape by rounding the edges using bezier curves. Default null.+ * @param {Number} cornerRadius Rounds the corner using an arc. Similar to CSS's border-radius. Cannot be used together with edgeRoundness.+ * @see [Documentation](https://www.remotion.dev/docs/shapes/callout)+ */+const CalloutInner: React.FC<CalloutProps> = ({+ width,+ height,+ pointerLength,+ pointerBaseWidth,+ pointerPosition,+ pointerDirection,+ edgeRoundness,+ cornerRadius,+ ...props+}) => {+ return (+ <RenderSvg+ defaultName="<Callout>"+ documentationLink="https://www.remotion.dev/docs/shapes/callout"+ {...makeCallout({+ width,+ height,+ pointerLength,+ pointerBaseWidth,+ pointerPosition,+ pointerDirection,+ edgeRoundness,+ cornerRadius,+ })}+ {...props}+ />+ );+};++export const Callout = Internals.wrapInSchema({+ Component: CalloutInner,+ componentIdentity: 'dev.remotion.shapes.Callout',+ schema: calloutSchema,+ supportsEffects: true,+}) as React.FC<CalloutProps>;++Internals.addSequenceStackTraces(Callout);diff --git a/packages/shapes/src/index.ts b/packages/shapes/src/index.tsindex 06a420f3e5..05a518eb9a 100644--- a/packages/shapes/src/index.ts+++ b/packages/shapes/src/index.ts@@ -1,5 +1,6 @@export type {Instruction} from '@remotion/paths';export {Arrow, ArrowProps} from './components/arrow';+export {Callout, CalloutProps} from './components/callout';export {Circle, CircleProps} from './components/circle';export {Ellipse, EllipseProps} from './components/ellipse';export {Heart, HeartProps} from './components/heart';@@ -9,6 +10,7 @@ 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 {MakeCalloutProps, makeCallout} from './utils/make-callout';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/callout.test.tsx b/packages/shapes/src/test/callout.test.tsxnew file mode 100644index 0000000000..d096e8d09d--- /dev/null+++ b/packages/shapes/src/test/callout.test.tsx@@ -0,0 +1,19 @@+import {expect, test} from 'bun:test';+import {Callout} from '../components/callout';+import {render} from './test-utils';++test('Should be able to make a callout svg', () => {+ const {container} = render(+ <Callout+ width={300}+ height={120}+ pointerLength={40}+ pointerBaseWidth={60}+ fill="red"+ />,+ );++ expect(container.querySelector('path')?.getAttribute('d')).toEqual(+ 'M 0 0 L 300 0 L 300 120 L 180 120 L 150 160 L 120 120 L 0 120 L 0 0 Z',+ );+});diff --git a/packages/shapes/src/test/make-callout.test.ts b/packages/shapes/src/test/make-callout.test.tsnew file mode 100644index 0000000000..09a45d28a4--- /dev/null+++ b/packages/shapes/src/test/make-callout.test.ts@@ -0,0 +1,111 @@+import {expect, test} from 'bun:test';+import {makeCallout} from '../utils/make-callout';++test('Should be able to make a callout path', () => {+ const callout = makeCallout({+ width: 300,+ height: 120,+ pointerLength: 40,+ pointerBaseWidth: 60,+ pointerPosition: 0.5,+ pointerDirection: 'up',+ });++ expect(callout).toEqual({+ height: 160,+ width: 300,+ path: 'M 0 40 L 120 40 L 150 0 L 180 40 L 300 40 L 300 160 L 0 160 L 0 40 Z',+ transformOrigin: '150 100',+ instructions: [+ {type: 'M', x: 0, y: 40},+ {type: 'L', x: 120, y: 40},+ {type: 'L', x: 150, y: 0},+ {type: 'L', x: 180, y: 40},+ {type: 'L', x: 300, y: 40},+ {type: 'L', x: 300, y: 160},+ {type: 'L', x: 0, y: 160},+ {type: 'L', x: 0, y: 40},+ {type: 'Z'},+ ],+ });+});++test('Should include the pointer in right callout dimensions', () => {+ const callout = makeCallout({+ width: 300,+ height: 120,+ pointerLength: 40,+ pointerBaseWidth: 60,+ pointerDirection: 'right',+ });++ expect(callout.width).toBe(340);+ expect(callout.height).toBe(120);+ expect(callout.transformOrigin).toBe('150 60');+ expect(callout.path).toBe(+ 'M 0 0 L 300 0 L 300 30 L 340 60 L 300 90 L 300 120 L 0 120 L 0 0 Z',+ );+});++test('Should handle callout path with radius', () => {+ const callout = makeCallout({+ pointerLength: 40,+ pointerBaseWidth: 60,+ cornerRadius: 20,+ });++ expect(callout).toEqual({+ height: 240,+ width: 500,+ path: 'M 20 0 L 480 0 a 20 20 0 0 1 20 20 L 500 180 a 20 20 0 0 1 -20 20 L 280 200 L 250 240 L 220 200 L 20 200 a 20 20 0 0 1 -20 -20 L 0 20 a 20 20 0 0 1 20 -20 Z',+ transformOrigin: '250 100',+ instructions: expect.any(Array),+ });+});++test('Should return the transform origin in the center of the body', () => {+ for (const [pointerDirection, transformOrigin] of [+ ['up', '150 100'],+ ['down', '150 60'],+ ['left', '190 60'],+ ['right', '150 60'],+ ] as const) {+ expect(+ makeCallout({+ width: 300,+ height: 120,+ pointerLength: 40,+ pointerDirection,+ }).transformOrigin,+ ).toBe(transformOrigin);+ }+});++test('Should validate callout pointerPosition', () => {+ expect(() =>+ makeCallout({+ pointerPosition: 1.5,+ }),+ ).toThrow(/pointerPosition/);+});++test('Should support edge pointer positions with corner radius', () => {+ const callout = makeCallout({+ pointerPosition: 0,+ cornerRadius: 10,+ });++ expect(callout.path).not.toContain('NaN');+});++test('Should not round the pointer join when left pointer is on the edge', () => {+ const callout = makeCallout({+ pointerDirection: 'left',+ pointerPosition: 0,+ cornerRadius: 40,+ });++ expect(callout.path).toBe(+ 'M 40 0 L 500 0 a 40 40 0 0 1 40 40 L 540 160 a 40 40 0 0 1 -40 40 L 80 200 a 40 40 0 0 1 -40 -40 L 40 30 L 0 0 Z',+ );+});diff --git a/packages/shapes/src/utils/make-callout.ts b/packages/shapes/src/utils/make-callout.tsnew file mode 100644index 0000000000..5b9e61dd9c--- /dev/null+++ b/packages/shapes/src/utils/make-callout.ts@@ -0,0 +1,329 @@+import type {Instruction} from '@remotion/paths';+import {serializeInstructions} from '@remotion/paths';+import {joinPoints} from './join-points';+import type {ShapeInfo} from './shape-info';++type PointerDirection = 'up' | 'down' | 'left' | 'right';+type Point = [number, number];+type CalloutPoint = {+ readonly point: Point;+ readonly round: boolean;+};++export type MakeCalloutProps = {+ width?: number;+ height?: number;+ pointerLength?: number;+ pointerBaseWidth?: number;+ pointerPosition?: number;+ pointerDirection?: PointerDirection;+ edgeRoundness?: number | null;+ cornerRadius?: number;+};++const ensurePositive = (name: string, value: number) => {+ if (typeof value !== 'number' || value <= 0) {+ throw new Error(`"${name}" must be a positive number, got ${value}`);+ }+};++const pointerInterval = ({+ availableLength,+ pointerBaseWidth,+ pointerPosition,+}: {+ readonly availableLength: number;+ readonly pointerBaseWidth: number;+ readonly pointerPosition: number;+}) => {+ const center = availableLength * pointerPosition;+ const half = pointerBaseWidth / 2;++ return {+ center,+ start: Math.max(0, center - half),+ end: Math.min(availableLength, center + half),+ };+};++const areSamePoint = (a: Point, b: Point) => {+ return a[0] === b[0] && a[1] === b[1];+};++const normalizeClosedPoints = (points: CalloutPoint[]) => {+ const deduplicated = points.reduce<CalloutPoint[]>((acc, entry) => {+ const previous = acc[acc.length - 1];++ if (previous && areSamePoint(previous.point, entry.point)) {+ acc[acc.length - 1] = {+ point: previous.point,+ round: previous.round && entry.round,+ };++ return acc;+ }++ return [...acc, entry];+ }, []);++ if (deduplicated.length === 0) {+ return deduplicated;+ }++ const first = deduplicated[0];+ const last = deduplicated[deduplicated.length - 1];++ if (areSamePoint(first.point, last.point)) {+ const [firstEntry, ...rest] = deduplicated;+ const withoutLast = rest.slice(0, -1);+ const mergedFirst = {+ point: firstEntry.point,+ round: firstEntry.round && last.round,+ };++ return [mergedFirst, ...withoutLast, mergedFirst];+ }++ return [...deduplicated, first];+};++const unitDir = (from: Point, to: Point): Point => {+ const dx = to[0] - from[0];+ const dy = to[1] - from[1];+ const len = Math.sqrt(dx * dx + dy * dy);++ if (len === 0) {+ return [0, 0];+ }++ return [dx / len, dy / len];+};++const makeInstructions = ({+ points,+ edgeRoundness,+ cornerRadius,+}: {+ readonly points: CalloutPoint[];+ readonly edgeRoundness: number | null;+ readonly cornerRadius: number;+}): Instruction[] => {+ const rawPoints = points.map((p) => p.point);++ if (edgeRoundness !== null || cornerRadius === 0) {+ return [+ ...joinPoints(rawPoints, {+ edgeRoundness,+ cornerRadius,+ roundCornerStrategy: 'arc',+ }),+ {+ type: 'Z',+ },+ ];+ }++ const uniquePoints = areSamePoint(+ points[0].point,+ points[points.length - 1].point,+ )+ ? points.slice(0, -1)+ : points;+ const firstPoint = uniquePoints[0];+ const startDir = firstPoint.round+ ? unitDir(uniquePoints[0].point, uniquePoints[1].point)+ : null;+ const startPoint: Point = startDir+ ? [+ firstPoint.point[0] + startDir[0] * cornerRadius,+ firstPoint.point[1] + startDir[1] * cornerRadius,+ ]+ : firstPoint.point;+ const instructions: Instruction[] = [+ {type: 'M', x: startPoint[0], y: startPoint[1]},+ ];++ for (let i = 1; i < uniquePoints.length; i++) {+ const current = uniquePoints[i];++ if (!current.round) {+ instructions.push({+ type: 'L',+ x: current.point[0],+ y: current.point[1],+ });+ continue;+ }++ const previous = uniquePoints[i - 1].point;+ const next = uniquePoints[(i + 1) % uniquePoints.length].point;+ const incoming = unitDir(previous, current.point);+ const outgoing = unitDir(current.point, next);+ const arcStart: Point = [+ current.point[0] - incoming[0] * cornerRadius,+ current.point[1] - incoming[1] * cornerRadius,+ ];++ instructions.push(+ {+ type: 'L',+ x: arcStart[0],+ y: arcStart[1],+ },+ {+ type: 'a',+ rx: cornerRadius,+ ry: cornerRadius,+ xAxisRotation: 0,+ dx: incoming[0] * cornerRadius + outgoing[0] * cornerRadius,+ dy: incoming[1] * cornerRadius + outgoing[1] * cornerRadius,+ largeArcFlag: false,+ sweepFlag: true,+ },+ );+ }++ if (firstPoint.round) {+ const previous = uniquePoints[uniquePoints.length - 1].point;+ const incoming = unitDir(previous, firstPoint.point);++ instructions.push(+ {+ type: 'L',+ x: firstPoint.point[0] - incoming[0] * cornerRadius,+ y: firstPoint.point[1] - incoming[1] * cornerRadius,+ },+ {+ type: 'a',+ rx: cornerRadius,+ ry: cornerRadius,+ xAxisRotation: 0,+ dx: incoming[0] * cornerRadius + startDir![0] * cornerRadius,+ dy: incoming[1] * cornerRadius + startDir![1] * cornerRadius,+ largeArcFlag: false,+ sweepFlag: true,+ },+ );+ }++ instructions.push({type: 'Z'});++ return instructions;+};++/**+ * @description Generates an SVG path for a callout shape.+ * @param {Number} width The width of the callout body. Default 500.+ * @param {Number} height The height of the callout body. Default 200.+ * @param {Number} pointerLength The length of the pointer. Default 40.+ * @param {Number} pointerBaseWidth The width of the pointer where it meets the body. Default 60.+ * @param {Number} pointerPosition Position of the pointer along its side, from 0 to 1. Default 0.5.+ * @param {string} pointerDirection The direction the pointer points. Default 'down'.+ * @param {null|Number} edgeRoundness Allows to modify the shape by rounding the edges using bezier curves. Default null.+ * @param {Number} cornerRadius Rounds the corner using an arc. Similar to CSS's border-radius. Cannot be used together with edgeRoundness.+ * @see [Documentation](https://www.remotion.dev/docs/shapes/make-callout)+ */+export const makeCallout = ({+ width = 500,+ height = 200,+ pointerLength = 40,+ pointerBaseWidth = 60,+ pointerPosition = 0.5,+ pointerDirection = 'down',+ edgeRoundness = null,+ cornerRadius = 0,+}: MakeCalloutProps): ShapeInfo => {+ ensurePositive('width', width);+ ensurePositive('height', height);+ ensurePositive('pointerLength', pointerLength);+ ensurePositive('pointerBaseWidth', pointerBaseWidth);++ if (+ typeof pointerPosition !== 'number' ||+ pointerPosition < 0 ||+ pointerPosition > 1+ ) {+ throw new Error(+ `"pointerPosition" must be a number between 0 and 1, got ${pointerPosition}`,+ );+ }++ const horizontalInterval = pointerInterval({+ availableLength: width,+ pointerBaseWidth,+ pointerPosition,+ });+ const verticalInterval = pointerInterval({+ availableLength: height,+ pointerBaseWidth,+ pointerPosition,+ });++ const pointsByDirection: Record<PointerDirection, CalloutPoint[]> = {+ up: [+ {point: [0, pointerLength], round: true},+ {point: [horizontalInterval.start, pointerLength], round: false},+ {point: [horizontalInterval.center, 0], round: false},+ {point: [horizontalInterval.end, pointerLength], round: false},+ {point: [width, pointerLength], round: true},+ {point: [width, height + pointerLength], round: true},+ {point: [0, height + pointerLength], round: true},+ {point: [0, pointerLength], round: true},+ ],+ down: [+ {point: [0, 0], round: true},+ {point: [width, 0], round: true},+ {point: [width, height], round: true},+ {point: [horizontalInterval.end, height], round: false},+ {+ point: [horizontalInterval.center, height + pointerLength],+ round: false,+ },+ {point: [horizontalInterval.start, height], round: false},+ {point: [0, height], round: true},+ {point: [0, 0], round: true},+ ],+ left: [+ {point: [pointerLength, 0], round: true},+ {point: [width + pointerLength, 0], round: true},+ {point: [width + pointerLength, height], round: true},+ {point: [pointerLength, height], round: true},+ {point: [pointerLength, verticalInterval.end], round: false},+ {point: [0, verticalInterval.center], round: false},+ {point: [pointerLength, verticalInterval.start], round: false},+ {point: [pointerLength, 0], round: true},+ ],+ right: [+ {point: [0, 0], round: true},+ {point: [width, 0], round: true},+ {point: [width, verticalInterval.start], round: false},+ {point: [width + pointerLength, verticalInterval.center], round: false},+ {point: [width, verticalInterval.end], round: false},+ {point: [width, height], round: true},+ {point: [0, height], round: true},+ {point: [0, 0], round: true},+ ],+ };+ const points = normalizeClosedPoints(pointsByDirection[pointerDirection]);++ const instructions = makeInstructions({points, edgeRoundness, cornerRadius});+ const path = serializeInstructions(instructions);+ const shapeWidth =+ pointerDirection === 'left' || pointerDirection === 'right'+ ? width + pointerLength+ : width;+ const shapeHeight =+ pointerDirection === 'up' || pointerDirection === 'down'+ ? height + pointerLength+ : height;+ const bodyX = pointerDirection === 'left' ? pointerLength : 0;+ const bodyY = pointerDirection === 'up' ? pointerLength : 0;++ return {+ width: shapeWidth,+ height: shapeHeight,+ instructions,+ path,+ transformOrigin: `${bodyX + width / 2} ${bodyY + height / 2}`,+ };+};