🎥 Make videos programmatically with React
outline
Jonny Burgere46f6f7fe7d2
diff --git a/packages/web-renderer/src/drawing/draw-element.ts b/packages/web-renderer/src/drawing/draw-element.tsindex 976acf7f08..9c6e8ccb94 100644--- a/packages/web-renderer/src/drawing/draw-element.ts+++ b/packages/web-renderer/src/drawing/draw-element.ts@@ -1,5 +1,6 @@import {parseBorderRadius, setBorderRadius} from './border-radius';import {drawBorder} from './draw-border';+import {drawOutline} from './draw-outline';import type {DrawFn} from './drawn-fn';import {setOpacity} from './opacity';import {setTransform} from './transform';@@ -83,6 +84,16 @@ export const drawElement = async ({computedStyle,});+ drawOutline({+ 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/drawing/draw-outline.ts b/packages/web-renderer/src/drawing/draw-outline.tsnew file mode 100644index 0000000000..769ef4de32--- /dev/null+++ b/packages/web-renderer/src/drawing/draw-outline.ts@@ -0,0 +1,191 @@+import type {BorderRadiusCorners} from './border-radius';++const parseOutlineWidth = (value: string): number => {+ return parseFloat(value) || 0;+};++const parseOutlineOffset = (value: string): number => {+ return parseFloat(value) || 0;+};++const getLineDashPattern = (style: string, width: number): number[] => {+ if (style === 'dashed') {+ return [width * 2, width];+ }++ if (style === 'dotted') {+ return [width, width];+ }++ return [];+};++export const drawOutline = ({+ ctx,+ x,+ y,+ width,+ height,+ borderRadius,+ computedStyle,+}: {+ ctx: OffscreenCanvasRenderingContext2D;+ x: number;+ y: number;+ width: number;+ height: number;+ borderRadius: BorderRadiusCorners;+ computedStyle: CSSStyleDeclaration;+}) => {+ const outlineWidth = parseOutlineWidth(computedStyle.outlineWidth);+ const {outlineStyle} = computedStyle;+ const outlineColor = computedStyle.outlineColor || 'black';+ const outlineOffset = parseOutlineOffset(computedStyle.outlineOffset);++ // Check if we have a visible outline+ if (+ outlineWidth <= 0 ||+ outlineStyle === 'none' ||+ outlineStyle === 'hidden'+ ) {+ return;+ }++ // Save original canvas state+ const originalStrokeStyle = ctx.strokeStyle;+ const originalLineWidth = ctx.lineWidth;+ const originalLineDash = ctx.getLineDash();++ ctx.beginPath();+ ctx.strokeStyle = outlineColor;+ ctx.lineWidth = outlineWidth;+ ctx.setLineDash(getLineDashPattern(outlineStyle, outlineWidth));++ // Calculate outline position+ // Outline is drawn outside the border edge, offset by outlineOffset+ const halfWidth = outlineWidth / 2;+ const offset = outlineOffset + halfWidth;++ const outlineX = x - offset;+ const outlineY = y - offset;+ const outlineW = width + offset * 2;+ const outlineH = height + offset * 2;++ // Adjust border radius for the outline offset+ // When outline-offset is positive, we need to expand the radius+ // When outline-offset is negative, we need to shrink the radius+ const adjustedBorderRadius = {+ topLeft: {+ horizontal: Math.max(0, borderRadius.topLeft.horizontal + offset),+ vertical: Math.max(0, borderRadius.topLeft.vertical + offset),+ },+ topRight: {+ horizontal: Math.max(0, borderRadius.topRight.horizontal + offset),+ vertical: Math.max(0, borderRadius.topRight.vertical + offset),+ },+ bottomRight: {+ horizontal: Math.max(0, borderRadius.bottomRight.horizontal + offset),+ vertical: Math.max(0, borderRadius.bottomRight.vertical + offset),+ },+ bottomLeft: {+ horizontal: Math.max(0, borderRadius.bottomLeft.horizontal + offset),+ vertical: Math.max(0, borderRadius.bottomLeft.vertical + offset),+ },+ };++ // Draw continuous path with border radius+ ctx.moveTo(outlineX + adjustedBorderRadius.topLeft.horizontal, outlineY);++ // Top edge+ ctx.lineTo(+ outlineX + outlineW - adjustedBorderRadius.topRight.horizontal,+ outlineY,+ );++ // Top-right corner+ if (+ adjustedBorderRadius.topRight.horizontal > 0 ||+ adjustedBorderRadius.topRight.vertical > 0+ ) {+ ctx.ellipse(+ outlineX + outlineW - adjustedBorderRadius.topRight.horizontal,+ outlineY + adjustedBorderRadius.topRight.vertical,+ adjustedBorderRadius.topRight.horizontal,+ adjustedBorderRadius.topRight.vertical,+ 0,+ -Math.PI / 2,+ 0,+ );+ }++ // Right edge+ ctx.lineTo(+ outlineX + outlineW,+ outlineY + outlineH - adjustedBorderRadius.bottomRight.vertical,+ );++ // Bottom-right corner+ if (+ adjustedBorderRadius.bottomRight.horizontal > 0 ||+ adjustedBorderRadius.bottomRight.vertical > 0+ ) {+ ctx.ellipse(+ outlineX + outlineW - adjustedBorderRadius.bottomRight.horizontal,+ outlineY + outlineH - adjustedBorderRadius.bottomRight.vertical,+ adjustedBorderRadius.bottomRight.horizontal,+ adjustedBorderRadius.bottomRight.vertical,+ 0,+ 0,+ Math.PI / 2,+ );+ }++ // Bottom edge+ ctx.lineTo(+ outlineX + adjustedBorderRadius.bottomLeft.horizontal,+ outlineY + outlineH,+ );++ // Bottom-left corner+ if (+ adjustedBorderRadius.bottomLeft.horizontal > 0 ||+ adjustedBorderRadius.bottomLeft.vertical > 0+ ) {+ ctx.ellipse(+ outlineX + adjustedBorderRadius.bottomLeft.horizontal,+ outlineY + outlineH - adjustedBorderRadius.bottomLeft.vertical,+ adjustedBorderRadius.bottomLeft.horizontal,+ adjustedBorderRadius.bottomLeft.vertical,+ 0,+ Math.PI / 2,+ Math.PI,+ );+ }++ // Left edge+ ctx.lineTo(outlineX, outlineY + adjustedBorderRadius.topLeft.vertical);++ // Top-left corner+ if (+ adjustedBorderRadius.topLeft.horizontal > 0 ||+ adjustedBorderRadius.topLeft.vertical > 0+ ) {+ ctx.ellipse(+ outlineX + adjustedBorderRadius.topLeft.horizontal,+ outlineY + adjustedBorderRadius.topLeft.vertical,+ adjustedBorderRadius.topLeft.horizontal,+ adjustedBorderRadius.topLeft.vertical,+ 0,+ Math.PI,+ (Math.PI * 3) / 2,+ );+ }++ ctx.closePath();+ ctx.stroke();++ // Restore original canvas state+ ctx.strokeStyle = originalStrokeStyle;+ ctx.lineWidth = originalLineWidth;+ ctx.setLineDash(originalLineDash);+};diff --git a/packages/web-renderer/src/test/Root.tsx b/packages/web-renderer/src/test/Root.tsxindex 6619e3675a..10be8004ef 100644--- a/packages/web-renderer/src/test/Root.tsx+++ b/packages/web-renderer/src/test/Root.tsx@@ -24,6 +24,7 @@ import {opacitySimple} from './fixtures/opacity-simple';import {opacityZero} from './fixtures/opacity-zero';import {overflowHidden} from './fixtures/overflow-hidden';import {overflowHidden3dTransform} from './fixtures/overflow-hidden-3d-transform';+import {outline} from './fixtures/outline';import {overflowY} from './fixtures/overflow-y';import {parentRotatedSvg} from './fixtures/parent-rotated-svg';import {parentTransformOrigin} from './fixtures/parent-transform-origin';@@ -73,6 +74,7 @@ export const Root: React.FC = () => {<Composition {...opacityNested} /><Composition {...opacityZero} /><Composition {...backgroundColor} />+ <Composition {...outline} /><Folder name="border"><Composition {...border} /><Composition {...borderRadius} />diff --git a/packages/web-renderer/src/test/fixtures/outline.tsx b/packages/web-renderer/src/test/fixtures/outline.tsxnew file mode 100644index 0000000000..4e365a5ab3--- /dev/null+++ b/packages/web-renderer/src/test/fixtures/outline.tsx@@ -0,0 +1,52 @@+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',+ outline: '5px solid blue',+ }}+ />+ <div+ style={{+ width: 100,+ height: 100,+ backgroundColor: 'lightgreen',+ outline: '3px dashed green',+ borderRadius: 10,+ }}+ />+ <div+ style={{+ width: 100,+ height: 100,+ transform: 'rotateX(30deg) rotateY(30deg)',+ backgroundColor: 'lightyellow',+ outline: '2px dotted orange',+ outlineOffset: 5,+ overflow: 'hidden',+ }}+ />+ </AbsoluteFill>+ );+};++export const outline = {+ component: Component,+ id: 'outline',+ width: 200,+ height: 450,+ fps: 25,+ durationInFrames: 1,+} as const;diff --git a/packages/web-renderer/src/test/outline.test.tsx b/packages/web-renderer/src/test/outline.test.tsxnew file mode 100644index 0000000000..24993c65a1--- /dev/null+++ b/packages/web-renderer/src/test/outline.test.tsx@@ -0,0 +1,16 @@+import {test} from 'vitest';+import {renderStillOnWeb} from '../render-still-on-web';+import {outline} from './fixtures/outline';+import {testImage} from './utils';++test('should render outline', async () => {+ const {blob} = await renderStillOnWeb({+ licenseKey: 'free-license',+ composition: outline,+ frame: 0,+ inputProps: {},+ imageFormat: 'png',+ });++ await testImage({blob, testId: 'outline'});+});