Skip to content

🎥 Make videos programmatically with React

`@remotion/shapes`: Add Spark shape (#8516)

Jonny Burger31f06e0c782f
diff --git a/packages/docs/components/demos/Spark.tsx b/packages/docs/components/demos/Spark.tsx
new file mode 100644
index 0000000000..eba2a66b7f
--- /dev/null
+++ b/packages/docs/components/demos/Spark.tsx
@@ -0,0 +1,28 @@
+import {Spark} from '@remotion/shapes';
+import React from 'react';
+import {AbsoluteFill} from 'remotion';
+
+export const SparkDemo: React.FC<{
+ readonly darkMode: boolean;
+ readonly width: number;
+ readonly height: number;
+ readonly edgeRoundness: number;
+ readonly cornerRadius: number;
+}> = ({darkMode, width, height, edgeRoundness, cornerRadius}) => {
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ }}
+ >
+ <Spark
+ width={width}
+ height={height}
+ edgeRoundness={edgeRoundness}
+ cornerRadius={cornerRadius}
+ fill={darkMode ? 'white' : 'var(--ifm-link-color)'}
+ />
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/docs/components/demos/index.tsx b/packages/docs/components/demos/index.tsx
index aeb1899b5f..a65b5c6d70 100644
--- a/packages/docs/components/demos/index.tsx
+++ b/packages/docs/components/demos/index.tsx
@@ -48,6 +48,7 @@ import {
skewDemo,
slidePresentationDemo,
slidePresentationDemoLongThreshold,
+ sparkDemo,
springDampingDemo,
springDemo,
starburstDemo,
@@ -113,6 +114,7 @@ const demos: DemoType[] = [
circleDemo,
ellipseDemo,
heartDemo,
+ sparkDemo,
starDemo,
polygonDemo,
pieDemo,
diff --git a/packages/docs/components/demos/types.ts b/packages/docs/components/demos/types.ts
index 014496a5b4..b56bf9945e 100644
--- a/packages/docs/components/demos/types.ts
+++ b/packages/docs/components/demos/types.ts
@@ -35,6 +35,7 @@ import {RectDemo} from './Rect';
import {RippleDocsDemo} from './RippleDemo';
import {RoundedTextBox} from './RoundedTextBox';
import {ShaderDemoComp} from './ShaderDemo';
+import {SparkDemo} from './Spark';
import {SpringDemo} from './Spring';
import {StarDemo} from './Star';
import {StarburstDemoComp} from './StarburstDemo';
@@ -724,6 +725,56 @@ export const polygonDemo: DemoType = {
],
};
+export const sparkDemo: DemoType = {
+ comp: SparkDemo,
+ compHeight: 720,
+ compWidth: 1280,
+ durationInFrames: 150,
+ fps: 30,
+ id: 'spark',
+ autoPlay: false,
+ controls: false,
+ logLevel: 'info',
+ options: [
+ {
+ default: 100,
+ max: 600,
+ step: 5,
+ min: 1,
+ type: 'numeric',
+ name: 'width',
+ optional: 'no',
+ },
+ {
+ default: 140,
+ max: 600,
+ step: 5,
+ min: 1,
+ type: 'numeric',
+ name: 'height',
+ optional: 'no',
+ },
+ {
+ name: 'edgeRoundness',
+ default: 1,
+ max: 2,
+ min: 0,
+ step: 0.01,
+ type: 'numeric',
+ optional: 'no',
+ },
+ {
+ name: 'cornerRadius',
+ default: 0,
+ max: 120,
+ min: 0,
+ step: 1,
+ type: 'numeric',
+ optional: 'no',
+ },
+ ],
+};
+
export const starDemo: DemoType = {
comp: StarDemo,
compHeight: 720,
diff --git a/packages/docs/components/shapes/shape-component-drag-data.ts b/packages/docs/components/shapes/shape-component-drag-data.ts
index 6d08762514..db41b0289c 100644
--- a/packages/docs/components/shapes/shape-component-drag-data.ts
+++ b/packages/docs/components/shapes/shape-component-drag-data.ts
@@ -62,6 +62,13 @@ const shapeDefaultProps: Record<ShapeName, readonly ComponentProp[]> = {
{name: 'cornerRadius', value: 0},
{name: 'fill', value: '#000000'},
],
+ Spark: [
+ {name: 'width', value: 100},
+ {name: 'height', value: 140},
+ {name: 'edgeRoundness', value: 1},
+ {name: 'cornerRadius', value: 0},
+ {name: 'fill', value: '#000000'},
+ ],
Star: [
{name: 'points', value: 5},
{name: 'innerRadius', value: 50},
@@ -86,6 +93,7 @@ const shapeNameByDemoId: Partial<Record<string, ShapeName>> = {
pie: 'Pie',
polygon: 'Polygon',
rect: 'Rect',
+ spark: 'Spark',
star: 'Star',
triangle: 'Triangle',
};
@@ -120,6 +128,7 @@ const shapeDemoPropNames: Record<ShapeName, readonly string[]> = {
Pie: ['radius', 'progress', 'closePath', 'counterClockwise', 'rotation'],
Polygon: ['points', 'radius', 'cornerRadius', 'edgeRoundness'],
Rect: ['width', 'height', 'cornerRadius', 'edgeRoundness'],
+ Spark: ['width', 'height', 'edgeRoundness', 'cornerRadius'],
Star: [
'points',
'innerRadius',
diff --git a/packages/docs/components/shapes/shape-drag-info.ts b/packages/docs/components/shapes/shape-drag-info.ts
index baf1e4d23f..7a1882ddb4 100644
--- a/packages/docs/components/shapes/shape-drag-info.ts
+++ b/packages/docs/components/shapes/shape-drag-info.ts
@@ -7,6 +7,7 @@ import {
makePie,
makePolygon,
makeRect,
+ makeSpark,
makeStar,
makeTriangle,
} from '@remotion/shapes';
@@ -29,6 +30,7 @@ const shapeNames: readonly ShapeName[] = [
'Pie',
'Polygon',
'Rect',
+ 'Spark',
'Star',
'Triangle',
];
@@ -195,6 +197,16 @@ export const getShapeDragInfo = (
}),
props,
);
+ case 'Spark':
+ return withFill(
+ makeSpark({
+ width: getNumber(props, 'width'),
+ height: getNumber(props, 'height'),
+ edgeRoundness: getNumber(props, 'edgeRoundness'),
+ cornerRadius: getNumber(props, 'cornerRadius'),
+ }),
+ props,
+ );
case 'Star':
return withFill(
makeStar({
diff --git a/packages/docs/components/shapes/shapes-info.tsx b/packages/docs/components/shapes/shapes-info.tsx
index 2952fefa41..0c9dcbcd20 100644
--- a/packages/docs/components/shapes/shapes-info.tsx
+++ b/packages/docs/components/shapes/shapes-info.tsx
@@ -7,6 +7,7 @@ import {
makePie,
makePolygon,
makeRect,
+ makeSpark,
makeStar,
makeTriangle,
} from '@remotion/shapes';
@@ -34,6 +35,7 @@ export type ShapeName =
| 'Pie'
| 'Polygon'
| 'Rect'
+ | 'Spark'
| 'Star'
| 'Triangle';
@@ -308,6 +310,37 @@ export const shapeComponents: ShapeComponent[] = [
},
],
},
+ {
+ shape: 'Spark',
+ fn: makeSpark,
+ params: [
+ {
+ name: 'width',
+ type: 'number',
+ description: 'The width of the spark.',
+ hiddenFromList: false,
+ },
+ {
+ name: 'height',
+ type: 'number',
+ description: 'The height of the spark.',
+ hiddenFromList: false,
+ },
+ {
+ name: 'edgeRoundness',
+ type: 'number',
+ description:
+ 'Controls the inward curvature of the edges between the four points. Default 1.',
+ hiddenFromList: false,
+ },
+ {
+ name: 'cornerRadius',
+ type: 'number',
+ description: 'Rounds the four points of the spark. Default 0.',
+ hiddenFromList: false,
+ },
+ ],
+ },
{
shape: 'Polygon',
fn: makePolygon,
diff --git a/packages/docs/docs/shapes/make-spark.mdx b/packages/docs/docs/shapes/make-spark.mdx
new file mode 100644
index 0000000000..c71eedf659
--- /dev/null
+++ b/packages/docs/docs/shapes/make-spark.mdx
@@ -0,0 +1,42 @@
+---
+image: /generated/articles-docs-shapes-make-spark.png
+title: makeSpark()
+crumb: "@remotion/shapes"
+---
+
+import {MakeShapeSeeAlso, ShapeOptions, MakeShapeReturnType} from "../../components/shapes/shapes-info"
+
+_Part of the [`@remotion/shapes`](/docs/shapes) package._
+
+Generates a spark SVG path.
+
+## Example
+
+```tsx twoslash title="spark.ts"
+import { makeSpark } from "@remotion/shapes";
+
+const { path, width, height, transformOrigin, instructions } = makeSpark({
+ width: 240,
+ height: 336,
+ edgeRoundness: 1,
+ cornerRadius: 0,
+});
+
+console.log(path); // M 120 0 C 120 92.78383797157332 173.72583002030478 168 240 168 C 173.72583002030478 168 120 243.21616202842668 120 336 C 120 243.21616202842668 66.27416997969523 168 0 168 C 66.27416997969523 168 120 92.78383797157332 120 0 Z
+console.log(width); // 240
+console.log(height); // 336
+console.log(transformOrigin); // '120 168'
+console.log(instructions); // '[{type: "M"}, ...]'
+```
+
+## Arguments
+
+<ShapeOptions shape="spark"/>
+
+## Return type
+
+<MakeShapeReturnType shape="spark"/>
+
+## See also
+
+<MakeShapeSeeAlso shape="spark"/>
diff --git a/packages/docs/docs/shapes/spark.mdx b/packages/docs/docs/shapes/spark.mdx
new file mode 100644
index 0000000000..de852077b5
--- /dev/null
+++ b/packages/docs/docs/shapes/spark.mdx
@@ -0,0 +1,50 @@
+---
+image: /generated/articles-docs-shapes-spark.png
+title: <Spark />
+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 four-point spark.
+
+## Explorer
+
+<Demo type="spark" />
+
+## Example
+
+```tsx twoslash title="src/Spark.tsx"
+import { Spark } from "@remotion/shapes";
+import { AbsoluteFill } from "remotion";
+
+export const MyComposition = () => {
+ return (
+ <AbsoluteFill
+ style={{
+ backgroundColor: "white",
+ justifyContent: "center",
+ alignItems: "center",
+ }}
+ >
+ <Spark
+ width={240}
+ height={336}
+ edgeRoundness={1}
+ cornerRadius={0}
+ fill="#1f2a44"
+ />
+ </AbsoluteFill>
+ );
+};
+```
+
+## Props
+
+<ShapeOptions shape="spark" all />
+
+## See also
+
+<ShapeSeeAlso shape="spark" />
diff --git a/packages/docs/docs/shapes/table-of-contents.tsx b/packages/docs/docs/shapes/table-of-contents.tsx
index 3ebdfe3ad4..dc3ad1b52c 100644
--- a/packages/docs/docs/shapes/table-of-contents.tsx
+++ b/packages/docs/docs/shapes/table-of-contents.tsx
@@ -7,6 +7,7 @@ import {
makePie,
makePolygon,
makeRect,
+ makeSpark,
makeStar,
makeTriangle,
} from '@remotion/shapes';
@@ -166,6 +167,13 @@ const makeDefaultShapeInfo = (shape: ShapeName): ShapeInfo => {
height: getNumber(props, 'height'),
cornerRadius: getNumber(props, 'cornerRadius'),
});
+ case 'Spark':
+ return makeSpark({
+ width: getNumber(props, 'width'),
+ height: getNumber(props, 'height'),
+ edgeRoundness: getNumber(props, 'edgeRoundness'),
+ cornerRadius: getNumber(props, 'cornerRadius'),
+ });
case 'Star':
return makeStar({
points: getNumber(props, 'points'),
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index 1c5d55e82f..1ee124c6b5 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -705,6 +705,7 @@ const sidebars: SidebarsConfig = {
'shapes/circle',
'shapes/ellipse',
'shapes/heart',
+ 'shapes/spark',
'shapes/star',
'shapes/pie',
'shapes/polygon',
@@ -715,6 +716,7 @@ const sidebars: SidebarsConfig = {
'shapes/make-circle',
'shapes/make-ellipse',
'shapes/make-heart',
+ 'shapes/make-spark',
'shapes/make-star',
'shapes/make-pie',
'shapes/make-polygon',
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 2613e01112..996518b2c2 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -6231,6 +6231,15 @@ export const articles = [
noAi: false,
slug: 'shapes/make-rect',
},
+ {
+ id: 'shapes/make-spark',
+ title: 'makeSpark()',
+ relativePath: 'docs/shapes/make-spark.mdx',
+ compId: 'articles-docs-shapes-make-spark',
+ crumb: '@remotion/shapes',
+ noAi: false,
+ slug: 'shapes/make-spark',
+ },
{
id: 'shapes/make-star',
title: 'makeStar()',
@@ -6276,6 +6285,15 @@ export const articles = [
noAi: false,
slug: 'shapes/rect',
},
+ {
+ id: 'shapes/spark',
+ title: '<Spark />',
+ relativePath: 'docs/shapes/spark.mdx',
+ compId: 'articles-docs-shapes-spark',
+ crumb: '@remotion/shapes',
+ noAi: false,
+ slug: 'shapes/spark',
+ },
{
id: 'shapes/star',
title: '<Star />',
diff --git a/packages/docs/src/test/shape-component-drag-data.test.ts b/packages/docs/src/test/shape-component-drag-data.test.ts
index 606d3ee5de..a84bbc492f 100644
--- a/packages/docs/src/test/shape-component-drag-data.test.ts
+++ b/packages/docs/src/test/shape-component-drag-data.test.ts
@@ -14,6 +14,7 @@ const shapes: ShapeName[] = [
'Pie',
'Polygon',
'Rect',
+ 'Spark',
'Star',
'Triangle',
];
diff --git a/packages/docs/static/generated/articles-docs-shapes-make-spark.png b/packages/docs/static/generated/articles-docs-shapes-make-spark.png
new file mode 100644
index 0000000000..c0a927dc96
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-shapes-make-spark.png differ
diff --git a/packages/docs/static/generated/articles-docs-shapes-spark.png b/packages/docs/static/generated/articles-docs-shapes-spark.png
new file mode 100644
index 0000000000..0fe13521ec
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-shapes-spark.png differ
diff --git a/packages/shapes/src/components/spark.tsx b/packages/shapes/src/components/spark.tsx
new file mode 100644
index 0000000000..d9fa026b10
--- /dev/null
+++ b/packages/shapes/src/components/spark.tsx
@@ -0,0 +1,67 @@
+import React from 'react';
+import {Internals, Interactive} from 'remotion';
+import type {MakeSparkProps} from '../utils/make-spark';
+import {makeSpark} from '../utils/make-spark';
+import type {AllShapesProps} from './render-svg';
+import {RenderSvg} from './render-svg';
+import {makeShapeSchema, numberField} from './schema';
+
+export type SparkProps = MakeSparkProps & AllShapesProps;
+
+const sparkSchema = makeShapeSchema({
+ width: numberField({
+ defaultValue: 100,
+ description: 'Width',
+ min: 0,
+ }),
+ height: numberField({
+ defaultValue: 140,
+ description: 'Height',
+ min: 0,
+ }),
+ edgeRoundness: numberField({
+ defaultValue: 1,
+ description: 'Edge Roundness',
+ min: 0,
+ step: 0.01,
+ }),
+ cornerRadius: numberField({
+ defaultValue: 0,
+ description: 'Corner Radius',
+ min: 0,
+ }),
+});
+
+/**
+ * @description Renders an SVG element containing a spark.
+ * @param {Number} width The width of the spark.
+ * @param {Number} height The height of the spark.
+ * @param {Number} edgeRoundness Controls the inward curvature between the points. Default 1.
+ * @param {Number} cornerRadius Rounds the four points. Default 0.
+ * @see [Documentation](https://www.remotion.dev/docs/shapes/spark)
+ */
+const SparkInner: React.FC<SparkProps> = ({
+ width,
+ height,
+ edgeRoundness,
+ cornerRadius,
+ ...props
+}) => {
+ return (
+ <RenderSvg
+ defaultName="<Spark>"
+ documentationLink="https://www.remotion.dev/docs/shapes/spark"
+ {...makeSpark({width, height, edgeRoundness, cornerRadius})}
+ {...props}
+ />
+ );
+};
+
+export const Spark = Interactive.withSchema({
+ Component: SparkInner,
+ componentIdentity: 'dev.remotion.shapes.Spark',
+ schema: sparkSchema,
+ supportsEffects: true,
+}) as React.FC<SparkProps>;
+
+Internals.addSequenceStackTraces(Spark);
diff --git a/packages/shapes/src/index.ts b/packages/shapes/src/index.ts
index 05a518eb9a..a1dbd2de1d 100644
--- a/packages/shapes/src/index.ts
+++ b/packages/shapes/src/index.ts
@@ -7,6 +7,7 @@ export {Heart, HeartProps} from './components/heart';
export {Pie, PieProps} from './components/pie';
export {Polygon, PolygonProps} from './components/polygon';
export {Rect, RectProps} from './components/rect';
+export {Spark, SparkProps} from './components/spark';
export {Star, StarProps} from './components/star';
export {Triangle, TriangleProps} from './components/triangle';
export {MakeArrowProps, makeArrow} from './utils/make-arrow';
@@ -17,5 +18,6 @@ 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';
+export {MakeSparkProps, makeSpark} from './utils/make-spark';
export {MakeStarProps, makeStar} from './utils/make-star';
export {MakeTriangleProps, makeTriangle} from './utils/make-triangle';
diff --git a/packages/shapes/src/test/make-spark.test.ts b/packages/shapes/src/test/make-spark.test.ts
new file mode 100644
index 0000000000..e70868a569
--- /dev/null
+++ b/packages/shapes/src/test/make-spark.test.ts
@@ -0,0 +1,80 @@
+import {expect, test} from 'bun:test';
+import {makeSpark} from '../utils/make-spark';
+
+test('Should be able to make a spark path', () => {
+ const spark = makeSpark({
+ width: 200,
+ height: 120,
+ });
+
+ expect(spark.width).toEqual(200);
+ expect(spark.height).toEqual(120);
+ expect(spark.transformOrigin).toEqual('100 60');
+ expect(spark.instructions.length).toEqual(6);
+ expect(spark.instructions[0]).toEqual({type: 'M', x: 100, y: 0});
+ const firstCurve = spark.instructions[1];
+ expect(firstCurve.type).toBe('C');
+ if (firstCurve.type !== 'C') {
+ throw new Error('Expected first curve to be cubic');
+ }
+
+ expect(firstCurve.cp1x).toBe(100);
+ expect(firstCurve.cp1y).toBeCloseTo(33.13708498984761);
+ expect(firstCurve.cp2x).toBeCloseTo(144.77152501692066);
+ expect(firstCurve.cp2y).toBe(60);
+ expect(firstCurve.x).toBe(200);
+ expect(firstCurve.y).toBe(60);
+ expect(spark.instructions[5]).toEqual({type: 'Z'});
+ expect(spark.path).toContain('C 100 33.137084989847615');
+});
+
+test('Should support rounded points', () => {
+ const spark = makeSpark({
+ width: 200,
+ height: 120,
+ edgeRoundness: 0.8,
+ cornerRadius: 12,
+ });
+
+ expect(spark.instructions.length).toEqual(10);
+ expect(spark.instructions[0]).toEqual({type: 'M', x: 112, y: 12});
+ expect(spark.path).toContain('C 204 48 204 72 188 72');
+ expect(spark.path).not.toContain('NaN');
+});
+
+test('Should transition smoothly from edges into rounded points', () => {
+ const spark = makeSpark({
+ width: 200,
+ height: 120,
+ edgeRoundness: 0.8,
+ cornerRadius: 12,
+ });
+
+ const topRightEdge = spark.instructions[1];
+ const rightCap = spark.instructions[2];
+ const rightBottomEdge = spark.instructions[3];
+
+ if (
+ topRightEdge.type !== 'C' ||
+ rightCap.type !== 'C' ||
+ rightBottomEdge.type !== 'C'
+ ) {
+ throw new Error('Expected cubic instructions');
+ }
+
+ expect(topRightEdge.cp2y).toBe(topRightEdge.y);
+ expect(rightCap.cp1y).toBe(topRightEdge.y);
+ expect(rightCap.cp2y).toBe(rightCap.y);
+ expect(rightBottomEdge.cp1y).toBe(rightCap.y);
+});
+
+test('Should cap corner radius at half the shortest side', () => {
+ const spark = makeSpark({
+ width: 100,
+ height: 40,
+ cornerRadius: 100,
+ });
+
+ expect(spark.instructions[0]).toEqual({type: 'M', x: 70, y: 20});
+ expect(spark.path).not.toContain('NaN');
+});
diff --git a/packages/shapes/src/test/spark.test.tsx b/packages/shapes/src/test/spark.test.tsx
new file mode 100644
index 0000000000..abba5592a5
--- /dev/null
+++ b/packages/shapes/src/test/spark.test.tsx
@@ -0,0 +1,13 @@
+import {expect, test} from 'bun:test';
+import {Spark} from '../components/spark';
+import {render} from './test-utils';
+
+test('Should be able to make a spark svg', () => {
+ const {container} = render(
+ <Spark width={200} height={120} edgeRoundness={1} cornerRadius={0} />,
+ );
+
+ expect(container.querySelector('path')?.getAttribute('d')).toContain(
+ 'C 100 33.137084989847615',
+ );
+});
diff --git a/packages/shapes/src/utils/make-spark.ts b/packages/shapes/src/utils/make-spark.ts
new file mode 100644
index 0000000000..056864374d
--- /dev/null
+++ b/packages/shapes/src/utils/make-spark.ts
@@ -0,0 +1,250 @@
+import type {Instruction} from '@remotion/paths';
+import {serializeInstructions} from '@remotion/paths';
+import type {ShapeInfo} from './shape-info';
+
+export type MakeSparkProps = {
+ width: number;
+ height: number;
+ edgeRoundness?: number;
+ cornerRadius?: number;
+};
+
+type Point = readonly [number, number];
+type CubicInstruction = Extract<Instruction, {type: 'C'}>;
+type CapDirection = 'top' | 'right' | 'bottom' | 'left';
+
+const KAPPA = 0.5522847498307936;
+const CAP_HANDLE = 4 / 3;
+
+const curve = ({
+ to,
+ cp1,
+ cp2,
+}: {
+ readonly to: Point;
+ readonly cp1: Point;
+ readonly cp2: Point;
+}): CubicInstruction => {
+ return {
+ type: 'C',
+ cp1x: cp1[0],
+ cp1y: cp1[1],
+ cp2x: cp2[0],
+ cp2y: cp2[1],
+ x: to[0],
+ y: to[1],
+ };
+};
+
+const cap = ({
+ from,
+ to,
+ radius,
+ direction,
+}: {
+ readonly from: Point;
+ readonly to: Point;
+ readonly radius: number;
+ readonly direction: CapDirection;
+}): CubicInstruction => {
+ if (direction === 'top') {
+ return curve({
+ to,
+ cp1: [from[0], from[1] - radius * CAP_HANDLE],
+ cp2: [to[0], to[1] - radius * CAP_HANDLE],
+ });
+ }
+
+ if (direction === 'right') {
+ return curve({
+ to,
+ cp1: [from[0] + radius * CAP_HANDLE, from[1]],
+ cp2: [to[0] + radius * CAP_HANDLE, to[1]],
+ });
+ }
+
+ if (direction === 'bottom') {
+ return curve({
+ to,
+ cp1: [from[0], from[1] + radius * CAP_HANDLE],
+ cp2: [to[0], to[1] + radius * CAP_HANDLE],
+ });
+ }
+
+ return curve({
+ to,
+ cp1: [from[0] - radius * CAP_HANDLE, from[1]],
+ cp2: [to[0] - radius * CAP_HANDLE, to[1]],
+ });
+};
+
+const edge = ({
+ from,
+ to,
+ edgeRoundness,
+ direction,
+}: {
+ readonly from: Point;
+ readonly to: Point;
+ readonly edgeRoundness: number;
+ readonly direction: 'top-right' | 'right-bottom' | 'bottom-left' | 'left-top';
+}): CubicInstruction => {
+ const dx = to[0] - from[0];
+ const dy = to[1] - from[1];
+ const handleX = Math.abs(dx) * KAPPA * edgeRoundness;
+ const handleY = Math.abs(dy) * KAPPA * edgeRoundness;
+
+ if (direction === 'top-right') {
+ return curve({
+ to,
+ cp1: [from[0], from[1] + handleY],
+ cp2: [to[0] - handleX, to[1]],
+ });
+ }
+
+ if (direction === 'right-bottom') {
+ return curve({
+ to,
+ cp1: [from[0] - handleX, from[1]],
+ cp2: [to[0], to[1] - handleY],
+ });
+ }
+
+ if (direction === 'bottom-left') {
+ return curve({
+ to,
+ cp1: [from[0], from[1] - handleY],
+ cp2: [to[0] + handleX, to[1]],
+ });
+ }
+
+ return curve({
+ to,
+ cp1: [from[0] + handleX, from[1]],
+ cp2: [to[0], to[1] + handleY],
+ });
+};
+
+const clampCornerRadius = ({
+ cornerRadius,
+ width,
+ height,
+}: {
+ readonly cornerRadius: number;
+ readonly width: number;
+ readonly height: number;
+}) => {
+ return Math.min(Math.max(cornerRadius, 0), width / 2, height / 2);
+};
+
+/**
+ * @description Generates a spark SVG path.
+ * @param {Number} width The width of the spark.
+ * @param {Number} height The height of the spark.
+ * @param {Number} edgeRoundness Controls the inward curvature between the points. Default 1.
+ * @param {Number} cornerRadius Rounds the four points. Default 0.
+ * @see [Documentation](https://www.remotion.dev/docs/shapes/make-spark)
+ */
+export const makeSpark = ({
+ width,
+ height,
+ edgeRoundness = 1,
+ cornerRadius = 0,
+}: MakeSparkProps): ShapeInfo => {
+ const centerX = width / 2;
+ const centerY = height / 2;
+ const radius = clampCornerRadius({cornerRadius, width, height});
+
+ const top: Point = [centerX, 0];
+ const right: Point = [width, centerY];
+ const bottom: Point = [centerX, height];
+ const left: Point = [0, centerY];
+
+ const topRight: Point = radius === 0 ? top : [centerX + radius, radius];
+ const rightTop: Point =
+ radius === 0 ? right : [width - radius, centerY - radius];
+ const rightBottom: Point =
+ radius === 0 ? right : [width - radius, centerY + radius];
+ const bottomRight: Point =
+ radius === 0 ? bottom : [centerX + radius, height - radius];
+ const bottomLeft: Point =
+ radius === 0 ? bottom : [centerX - radius, height - radius];
+ const leftBottom: Point = radius === 0 ? left : [radius, centerY + radius];
+ const leftTop: Point = radius === 0 ? left : [radius, centerY - radius];
+ const topLeft: Point = radius === 0 ? top : [centerX - radius, radius];
+
+ const instructions: Instruction[] = [
+ {
+ type: 'M',
+ x: topRight[0],
+ y: topRight[1],
+ },
+ edge({
+ from: topRight,
+ to: rightTop,
+ edgeRoundness,
+ direction: 'top-right',
+ }),
+ ];
+
+ if (radius > 0) {
+ instructions.push(
+ cap({from: rightTop, to: rightBottom, radius, direction: 'right'}),
+ );
+ }
+
+ instructions.push(
+ edge({
+ from: rightBottom,
+ to: bottomRight,
+ edgeRoundness,
+ direction: 'right-bottom',
+ }),
+ );
+
+ if (radius > 0) {
+ instructions.push(
+ cap({from: bottomRight, to: bottomLeft, radius, direction: 'bottom'}),
+ );
+ }
+
+ instructions.push(
+ edge({
+ from: bottomLeft,
+ to: leftBottom,
+ edgeRoundness,
+ direction: 'bottom-left',
+ }),
+ );
+
+ if (radius > 0) {
+ instructions.push(
+ cap({from: leftBottom, to: leftTop, radius, direction: 'left'}),
+ );
+ }
+
+ instructions.push(
+ edge({
+ from: leftTop,
+ to: topLeft,
+ edgeRoundness,
+ direction: 'left-top',
+ }),
+ );
+
+ if (radius > 0) {
+ instructions.push(
+ cap({from: topLeft, to: topRight, radius, direction: 'top'}),
+ );
+ }
+
+ instructions.push({type: 'Z'});
+
+ return {
+ path: serializeInstructions(instructions),
+ width,
+ height,
+ transformOrigin: `${centerX} ${centerY}`,
+ instructions,
+ };
+};