Skip to content

πŸŽ₯ Make videos programmatically with React

refine cornerRadius

Jonny Burgerf24d3bc1550d
diff --git a/packages/shapes/src/test/make-arrow.test.ts b/packages/shapes/src/test/make-arrow.test.ts
index f78f2d87b7..bbb4aacb6b 100644
--- a/packages/shapes/src/test/make-arrow.test.ts
+++ b/packages/shapes/src/test/make-arrow.test.ts
@@ -12,7 +12,7 @@ test('Should be able to make a right-pointing arrow path', () => {
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',
+ path: 'M 0 20 L 120 20 L 120 0 L 200 40 L 120 80 L 120 60 L 0 60 Z',
transformOrigin: '100 40',
instructions: [
{type: 'M', x: 0, y: 20},
@@ -22,7 +22,6 @@ test('Should be able to make a right-pointing arrow path', () => {
{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'},
],
});
@@ -41,7 +40,7 @@ test('Should be able to make a left-pointing arrow path', () => {
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',
+ 'M 200 20 L 80 20 L 80 0 L 0 40 L 80 80 L 80 60 L 200 60 Z',
);
});
@@ -58,7 +57,7 @@ test('Should be able to make an upward-pointing arrow path', () => {
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',
+ 'M 20 200 L 60 200 L 60 80 L 80 80 L 40 0 L 0 80 L 20 80 Z',
);
});
@@ -75,7 +74,7 @@ test('Should be able to make a downward-pointing arrow path', () => {
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',
+ 'M 20 0 L 60 0 L 60 120 L 80 120 L 40 200 L 0 120 L 20 120 Z',
);
});
diff --git a/packages/shapes/src/utils/make-arrow.ts b/packages/shapes/src/utils/make-arrow.ts
index d1c316a151..de206ce309 100644
--- a/packages/shapes/src/utils/make-arrow.ts
+++ b/packages/shapes/src/utils/make-arrow.ts
@@ -1,6 +1,5 @@
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';
@@ -14,6 +13,91 @@ export type MakeArrowProps = {
cornerRadius?: number;
};
+const unitDir = (
+ from: [number, number],
+ to: [number, number],
+): [number, number] => {
+ const dx = to[0] - from[0];
+ const dy = to[1] - from[1];
+ const len = Math.sqrt(dx * dx + dy * dy);
+ return len === 0 ? [0, 0] : [dx / len, dy / len];
+};
+
+const buildArrowPath = (
+ points: [number, number][],
+ roundedIndices: Set<number>,
+ cornerRadius: number,
+): Instruction[] => {
+ const n = points.length;
+
+ if (cornerRadius === 0) {
+ return [
+ {type: 'M', x: points[0][0], y: points[0][1]},
+ ...points
+ .slice(1)
+ .map(([x, y]): Instruction => ({type: 'L' as const, x, y})),
+ {type: 'Z'},
+ ];
+ }
+
+ // Corner 0 is always rounded β€” start at its depart point
+ const d0 = unitDir(points[0], points[1]);
+ const startX = points[0][0] + d0[0] * cornerRadius;
+ const startY = points[0][1] + d0[1] * cornerRadius;
+ const instrs: Instruction[] = [{type: 'M', x: startX, y: startY}];
+
+ for (let i = 1; i < n; i++) {
+ const curr = points[i];
+ if (roundedIndices.has(i)) {
+ const prev = points[i - 1];
+ const next = points[(i + 1) % n];
+ const dIn = unitDir(prev, curr);
+ const dOut = unitDir(curr, next);
+
+ instrs.push(
+ {
+ type: 'L',
+ x: curr[0] - dIn[0] * cornerRadius,
+ y: curr[1] - dIn[1] * cornerRadius,
+ },
+ {
+ type: 'C',
+ cp1x: curr[0],
+ cp1y: curr[1],
+ cp2x: curr[0],
+ cp2y: curr[1],
+ x: curr[0] + dOut[0] * cornerRadius,
+ y: curr[1] + dOut[1] * cornerRadius,
+ },
+ );
+ } else {
+ instrs.push({type: 'L', x: curr[0], y: curr[1]});
+ }
+ }
+
+ // Close back to corner 0 (rounded)
+ const dIn0 = unitDir(points[n - 1], points[0]);
+ instrs.push(
+ {
+ type: 'L',
+ x: points[0][0] - dIn0[0] * cornerRadius,
+ y: points[0][1] - dIn0[1] * cornerRadius,
+ },
+ {
+ type: 'C',
+ cp1x: points[0][0],
+ cp1y: points[0][1],
+ cp2x: points[0][0],
+ cp2y: points[0][1],
+ x: startX,
+ y: startY,
+ },
+ {type: 'Z'},
+ );
+
+ return instrs;
+};
+
/**
* @description Generates an SVG path for an arrow shape.
* @param {Number} length The total length of the arrow along its direction axis. Default 300.
@@ -106,14 +190,16 @@ export const makeArrow = ({
height = length;
}
- const instructions: Instruction[] = [
- ...joinPoints([...points, points[0]], {
- edgeRoundness: null,
- cornerRadius,
- roundCornerStrategy: 'bezier',
- }),
- {type: 'Z'},
- ];
+ // Round the back corners and all 3 arrowhead tips, but not the inner
+ // shaft-to-head junctions so the shaft edges stay straight.
+ // right/left: back = 0,6; head tips = 2,3,4; inner junctions (skip) = 1,5
+ // up/down: back = 0,1; head tips = 3,4,5; inner junctions (skip) = 2,6
+ const roundedIndices =
+ direction === 'right' || direction === 'left'
+ ? new Set([0, 2, 3, 4, 6])
+ : new Set([0, 1, 3, 4, 5]);
+
+ const instructions = buildArrowPath(points, roundedIndices, cornerRadius);
const path = serializeInstructions(instructions);