🎥 Make videos programmatically with React
`@remotion/web-renderer`: Clamp oversized border radii (#10668)
Jonny Burgerec4d4f23fe61
diff --git a/packages/web-renderer/src/drawing/border-radius.ts b/packages/web-renderer/src/drawing/border-radius.tsindex 9dc1f9d3e0..ea1307a443 100644--- a/packages/web-renderer/src/drawing/border-radius.ts+++ b/packages/web-renderer/src/drawing/border-radius.ts@@ -58,49 +58,42 @@ function clampBorderRadius({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;- }+ // CSS scales all radii by the same factor when any adjacent pair overlaps.+ // This preserves circular corners for pill shapes such as border-radius: 999px.+ const topHorizontal =+ borderRadius.topLeft.horizontal + borderRadius.topRight.horizontal;+ const bottomHorizontal =+ borderRadius.bottomLeft.horizontal + borderRadius.bottomRight.horizontal;+ const leftVertical =+ borderRadius.topLeft.vertical + borderRadius.bottomLeft.vertical;+ const rightVertical =+ borderRadius.topRight.vertical + borderRadius.bottomRight.vertical;+ const factor = Math.min(+ 1,+ topHorizontal === 0 ? 1 : width / topHorizontal,+ bottomHorizontal === 0 ? 1 : width / bottomHorizontal,+ leftVertical === 0 ? 1 : height / leftVertical,+ rightVertical === 0 ? 1 : height / rightVertical,+ );- return clamped;+ return {+ topLeft: {+ horizontal: borderRadius.topLeft.horizontal * factor,+ vertical: borderRadius.topLeft.vertical * factor,+ },+ topRight: {+ horizontal: borderRadius.topRight.horizontal * factor,+ vertical: borderRadius.topRight.vertical * factor,+ },+ bottomRight: {+ horizontal: borderRadius.bottomRight.horizontal * factor,+ vertical: borderRadius.bottomRight.vertical * factor,+ },+ bottomLeft: {+ horizontal: borderRadius.bottomLeft.horizontal * factor,+ vertical: borderRadius.bottomLeft.vertical * factor,+ },+ };}export function parseBorderRadius({diff --git a/packages/web-renderer/src/test/Root.tsx b/packages/web-renderer/src/test/Root.tsxindex 1b16e93ba3..2c0297b508 100644--- a/packages/web-renderer/src/test/Root.tsx+++ b/packages/web-renderer/src/test/Root.tsx@@ -14,6 +14,7 @@ import {borderRadiusNested} from './fixtures/border-radius-nested';import {borderRadiusNestedOverflowHidden} from './fixtures/border-radius-nested-overflow-hidden';import {borderRadiusNone} from './fixtures/border-radius-none';import {borderRadiusPercentage} from './fixtures/border-radius-percentage';+import {borderRadiusPill} from './fixtures/border-radius-pill';import {borderRadiusSimple} from './fixtures/border-radius-simple';import {boxShadow} from './fixtures/box-shadow';import {clipPathCircle} from './fixtures/clip-path-circle';@@ -173,6 +174,7 @@ export const Root: React.FC = () => {<Composition {...borderRadiusPercentage} /><Composition {...borderRadiusNone} /><Composition {...borderRadiusClamped} />+ <Composition {...borderRadiusPill} /><Composition {...borderRadiusNested} /><Composition {...borderRadiusNestedOverflowHidden} /><Composition {...borderIndividualSides} />diff --git a/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-pill-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-pill-chromium-darwin.pngnew file mode 100644index 0000000000..a8034a22eeBinary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-pill-chromium-darwin.png differdiff --git a/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-pill-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-pill-firefox-darwin.pngnew file mode 100644index 0000000000..652c1c9055Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-pill-firefox-darwin.png differdiff --git a/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-pill-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-pill-webkit-darwin.pngnew file mode 100644index 0000000000..1aadd44ab5Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/draw-border-radius-pill-webkit-darwin.png differdiff --git a/packages/web-renderer/src/test/border-radius.test.tsx b/packages/web-renderer/src/test/border-radius.test.tsxindex dd7812f569..c2ce1b75ca 100644--- a/packages/web-renderer/src/test/border-radius.test.tsx+++ b/packages/web-renderer/src/test/border-radius.test.tsx@@ -9,6 +9,7 @@ import {borderRadiusNested} from './fixtures/border-radius-nested';import {borderRadiusNestedOverflowHidden} from './fixtures/border-radius-nested-overflow-hidden';import {borderRadiusNone} from './fixtures/border-radius-none';import {borderRadiusPercentage} from './fixtures/border-radius-percentage';+import {borderRadiusPill} from './fixtures/border-radius-pill';import {borderRadiusSimple} from './fixtures/border-radius-simple';import {testImage} from './utils';@@ -103,6 +104,19 @@ test('should clamp border radius that exceeds maximum', async () => {await testImage({blob, testId: 'draw-border-radius-clamped'});});+test('should preserve circular corners when clamping a pixel border radius', async () => {+ const blob = await (+ await renderStillOnWeb({+ licenseKey: 'free-license',+ composition: borderRadiusPill,+ frame: 0,+ inputProps: {},+ })+ ).blob({format: 'png'});++ await testImage({blob, testId: 'draw-border-radius-pill'});+});+test('should render nested border radii correctly', async () => {const blob = await (await renderStillOnWeb({diff --git a/packages/web-renderer/src/test/fixtures/border-radius-pill.tsx b/packages/web-renderer/src/test/fixtures/border-radius-pill.tsxnew file mode 100644index 0000000000..2a1f466c8e--- /dev/null+++ b/packages/web-renderer/src/test/fixtures/border-radius-pill.tsx@@ -0,0 +1,32 @@+import {AbsoluteFill} from 'remotion';++const Component: React.FC = () => {+ return (+ <AbsoluteFill+ style={{+ justifyContent: 'center',+ alignItems: 'center',+ backgroundColor: 'white',+ }}+ >+ <div+ style={{+ width: 200,+ height: 50,+ borderRadius: 999,+ backgroundColor: 'teal',+ border: '1px solid #ff6dae',+ }}+ />+ </AbsoluteFill>+ );+};++export const borderRadiusPill = {+ component: Component,+ id: 'border-radius-pill',+ width: 300,+ height: 150,+ 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.tsxindex 9006761dc5..fb78c286a7 100644--- a/packages/web-renderer/src/test/parse-border-radius.test.tsx+++ b/packages/web-renderer/src/test/parse-border-radius.test.tsx@@ -65,10 +65,10 @@ test('should parse four values', () => {height: 200,});expect(result).toEqual({- 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: 7.6923076923076925},+ topLeft: {horizontal: 90.9090909090909, vertical: 181.8181818181818}, // 100% of width, 100% of height (clamped)+ topRight: {horizontal: 9.09090909090909, vertical: 9.09090909090909},+ bottomRight: {horizontal: 29.09090909090909, vertical: 29.09090909090909},+ bottomLeft: {horizontal: 7.2727272727272725, vertical: 7.2727272727272725},});});@@ -79,10 +79,10 @@ test('should parse mixed percentages and pixels', () => {height: 200,});expect(result).toEqual({- 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)+ topLeft: {horizontal: 5.555555555555555, vertical: 5.555555555555555},+ topRight: {horizontal: 83.33333333333334, vertical: 166.66666666666669}, // 150% of width, 150% of height (clamped)+ bottomRight: {horizontal: 16.666666666666668, vertical: 16.666666666666668},+ bottomLeft: {horizontal: 83.33333333333334, vertical: 166.66666666666669}, // 150% of width, 150% of height (clamped)});});@@ -226,3 +226,17 @@ test('should parse large percentage values that will need clamping', () => {bottomLeft: {horizontal: 50, vertical: 50},});});++test('should proportionally clamp a large pixel radius on a wide box', () => {+ const result = parseBorderRadius({+ borderRadius: '999px',+ width: 200,+ height: 50,+ });+ expect(result).toEqual({+ topLeft: {horizontal: 25, vertical: 25},+ topRight: {horizontal: 25, vertical: 25},+ bottomRight: {horizontal: 25, vertical: 25},+ bottomLeft: {horizontal: 25, vertical: 25},+ });+});