Skip to content

🎥 Make videos programmatically with React

clamp border radii

Jonny Burgere718ae5b43db
diff --git a/packages/web-renderer/src/border-radius.ts b/packages/web-renderer/src/border-radius.ts
index 2e2144fe92..1c7c82e640 100644
--- a/packages/web-renderer/src/border-radius.ts
+++ b/packages/web-renderer/src/border-radius.ts
@@ -46,6 +46,60 @@ function expandShorthand(values: string[]): [string, string, string, string] {
return [values[0], values[1], values[2], values[3]];
}
+function clampBorderRadius({
+ borderRadius,
+ width,
+ height,
+}: {
+ borderRadius: BorderRadiusCorners;
+ width: number;
+ height: number;
+}): BorderRadiusCorners {
+ // According to CSS spec, if the sum of border radii on adjacent corners
+ // exceeds the length of the edge, they should be proportionally reduced
+ const clamped = {
+ topLeft: {...borderRadius.topLeft},
+ topRight: {...borderRadius.topRight},
+ bottomRight: {...borderRadius.bottomRight},
+ bottomLeft: {...borderRadius.bottomLeft},
+ };
+
+ // Check top edge
+ const topSum = clamped.topLeft.horizontal + clamped.topRight.horizontal;
+ if (topSum > width) {
+ const factor = width / topSum;
+ clamped.topLeft.horizontal *= factor;
+ clamped.topRight.horizontal *= factor;
+ }
+
+ // Check right edge
+ const rightSum = clamped.topRight.vertical + clamped.bottomRight.vertical;
+ if (rightSum > height) {
+ const factor = height / rightSum;
+ clamped.topRight.vertical *= factor;
+ clamped.bottomRight.vertical *= factor;
+ }
+
+ // Check bottom edge
+ const bottomSum =
+ clamped.bottomRight.horizontal + clamped.bottomLeft.horizontal;
+ if (bottomSum > width) {
+ const factor = width / bottomSum;
+ clamped.bottomRight.horizontal *= factor;
+ clamped.bottomLeft.horizontal *= factor;
+ }
+
+ // Check left edge
+ const leftSum = clamped.bottomLeft.vertical + clamped.topLeft.vertical;
+ if (leftSum > height) {
+ const factor = height / leftSum;
+ clamped.bottomLeft.vertical *= factor;
+ clamped.topLeft.vertical *= factor;
+ }
+
+ return clamped;
+}
+
export function parseBorderRadius({
borderRadius,
width,
@@ -73,24 +127,28 @@ export function parseBorderRadius({
const [vTopLeft, vTopRight, vBottomRight, vBottomLeft] =
expandShorthand(verticalValues);
- return {
- topLeft: {
- horizontal: parseValue({value: hTopLeft, reference: width}),
- vertical: parseValue({value: vTopLeft, reference: height}),
- },
- topRight: {
- horizontal: parseValue({value: hTopRight, reference: width}),
- vertical: parseValue({value: vTopRight, reference: height}),
+ return clampBorderRadius({
+ borderRadius: {
+ topLeft: {
+ horizontal: parseValue({value: hTopLeft, reference: width}),
+ vertical: parseValue({value: vTopLeft, reference: height}),
+ },
+ topRight: {
+ horizontal: parseValue({value: hTopRight, reference: width}),
+ vertical: parseValue({value: vTopRight, reference: height}),
+ },
+ bottomRight: {
+ horizontal: parseValue({value: hBottomRight, reference: width}),
+ vertical: parseValue({value: vBottomRight, reference: height}),
+ },
+ bottomLeft: {
+ horizontal: parseValue({value: hBottomLeft, reference: width}),
+ vertical: parseValue({value: vBottomLeft, reference: height}),
+ },
},
- bottomRight: {
- horizontal: parseValue({value: hBottomRight, reference: width}),
- vertical: parseValue({value: vBottomRight, reference: height}),
- },
- bottomLeft: {
- horizontal: parseValue({value: hBottomLeft, reference: width}),
- vertical: parseValue({value: vBottomLeft, reference: height}),
- },
- };
+ width,
+ height,
+ });
}
export function setBorderRadius({
diff --git a/packages/web-renderer/src/test/Root.tsx b/packages/web-renderer/src/test/Root.tsx
index 8b50b2838d..f5dfc07acf 100644
--- a/packages/web-renderer/src/test/Root.tsx
+++ b/packages/web-renderer/src/test/Root.tsx
@@ -3,6 +3,7 @@ import React from 'react';
import {Composition} from 'remotion';
import {accumulatedTransforms} from './fixtures/accumulated-transforms';
import {borderRadius} from './fixtures/border-radius';
+import {borderRadiusClamped} from './fixtures/border-radius-clamped';
import {borderRadiusDifferent} from './fixtures/border-radius-different';
import {borderRadiusElliptical} from './fixtures/border-radius-elliptical';
import {borderRadiusNone} from './fixtures/border-radius-none';
@@ -43,6 +44,7 @@ export const Root: React.FC = () => {
<Composition {...borderRadiusDifferent} />
<Composition {...borderRadiusPercentage} />
<Composition {...borderRadiusNone} />
+ <Composition {...borderRadiusClamped} />
</>
);
};
diff --git a/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-clamped-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-clamped-chromium-darwin.png
new file mode 100644
index 0000000000..e40d6dacf5
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-clamped-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-clamped-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-clamped-firefox-darwin.png
new file mode 100644
index 0000000000..06265cd6af
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-clamped-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-clamped-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-clamped-webkit-darwin.png
new file mode 100644
index 0000000000..779ea774ee
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-clamped-webkit-darwin.png differ
diff --git a/packages/web-renderer/src/test/border-radius.test.tsx b/packages/web-renderer/src/test/border-radius.test.tsx
index 0d78e781dd..e0e92b69a1 100644
--- a/packages/web-renderer/src/test/border-radius.test.tsx
+++ b/packages/web-renderer/src/test/border-radius.test.tsx
@@ -1,6 +1,7 @@
import {test} from 'vitest';
import {renderStillOnWeb} from '../render-still-on-web';
import {borderRadius} from './fixtures/border-radius';
+import {borderRadiusClamped} from './fixtures/border-radius-clamped';
import {borderRadiusDifferent} from './fixtures/border-radius-different';
import {borderRadiusElliptical} from './fixtures/border-radius-elliptical';
import {borderRadiusNone} from './fixtures/border-radius-none';
@@ -73,3 +74,14 @@ test('should draw image with no border radius', async () => {
await testImage({blob, testId: 'draw-border-radius-none'});
});
+
+test('should clamp border radius that exceeds maximum', async () => {
+ const blob = await renderStillOnWeb({
+ composition: borderRadiusClamped,
+ frame: 0,
+ inputProps: {},
+ imageFormat: 'png',
+ });
+
+ await testImage({blob, testId: 'draw-border-radius-clamped'});
+});
diff --git a/packages/web-renderer/src/test/fixtures/border-radius-clamped.tsx b/packages/web-renderer/src/test/fixtures/border-radius-clamped.tsx
new file mode 100644
index 0000000000..d61ef29861
--- /dev/null
+++ b/packages/web-renderer/src/test/fixtures/border-radius-clamped.tsx
@@ -0,0 +1,42 @@
+import {useLayoutEffect, useRef} from 'react';
+import {AbsoluteFill} from 'remotion';
+
+const Component: React.FC = () => {
+ const ref = useRef<HTMLCanvasElement>(null);
+
+ useLayoutEffect(() => {
+ if (!ref.current) return;
+ const ctx = ref.current.getContext('2d');
+ if (!ctx) return;
+ ctx.fillStyle = 'teal';
+ ctx.fillRect(0, 0, 100, 100);
+ }, []);
+
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ backgroundColor: 'white',
+ }}
+ >
+ <canvas
+ ref={ref}
+ style={{
+ borderRadius: '150%',
+ }}
+ width={100}
+ height={100}
+ />
+ </AbsoluteFill>
+ );
+};
+
+export const borderRadiusClamped = {
+ component: Component,
+ id: 'border-radius-clamped',
+ width: 200,
+ height: 200,
+ fps: 25,
+ durationInFrames: 1,
+} as const;
diff --git a/packages/web-renderer/src/test/parse-border-radius.test.tsx b/packages/web-renderer/src/test/parse-border-radius.test.tsx
index c1a0274f42..b2b765b326 100644
--- a/packages/web-renderer/src/test/parse-border-radius.test.tsx
+++ b/packages/web-renderer/src/test/parse-border-radius.test.tsx
@@ -64,10 +64,10 @@ test('should parse four values', () => {
height: 200,
});
expect(result).toEqual({
- topLeft: {horizontal: 100, vertical: 200}, // 100% of width, 100% of height
- topRight: {horizontal: 10, vertical: 10},
+ topLeft: {horizontal: 90.9090909090909, vertical: 192.30769230769232}, // 100% of width, 100% of height (clamped)
+ topRight: {horizontal: 9.09090909090909, vertical: 10},
bottomRight: {horizontal: 32, vertical: 32},
- bottomLeft: {horizontal: 8, vertical: 8},
+ bottomLeft: {horizontal: 8, vertical: 7.6923076923076925},
});
});
@@ -78,10 +78,10 @@ test('should parse mixed percentages and pixels', () => {
height: 200,
});
expect(result).toEqual({
- topLeft: {horizontal: 10, vertical: 10},
- topRight: {horizontal: 150, vertical: 300}, // 150% of width, 150% of height
- bottomRight: {horizontal: 30, vertical: 30},
- bottomLeft: {horizontal: 150, vertical: 300}, // 150% of width, 150% of height
+ topLeft: {horizontal: 6.25, vertical: 6.451612903225806},
+ topRight: {horizontal: 93.75, vertical: 181.8181818181818}, // 150% of width, 150% of height (clamped)
+ bottomRight: {horizontal: 16.666666666666668, vertical: 18.181818181818183},
+ bottomLeft: {horizontal: 83.33333333333334, vertical: 193.54838709677418}, // 150% of width, 150% of height (clamped)
});
});
@@ -190,10 +190,10 @@ test('should handle percentages over 100%', () => {
height: 200,
});
expect(result).toEqual({
- topLeft: {horizontal: 150, vertical: 300}, // 150% of width, 150% of height
- topRight: {horizontal: 200, vertical: 400}, // 200% of width, 200% of height
- bottomRight: {horizontal: 150, vertical: 300},
- bottomLeft: {horizontal: 200, vertical: 400},
+ topLeft: {horizontal: 42.857142857142854, vertical: 85.71428571428571}, // 150% of width, 150% of height (clamped)
+ topRight: {horizontal: 57.14285714285714, vertical: 114.28571428571428}, // 200% of width, 200% of height (clamped)
+ bottomRight: {horizontal: 42.857142857142854, vertical: 85.71428571428571},
+ bottomLeft: {horizontal: 57.14285714285714, vertical: 114.28571428571428},
});
});
@@ -210,3 +210,18 @@ test('should handle mixed units with slash and percentages', () => {
bottomLeft: {horizontal: 10, vertical: 30},
});
});
+
+test('should parse large percentage values that will need clamping', () => {
+ const result = parseBorderRadius({
+ borderRadius: '150%',
+ width: 100,
+ height: 100,
+ });
+ // Clamping now happens during parsing
+ expect(result).toEqual({
+ topLeft: {horizontal: 50, vertical: 50},
+ topRight: {horizontal: 50, vertical: 50},
+ bottomRight: {horizontal: 50, vertical: 50},
+ bottomLeft: {horizontal: 50, vertical: 50},
+ });
+});