Skip to content

🎥 Make videos programmatically with React

nice

Jonny Burger73fb7df6bb99
diff --git a/packages/web-renderer/src/border-radius.ts b/packages/web-renderer/src/border-radius.ts
new file mode 100644
index 0000000000..2e2144fe92
--- /dev/null
+++ b/packages/web-renderer/src/border-radius.ts
@@ -0,0 +1,212 @@
+export type BorderRadiusCorners = {
+ topLeft: {horizontal: number; vertical: number};
+ topRight: {horizontal: number; vertical: number};
+ bottomRight: {horizontal: number; vertical: number};
+ bottomLeft: {horizontal: number; vertical: number};
+};
+
+function parseValue({
+ value,
+ reference,
+}: {
+ value: string;
+ reference: number;
+}): number {
+ value = value.trim();
+ if (value.endsWith('%')) {
+ const percentage = parseFloat(value);
+ return (percentage / 100) * reference;
+ }
+
+ if (value.endsWith('px')) {
+ return parseFloat(value);
+ }
+
+ // If no unit, assume pixels
+ return parseFloat(value);
+}
+
+function expandShorthand(values: string[]): [string, string, string, string] {
+ if (values.length === 1) {
+ // All corners the same
+ return [values[0], values[0], values[0], values[0]];
+ }
+
+ if (values.length === 2) {
+ // [0] = top-left & bottom-right, [1] = top-right & bottom-left
+ return [values[0], values[1], values[0], values[1]];
+ }
+
+ if (values.length === 3) {
+ // [0] = top-left, [1] = top-right & bottom-left, [2] = bottom-right
+ return [values[0], values[1], values[2], values[1]];
+ }
+
+ // 4 values: top-left, top-right, bottom-right, bottom-left
+ return [values[0], values[1], values[2], values[3]];
+}
+
+export function parseBorderRadius({
+ borderRadius,
+ width,
+ height,
+}: {
+ borderRadius: string;
+ width: number;
+ height: number;
+}): BorderRadiusCorners {
+ // Split by '/' to separate horizontal and vertical radii
+ const parts = borderRadius.split('/').map((part) => part.trim());
+
+ const horizontalPart = parts[0];
+ const verticalPart = parts[1];
+
+ // Split each part into individual values
+ const horizontalValues = horizontalPart.split(/\s+/).filter((v) => v);
+ const verticalValues = verticalPart
+ ? verticalPart.split(/\s+/).filter((v) => v)
+ : horizontalValues; // If no '/', use horizontal values for vertical
+
+ // Expand shorthand to 4 values
+ const [hTopLeft, hTopRight, hBottomRight, hBottomLeft] =
+ expandShorthand(horizontalValues);
+ 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}),
+ },
+ 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}),
+ },
+ };
+}
+
+export function setBorderRadius({
+ ctx,
+ x,
+ y,
+ width,
+ height,
+ borderRadius,
+}: {
+ ctx: OffscreenCanvasRenderingContext2D;
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+ borderRadius: BorderRadiusCorners;
+}) {
+ if (
+ borderRadius.topLeft.horizontal === 0 &&
+ borderRadius.topLeft.vertical === 0 &&
+ borderRadius.topRight.horizontal === 0 &&
+ borderRadius.topRight.vertical === 0 &&
+ borderRadius.bottomRight.horizontal === 0 &&
+ borderRadius.bottomRight.vertical === 0 &&
+ borderRadius.bottomLeft.horizontal === 0 &&
+ borderRadius.bottomLeft.vertical === 0
+ ) {
+ return () => {};
+ }
+
+ ctx.save();
+ ctx.beginPath();
+
+ // Start at top-left corner, after the horizontal radius
+ ctx.moveTo(x + borderRadius.topLeft.horizontal, y);
+
+ // Top edge to top-right corner
+ ctx.lineTo(x + width - borderRadius.topRight.horizontal, y);
+
+ // Top-right corner (elliptical arc)
+ if (
+ borderRadius.topRight.horizontal > 0 ||
+ borderRadius.topRight.vertical > 0
+ ) {
+ ctx.ellipse(
+ x + width - borderRadius.topRight.horizontal,
+ y + borderRadius.topRight.vertical,
+ borderRadius.topRight.horizontal,
+ borderRadius.topRight.vertical,
+ 0,
+ -Math.PI / 2,
+ 0,
+ );
+ }
+
+ // Right edge to bottom-right corner
+ ctx.lineTo(x + width, y + height - borderRadius.bottomRight.vertical);
+
+ // Bottom-right corner (elliptical arc)
+ if (
+ borderRadius.bottomRight.horizontal > 0 ||
+ borderRadius.bottomRight.vertical > 0
+ ) {
+ ctx.ellipse(
+ x + width - borderRadius.bottomRight.horizontal,
+ y + height - borderRadius.bottomRight.vertical,
+ borderRadius.bottomRight.horizontal,
+ borderRadius.bottomRight.vertical,
+ 0,
+ 0,
+ Math.PI / 2,
+ );
+ }
+
+ // Bottom edge to bottom-left corner
+ ctx.lineTo(x + borderRadius.bottomLeft.horizontal, y + height);
+
+ // Bottom-left corner (elliptical arc)
+ if (
+ borderRadius.bottomLeft.horizontal > 0 ||
+ borderRadius.bottomLeft.vertical > 0
+ ) {
+ ctx.ellipse(
+ x + borderRadius.bottomLeft.horizontal,
+ y + height - borderRadius.bottomLeft.vertical,
+ borderRadius.bottomLeft.horizontal,
+ borderRadius.bottomLeft.vertical,
+ 0,
+ Math.PI / 2,
+ Math.PI,
+ );
+ }
+
+ // Left edge to top-left corner
+ ctx.lineTo(x, y + borderRadius.topLeft.vertical);
+
+ // Top-left corner (elliptical arc)
+ if (
+ borderRadius.topLeft.horizontal > 0 ||
+ borderRadius.topLeft.vertical > 0
+ ) {
+ ctx.ellipse(
+ x + borderRadius.topLeft.horizontal,
+ y + borderRadius.topLeft.vertical,
+ borderRadius.topLeft.horizontal,
+ borderRadius.topLeft.vertical,
+ 0,
+ Math.PI,
+ (Math.PI * 3) / 2,
+ );
+ }
+
+ ctx.closePath();
+ ctx.clip();
+
+ return () => {
+ ctx.restore();
+ };
+}
diff --git a/packages/web-renderer/src/calculate-transforms.ts b/packages/web-renderer/src/calculate-transforms.ts
index 2b430de2e2..df662ee6b9 100644
--- a/packages/web-renderer/src/calculate-transforms.ts
+++ b/packages/web-renderer/src/calculate-transforms.ts
@@ -1,3 +1,4 @@
+import {parseBorderRadius} from './border-radius';
import {parseTransformOrigin} from './parse-transform-origin';
type Transform = {
@@ -33,8 +34,15 @@ export const calculateTransforms = (element: HTMLElement | SVGSVGElement) => {
let parent: HTMLElement | SVGSVGElement | null = element;
const transforms: Transform[] = [];
const toReset: (() => void)[] = [];
+
+ let borderRadius = '';
+
while (parent) {
const computedStyle = getComputedStyle(parent);
+ if (parent === element) {
+ borderRadius = computedStyle.borderRadius;
+ }
+
if (
(computedStyle.transform && computedStyle.transform !== 'none') ||
parent === element
@@ -95,5 +103,10 @@ export const calculateTransforms = (element: HTMLElement | SVGSVGElement) => {
}
},
nativeTransformOrigin,
+ borderRadius: parseBorderRadius({
+ borderRadius,
+ width: dimensions.width,
+ height: dimensions.height,
+ }),
};
};
diff --git a/packages/web-renderer/src/compose-canvas.ts b/packages/web-renderer/src/compose-canvas.ts
index 24fd007df5..a02cf307a2 100644
--- a/packages/web-renderer/src/compose-canvas.ts
+++ b/packages/web-renderer/src/compose-canvas.ts
@@ -1,3 +1,4 @@
+import {setBorderRadius} from './border-radius';
import {calculateTransforms} from './calculate-transforms';
import {turnSvgIntoDrawable} from './compose-svg';
@@ -5,7 +6,8 @@ export const composeCanvas = async (
canvas: HTMLCanvasElement | HTMLImageElement | SVGSVGElement,
context: OffscreenCanvasRenderingContext2D,
) => {
- const {totalMatrix, reset, dimensions} = calculateTransforms(canvas);
+ const {totalMatrix, reset, dimensions, borderRadius} =
+ calculateTransforms(canvas);
context.setTransform(totalMatrix);
const drawable =
@@ -13,6 +15,15 @@ export const composeCanvas = async (
? await turnSvgIntoDrawable(canvas)
: canvas;
+ const finish = setBorderRadius({
+ ctx: context,
+ x: dimensions.left,
+ y: dimensions.top,
+ width: dimensions.width,
+ height: dimensions.height,
+ borderRadius,
+ });
+
context.drawImage(
drawable,
dimensions.left,
@@ -20,6 +31,8 @@ export const composeCanvas = async (
dimensions.width,
dimensions.height,
);
+
+ finish();
context.setTransform(new DOMMatrix());
reset();
diff --git a/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/border-radius-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/border-radius-chromium-darwin.png
new file mode 100644
index 0000000000..5863b4b556
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/border-radius.test.tsx/border-radius-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/draw-border-radius.test.tsx b/packages/web-renderer/src/test/draw-border-radius.test.tsx
new file mode 100644
index 0000000000..14e4070be8
--- /dev/null
+++ b/packages/web-renderer/src/test/draw-border-radius.test.tsx
@@ -0,0 +1,341 @@
+import React, {useLayoutEffect, useRef} from 'react';
+import {AbsoluteFill} from 'remotion';
+import {test} from 'vitest';
+import {parseBorderRadius, setBorderRadius} from '../border-radius';
+import {renderStillOnWeb} from '../render-still-on-web';
+import {testImage} from './utils';
+
+test('should draw image with simple border radius', async () => {
+ const Component: React.FC = () => {
+ const ref = useRef<HTMLCanvasElement>(null);
+
+ useLayoutEffect(() => {
+ if (!ref.current) return;
+ const ctx = ref.current.getContext('2d');
+ if (!ctx) return;
+
+ // Create a simple colored rectangle as our "image"
+ const tempCanvas = document.createElement('canvas');
+ tempCanvas.width = 100;
+ tempCanvas.height = 100;
+ const tempCtx = tempCanvas.getContext('2d');
+ if (!tempCtx) return;
+ tempCtx.fillStyle = 'red';
+ tempCtx.fillRect(0, 0, 100, 100);
+
+ const borderRadius = parseBorderRadius({
+ borderRadius: '20px',
+ width: 100,
+ height: 100,
+ });
+
+ setBorderRadius({
+ ctx,
+ img: tempCanvas,
+ x: 0,
+ y: 0,
+ width: 100,
+ height: 100,
+ borderRadius,
+ });
+ }, []);
+
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ backgroundColor: 'white',
+ }}
+ >
+ <canvas ref={ref} width={100} height={100} />
+ </AbsoluteFill>
+ );
+ };
+
+ const blob = await renderStillOnWeb({
+ composition: {
+ component: Component,
+ id: 'draw-border-radius-simple',
+ width: 200,
+ height: 200,
+ fps: 25,
+ durationInFrames: 1,
+ calculateMetadata: () => Promise.resolve({}),
+ },
+ frame: 0,
+ inputProps: {},
+ imageFormat: 'png',
+ });
+
+ await testImage({blob, testId: 'draw-border-radius-simple'});
+});
+
+test('should draw image with elliptical border radius', async () => {
+ const Component: React.FC = () => {
+ const ref = useRef<HTMLCanvasElement>(null);
+
+ useLayoutEffect(() => {
+ if (!ref.current) return;
+ const ctx = ref.current.getContext('2d');
+ if (!ctx) return;
+
+ // Create a gradient as our "image"
+ const tempCanvas = document.createElement('canvas');
+ tempCanvas.width = 100;
+ tempCanvas.height = 100;
+ const tempCtx = tempCanvas.getContext('2d');
+ if (!tempCtx) return;
+ const gradient = tempCtx.createLinearGradient(0, 0, 100, 100);
+ gradient.addColorStop(0, 'blue');
+ gradient.addColorStop(1, 'green');
+ tempCtx.fillStyle = gradient;
+ tempCtx.fillRect(0, 0, 100, 100);
+
+ const borderRadius = parseBorderRadius({
+ borderRadius: '50px / 25px',
+ width: 100,
+ height: 100,
+ });
+
+ setBorderRadius({
+ ctx,
+ img: tempCanvas,
+ x: 0,
+ y: 0,
+ width: 100,
+ height: 100,
+ borderRadius,
+ });
+ }, []);
+
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ backgroundColor: 'white',
+ }}
+ >
+ <canvas ref={ref} width={100} height={100} />
+ </AbsoluteFill>
+ );
+ };
+
+ const blob = await renderStillOnWeb({
+ composition: {
+ component: Component,
+ id: 'draw-border-radius-elliptical',
+ width: 200,
+ height: 200,
+ fps: 25,
+ durationInFrames: 1,
+ calculateMetadata: () => Promise.resolve({}),
+ },
+ frame: 0,
+ inputProps: {},
+ imageFormat: 'png',
+ });
+
+ await testImage({blob, testId: 'draw-border-radius-elliptical'});
+});
+
+test('should draw image with different corner radii', async () => {
+ const Component: React.FC = () => {
+ const ref = useRef<HTMLCanvasElement>(null);
+
+ useLayoutEffect(() => {
+ if (!ref.current) return;
+ const ctx = ref.current.getContext('2d');
+ if (!ctx) return;
+
+ // Create a pattern
+ const tempCanvas = document.createElement('canvas');
+ tempCanvas.width = 100;
+ tempCanvas.height = 100;
+ const tempCtx = tempCanvas.getContext('2d');
+ if (!tempCtx) return;
+ tempCtx.fillStyle = 'purple';
+ tempCtx.fillRect(0, 0, 100, 100);
+ tempCtx.fillStyle = 'yellow';
+ tempCtx.fillRect(25, 25, 50, 50);
+
+ const borderRadius = parseBorderRadius({
+ borderRadius: '10px 20px 30px 40px',
+ width: 100,
+ height: 100,
+ });
+
+ setBorderRadius({
+ ctx,
+ img: tempCanvas,
+ x: 0,
+ y: 0,
+ width: 100,
+ height: 100,
+ borderRadius,
+ });
+ }, []);
+
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ backgroundColor: 'white',
+ }}
+ >
+ <canvas ref={ref} width={100} height={100} />
+ </AbsoluteFill>
+ );
+ };
+
+ const blob = await renderStillOnWeb({
+ composition: {
+ component: Component,
+ id: 'draw-border-radius-different',
+ width: 200,
+ height: 200,
+ fps: 25,
+ durationInFrames: 1,
+ calculateMetadata: () => Promise.resolve({}),
+ },
+ frame: 0,
+ inputProps: {},
+ imageFormat: 'png',
+ });
+
+ await testImage({blob, testId: 'draw-border-radius-different'});
+});
+
+test('should draw image with percentage border radius', async () => {
+ const Component: React.FC = () => {
+ const ref = useRef<HTMLCanvasElement>(null);
+
+ useLayoutEffect(() => {
+ if (!ref.current) return;
+ const ctx = ref.current.getContext('2d');
+ if (!ctx) return;
+
+ // Create a simple colored rectangle
+ const tempCanvas = document.createElement('canvas');
+ tempCanvas.width = 100;
+ tempCanvas.height = 100;
+ const tempCtx = tempCanvas.getContext('2d');
+ if (!tempCtx) return;
+ tempCtx.fillStyle = 'orange';
+ tempCtx.fillRect(0, 0, 100, 100);
+
+ const borderRadius = parseBorderRadius({
+ borderRadius: '50%',
+ width: 100,
+ height: 100,
+ });
+
+ setBorderRadius({
+ ctx,
+ img: tempCanvas,
+ x: 0,
+ y: 0,
+ width: 100,
+ height: 100,
+ borderRadius,
+ });
+ }, []);
+
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ backgroundColor: 'white',
+ }}
+ >
+ <canvas ref={ref} width={100} height={100} />
+ </AbsoluteFill>
+ );
+ };
+
+ const blob = await renderStillOnWeb({
+ composition: {
+ component: Component,
+ id: 'draw-border-radius-percentage',
+ width: 200,
+ height: 200,
+ fps: 25,
+ durationInFrames: 1,
+ calculateMetadata: () => Promise.resolve({}),
+ },
+ frame: 0,
+ inputProps: {},
+ imageFormat: 'png',
+ });
+
+ await testImage({blob, testId: 'draw-border-radius-percentage'});
+});
+
+test('should draw image with no border radius', async () => {
+ const Component: React.FC = () => {
+ const ref = useRef<HTMLCanvasElement>(null);
+
+ useLayoutEffect(() => {
+ if (!ref.current) return;
+ const ctx = ref.current.getContext('2d');
+ if (!ctx) return;
+
+ // Create a simple colored rectangle
+ const tempCanvas = document.createElement('canvas');
+ tempCanvas.width = 100;
+ tempCanvas.height = 100;
+ const tempCtx = tempCanvas.getContext('2d');
+ if (!tempCtx) return;
+ tempCtx.fillStyle = 'cyan';
+ tempCtx.fillRect(0, 0, 100, 100);
+
+ const borderRadius = parseBorderRadius({
+ borderRadius: '0px',
+ width: 100,
+ height: 100,
+ });
+
+ setBorderRadius({
+ ctx,
+ img: tempCanvas,
+ x: 0,
+ y: 0,
+ width: 100,
+ height: 100,
+ borderRadius,
+ });
+ }, []);
+
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ backgroundColor: 'white',
+ }}
+ >
+ <canvas ref={ref} width={100} height={100} />
+ </AbsoluteFill>
+ );
+ };
+
+ const blob = await renderStillOnWeb({
+ composition: {
+ component: Component,
+ id: 'draw-border-radius-none',
+ width: 200,
+ height: 200,
+ fps: 25,
+ durationInFrames: 1,
+ calculateMetadata: () => Promise.resolve({}),
+ },
+ frame: 0,
+ inputProps: {},
+ imageFormat: 'png',
+ });
+
+ await testImage({blob, testId: 'draw-border-radius-none'});
+});
diff --git a/packages/web-renderer/src/test/fixtures/border-radius.tsx b/packages/web-renderer/src/test/fixtures/border-radius.tsx
new file mode 100644
index 0000000000..9af6981b4c
--- /dev/null
+++ b/packages/web-renderer/src/test/fixtures/border-radius.tsx
@@ -0,0 +1,43 @@
+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 = 'red';
+ ctx.fillRect(0, 0, 100, 100);
+ }, []);
+
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ }}
+ >
+ <canvas
+ ref={ref}
+ style={{
+ width: '50%',
+ height: '50%',
+ borderRadius: '10px / 20px',
+ }}
+ width={100}
+ height={100}
+ />
+ </AbsoluteFill>
+ );
+};
+
+export const borderRadius = {
+ component: Component,
+ id: 'border-radius',
+ width: 200,
+ height: 200,
+ fps: 30,
+ durationInFrames: 100,
+} 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
new file mode 100644
index 0000000000..c1a0274f42
--- /dev/null
+++ b/packages/web-renderer/src/test/parse-border-radius.test.tsx
@@ -0,0 +1,212 @@
+import {expect, test} from 'vitest';
+import {parseBorderRadius} from '../border-radius';
+
+test('should parse single pixel value', () => {
+ const result = parseBorderRadius({
+ borderRadius: '10px',
+ width: 100,
+ height: 200,
+ });
+ expect(result).toEqual({
+ topLeft: {horizontal: 10, vertical: 10},
+ topRight: {horizontal: 10, vertical: 10},
+ bottomRight: {horizontal: 10, vertical: 10},
+ bottomLeft: {horizontal: 10, vertical: 10},
+ });
+});
+
+test('should parse zero value', () => {
+ const result = parseBorderRadius({
+ borderRadius: '0px',
+ width: 100,
+ height: 200,
+ });
+ expect(result).toEqual({
+ topLeft: {horizontal: 0, vertical: 0},
+ topRight: {horizontal: 0, vertical: 0},
+ bottomRight: {horizontal: 0, vertical: 0},
+ bottomLeft: {horizontal: 0, vertical: 0},
+ });
+});
+
+test('should parse two values', () => {
+ const result = parseBorderRadius({
+ borderRadius: '10px 20px',
+ width: 100,
+ height: 200,
+ });
+ expect(result).toEqual({
+ topLeft: {horizontal: 10, vertical: 10},
+ topRight: {horizontal: 20, vertical: 20},
+ bottomRight: {horizontal: 10, vertical: 10},
+ bottomLeft: {horizontal: 20, vertical: 20},
+ });
+});
+
+test('should parse three values', () => {
+ const result = parseBorderRadius({
+ borderRadius: '10px 20px 30px',
+ width: 100,
+ height: 200,
+ });
+ expect(result).toEqual({
+ topLeft: {horizontal: 10, vertical: 10},
+ topRight: {horizontal: 20, vertical: 20},
+ bottomRight: {horizontal: 30, vertical: 30},
+ bottomLeft: {horizontal: 20, vertical: 20},
+ });
+});
+
+test('should parse four values', () => {
+ const result = parseBorderRadius({
+ borderRadius: '100% 10px 32px 8px',
+ width: 100,
+ height: 200,
+ });
+ expect(result).toEqual({
+ topLeft: {horizontal: 100, vertical: 200}, // 100% of width, 100% of height
+ topRight: {horizontal: 10, vertical: 10},
+ bottomRight: {horizontal: 32, vertical: 32},
+ bottomLeft: {horizontal: 8, vertical: 8},
+ });
+});
+
+test('should parse mixed percentages and pixels', () => {
+ const result = parseBorderRadius({
+ borderRadius: '10px 150% 30px',
+ width: 100,
+ 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
+ });
+});
+
+test('should parse horizontal and vertical radii with slash', () => {
+ const result = parseBorderRadius({
+ borderRadius: '10px 5% / 20px 30px',
+ width: 100,
+ height: 200,
+ });
+ expect(result).toEqual({
+ topLeft: {horizontal: 10, vertical: 20},
+ topRight: {horizontal: 5, vertical: 30},
+ bottomRight: {horizontal: 10, vertical: 20},
+ bottomLeft: {horizontal: 5, vertical: 30},
+ });
+});
+
+test('should resolve horizontal percentages using width', () => {
+ const result = parseBorderRadius({
+ borderRadius: '50%',
+ width: 100,
+ height: 200,
+ });
+ expect(result).toEqual({
+ topLeft: {horizontal: 50, vertical: 100}, // 50% of width, 50% of height
+ topRight: {horizontal: 50, vertical: 100},
+ bottomRight: {horizontal: 50, vertical: 100},
+ bottomLeft: {horizontal: 50, vertical: 100},
+ });
+});
+
+test('should resolve vertical percentages using height', () => {
+ const result = parseBorderRadius({
+ borderRadius: '10px / 25%',
+ width: 100,
+ height: 200,
+ });
+ expect(result).toEqual({
+ topLeft: {horizontal: 10, vertical: 50},
+ topRight: {horizontal: 10, vertical: 50},
+ bottomRight: {horizontal: 10, vertical: 50},
+ bottomLeft: {horizontal: 10, vertical: 50},
+ });
+});
+
+test('should handle complex case with all four corners and slash', () => {
+ const result = parseBorderRadius({
+ borderRadius: '10px 20px 30px 40px / 15px 25px 35px 45px',
+ width: 100,
+ height: 200,
+ });
+ expect(result).toEqual({
+ topLeft: {horizontal: 10, vertical: 15},
+ topRight: {horizontal: 20, vertical: 25},
+ bottomRight: {horizontal: 30, vertical: 35},
+ bottomLeft: {horizontal: 40, vertical: 45},
+ });
+});
+
+test('should handle percentage with slash notation', () => {
+ const result = parseBorderRadius({
+ borderRadius: '50% / 25%',
+ width: 100,
+ height: 200,
+ });
+ expect(result).toEqual({
+ topLeft: {horizontal: 50, vertical: 50}, // 50% of width, 25% of height
+ topRight: {horizontal: 50, vertical: 50},
+ bottomRight: {horizontal: 50, vertical: 50},
+ bottomLeft: {horizontal: 50, vertical: 50},
+ });
+});
+
+test('should handle asymmetric shorthand expansion with slash', () => {
+ const result = parseBorderRadius({
+ borderRadius: '10px 20px / 30px',
+ width: 100,
+ height: 200,
+ });
+ expect(result).toEqual({
+ topLeft: {horizontal: 10, vertical: 30},
+ topRight: {horizontal: 20, vertical: 30},
+ bottomRight: {horizontal: 10, vertical: 30},
+ bottomLeft: {horizontal: 20, vertical: 30},
+ });
+});
+
+test('should handle three values before and after slash', () => {
+ const result = parseBorderRadius({
+ borderRadius: '10px 20px 30px / 15px 25px 35px',
+ width: 100,
+ height: 200,
+ });
+ expect(result).toEqual({
+ topLeft: {horizontal: 10, vertical: 15},
+ topRight: {horizontal: 20, vertical: 25},
+ bottomRight: {horizontal: 30, vertical: 35},
+ bottomLeft: {horizontal: 20, vertical: 25},
+ });
+});
+
+test('should handle percentages over 100%', () => {
+ const result = parseBorderRadius({
+ borderRadius: '150% 200%',
+ width: 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},
+ });
+});
+
+test('should handle mixed units with slash and percentages', () => {
+ const result = parseBorderRadius({
+ borderRadius: '50% 10px / 20% 30px',
+ width: 100,
+ height: 200,
+ });
+ expect(result).toEqual({
+ topLeft: {horizontal: 50, vertical: 40},
+ topRight: {horizontal: 10, vertical: 30},
+ bottomRight: {horizontal: 50, vertical: 40},
+ bottomLeft: {horizontal: 10, vertical: 30},
+ });
+});