Skip to content

🎥 Make videos programmatically with React

`@remotion/web-renderer`: Support `border-style`, `border-width` and `border-color`

Jonny Burger9239a2d757b4
diff --git a/packages/docs/docs/client-side-rendering/limitations.mdx b/packages/docs/docs/client-side-rendering/limitations.mdx
index a38685da7d..443cb13a6b 100644
--- a/packages/docs/docs/client-side-rendering/limitations.mdx
+++ b/packages/docs/docs/client-side-rendering/limitations.mdx
@@ -48,6 +48,7 @@ Only some CSS styles are being supported:
Remotion is able to obtain the position of the element in the DOM, no matter which CSS properties are used
- Transformations, such as `transform`, `rotate`, `scale`, `translate`, `perspective`, `matrix`, `matrix3d`.
- `border-radius`
+- `border-style`, `border-color`, `border-width`
- `display: none`
- `opacity`
- `background-color`
@@ -76,7 +77,6 @@ Every element and style that is not explicitly supported is unsupported. Some im
- `background`
- `color`
- `mask-image` or `clip-path`
-- `border`
- `box-shadow`
- `z-index`
- Any other property not listed above.
diff --git a/packages/web-renderer/src/drawing/draw-border.ts b/packages/web-renderer/src/drawing/draw-border.ts
new file mode 100644
index 0000000000..b1b3ed4565
--- /dev/null
+++ b/packages/web-renderer/src/drawing/draw-border.ts
@@ -0,0 +1,218 @@
+import type {BorderRadiusCorners} from './border-radius';
+
+export const drawBorder = ({
+ ctx,
+ x,
+ y,
+ width,
+ height,
+ borderRadius,
+ computedStyle,
+}: {
+ ctx: OffscreenCanvasRenderingContext2D;
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+ borderRadius: BorderRadiusCorners;
+ computedStyle: CSSStyleDeclaration;
+}) => {
+ const {
+ borderStyle,
+ borderColor,
+ borderWidth: computedBorderWidth,
+ } = computedStyle;
+
+ // Parse border width (can be shorthand like "1px 2px 3px 4px")
+ const borderWidths = computedBorderWidth
+ .split(/\s+/)
+ .map((w) => parseFloat(w));
+ const borderTop = borderWidths[0] || 0;
+ const borderRight = borderWidths[1] || borderTop;
+ const borderBottom = borderWidths[2] || borderTop;
+ const borderLeft = borderWidths[3] || borderRight;
+
+ // Check if we have a visible border
+ const hasBorder =
+ borderStyle &&
+ borderStyle !== 'none' &&
+ borderStyle !== 'hidden' &&
+ (borderTop > 0 || borderRight > 0 || borderBottom > 0 || borderLeft > 0);
+
+ if (!hasBorder) {
+ return;
+ }
+
+ const originalStrokeStyle = ctx.strokeStyle;
+ const originalLineWidth = ctx.lineWidth;
+ const originalLineDash = ctx.getLineDash();
+
+ ctx.strokeStyle = borderColor;
+
+ // Set line dash based on border style
+ if (borderStyle === 'dashed') {
+ const max = Math.max(borderTop, borderRight, borderBottom, borderLeft);
+ ctx.setLineDash([max * 2, max]);
+ } else if (borderStyle === 'dotted') {
+ ctx.setLineDash([
+ Math.max(borderTop, borderRight, borderBottom, borderLeft),
+ ]);
+ } else {
+ ctx.setLineDash([]);
+ }
+
+ // For simplicity, use the maximum border width if they differ
+ // A full implementation would draw each side separately
+ const maxBorderWidth = Math.max(
+ borderTop,
+ borderRight,
+ borderBottom,
+ borderLeft,
+ );
+
+ // Create path for border (inset by half the border width to draw inside)
+ ctx.beginPath();
+
+ const borderX = x + maxBorderWidth / 2;
+ const borderY = y + maxBorderWidth / 2;
+ const borderWidth = width - maxBorderWidth;
+ const borderHeight = height - maxBorderWidth;
+
+ // Account for border radius, adjusted for the border width
+ const adjustedBorderRadius = {
+ topLeft: {
+ horizontal: Math.max(
+ 0,
+ borderRadius.topLeft.horizontal - maxBorderWidth / 2,
+ ),
+ vertical: Math.max(0, borderRadius.topLeft.vertical - maxBorderWidth / 2),
+ },
+ topRight: {
+ horizontal: Math.max(
+ 0,
+ borderRadius.topRight.horizontal - maxBorderWidth / 2,
+ ),
+ vertical: Math.max(
+ 0,
+ borderRadius.topRight.vertical - maxBorderWidth / 2,
+ ),
+ },
+ bottomRight: {
+ horizontal: Math.max(
+ 0,
+ borderRadius.bottomRight.horizontal - maxBorderWidth / 2,
+ ),
+ vertical: Math.max(
+ 0,
+ borderRadius.bottomRight.vertical - maxBorderWidth / 2,
+ ),
+ },
+ bottomLeft: {
+ horizontal: Math.max(
+ 0,
+ borderRadius.bottomLeft.horizontal - maxBorderWidth / 2,
+ ),
+ vertical: Math.max(
+ 0,
+ borderRadius.bottomLeft.vertical - maxBorderWidth / 2,
+ ),
+ },
+ };
+
+ // Draw path with border radius
+ ctx.moveTo(borderX + adjustedBorderRadius.topLeft.horizontal, borderY);
+
+ // Top edge
+ ctx.lineTo(
+ borderX + borderWidth - adjustedBorderRadius.topRight.horizontal,
+ borderY,
+ );
+
+ // Top-right corner
+ if (
+ adjustedBorderRadius.topRight.horizontal > 0 ||
+ adjustedBorderRadius.topRight.vertical > 0
+ ) {
+ ctx.ellipse(
+ borderX + borderWidth - adjustedBorderRadius.topRight.horizontal,
+ borderY + adjustedBorderRadius.topRight.vertical,
+ adjustedBorderRadius.topRight.horizontal,
+ adjustedBorderRadius.topRight.vertical,
+ 0,
+ -Math.PI / 2,
+ 0,
+ );
+ }
+
+ // Right edge
+ ctx.lineTo(
+ borderX + borderWidth,
+ borderY + borderHeight - adjustedBorderRadius.bottomRight.vertical,
+ );
+
+ // Bottom-right corner
+ if (
+ adjustedBorderRadius.bottomRight.horizontal > 0 ||
+ adjustedBorderRadius.bottomRight.vertical > 0
+ ) {
+ ctx.ellipse(
+ borderX + borderWidth - adjustedBorderRadius.bottomRight.horizontal,
+ borderY + borderHeight - adjustedBorderRadius.bottomRight.vertical,
+ adjustedBorderRadius.bottomRight.horizontal,
+ adjustedBorderRadius.bottomRight.vertical,
+ 0,
+ 0,
+ Math.PI / 2,
+ );
+ }
+
+ // Bottom edge
+ ctx.lineTo(
+ borderX + adjustedBorderRadius.bottomLeft.horizontal,
+ borderY + borderHeight,
+ );
+
+ // Bottom-left corner
+ if (
+ adjustedBorderRadius.bottomLeft.horizontal > 0 ||
+ adjustedBorderRadius.bottomLeft.vertical > 0
+ ) {
+ ctx.ellipse(
+ borderX + adjustedBorderRadius.bottomLeft.horizontal,
+ borderY + borderHeight - adjustedBorderRadius.bottomLeft.vertical,
+ adjustedBorderRadius.bottomLeft.horizontal,
+ adjustedBorderRadius.bottomLeft.vertical,
+ 0,
+ Math.PI / 2,
+ Math.PI,
+ );
+ }
+
+ // Left edge
+ ctx.lineTo(borderX, borderY + adjustedBorderRadius.topLeft.vertical);
+
+ // Top-left corner
+ if (
+ adjustedBorderRadius.topLeft.horizontal > 0 ||
+ adjustedBorderRadius.topLeft.vertical > 0
+ ) {
+ ctx.ellipse(
+ borderX + adjustedBorderRadius.topLeft.horizontal,
+ borderY + adjustedBorderRadius.topLeft.vertical,
+ adjustedBorderRadius.topLeft.horizontal,
+ adjustedBorderRadius.topLeft.vertical,
+ 0,
+ Math.PI,
+ (Math.PI * 3) / 2,
+ );
+ }
+
+ ctx.closePath();
+ ctx.lineWidth = maxBorderWidth;
+ ctx.stroke();
+
+ // Restore original values
+ ctx.strokeStyle = originalStrokeStyle;
+ ctx.lineWidth = originalLineWidth;
+ ctx.setLineDash(originalLineDash);
+};
diff --git a/packages/web-renderer/src/drawing/draw-element-to-canvas.ts b/packages/web-renderer/src/drawing/draw-element-to-canvas.ts
index 9c82a3d903..e0ad4e2af2 100644
--- a/packages/web-renderer/src/drawing/draw-element-to-canvas.ts
+++ b/packages/web-renderer/src/drawing/draw-element-to-canvas.ts
@@ -1,5 +1,6 @@
import {parseBorderRadius, setBorderRadius} from './border-radius';
import {calculateTransforms} from './calculate-transforms';
+import {drawBorder} from './draw-border';
import {setOpacity} from './opacity';
import {setTransform} from './transform';
import {turnSvgIntoDrawable} from './turn-svg-into-drawable';
@@ -87,6 +88,16 @@ export const drawElementToCanvas = async ({
);
}
+ drawBorder({
+ ctx: context,
+ x: dimensions.left,
+ y: dimensions.top,
+ width: dimensions.width,
+ height: dimensions.height,
+ borderRadius,
+ computedStyle,
+ });
+
finishOpacity();
finishBorderRadius();
finishTransform();
diff --git a/packages/web-renderer/src/test/Root.tsx b/packages/web-renderer/src/test/Root.tsx
index a896b04393..894fb172ef 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 {backgroundColor} from './fixtures/background-color';
+import {border} from './fixtures/border';
import {borderRadius} from './fixtures/border-radius';
import {borderRadiusClamped} from './fixtures/border-radius-clamped';
import {borderRadiusDifferent} from './fixtures/border-radius-different';
@@ -55,6 +56,7 @@ export const Root: React.FC = () => {
<Composition {...opacityNested} />
<Composition {...opacityZero} />
<Composition {...backgroundColor} />
+ <Composition {...border} />
</>
);
};
diff --git a/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-chromium-darwin.png
new file mode 100644
index 0000000000..5d0dae4624
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-firefox-darwin.png
new file mode 100644
index 0000000000..8b4283581c
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-webkit-darwin.png
new file mode 100644
index 0000000000..6ffe4be885
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-webkit-darwin.png differ
diff --git a/packages/web-renderer/src/test/border.test.tsx b/packages/web-renderer/src/test/border.test.tsx
new file mode 100644
index 0000000000..8838f8a368
--- /dev/null
+++ b/packages/web-renderer/src/test/border.test.tsx
@@ -0,0 +1,15 @@
+import {test} from 'vitest';
+import {renderStillOnWeb} from '../render-still-on-web';
+import {border} from './fixtures/border';
+import {testImage} from './utils';
+
+test('should render border', async () => {
+ const blob = await renderStillOnWeb({
+ composition: border,
+ frame: 0,
+ inputProps: {},
+ imageFormat: 'png',
+ });
+
+ await testImage({blob, testId: 'border'});
+});
diff --git a/packages/web-renderer/src/test/fixtures/border.tsx b/packages/web-renderer/src/test/fixtures/border.tsx
new file mode 100644
index 0000000000..caddf872ef
--- /dev/null
+++ b/packages/web-renderer/src/test/fixtures/border.tsx
@@ -0,0 +1,50 @@
+import {AbsoluteFill} from 'remotion';
+
+const Component: React.FC = () => {
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ gap: 20,
+ flexDirection: 'column',
+ }}
+ >
+ <div
+ style={{
+ width: 100,
+ height: 100,
+ backgroundColor: 'lightblue',
+ border: '5px solid blue',
+ borderRadius: 20,
+ }}
+ />
+ <div
+ style={{
+ width: 100,
+ height: 100,
+ backgroundColor: 'lightgreen',
+ border: '3px dashed green',
+ borderRadius: 10,
+ }}
+ />
+ <div
+ style={{
+ width: 100,
+ height: 100,
+ backgroundColor: 'lightyellow',
+ border: '2px dotted orange',
+ }}
+ />
+ </AbsoluteFill>
+ );
+};
+
+export const border = {
+ component: Component,
+ id: 'border',
+ width: 200,
+ height: 400,
+ fps: 25,
+ durationInFrames: 1,
+} as const;