Skip to content

🎥 Make videos programmatically with React

simplify, remove support for spread radius

Jonny Burgerb25c813386a6
diff --git a/packages/web-renderer/src/drawing/border-radius.ts b/packages/web-renderer/src/drawing/border-radius.ts
index 75b15109ad..f6b3f59cd6 100644
--- a/packages/web-renderer/src/drawing/border-radius.ts
+++ b/packages/web-renderer/src/drawing/border-radius.ts
@@ -1,3 +1,5 @@
+import {drawRoundedRectPath} from './draw-rounded';
+
export type BorderRadiusCorners = {
topLeft: {horizontal: number; vertical: number};
topRight: {horizontal: number; vertical: number};
@@ -177,97 +179,15 @@ export function setBorderRadius({
}
ctx.save();
- ctx.beginPath();
-
- // Start at top-left corner, after the horizontal radius
- ctx.moveTo(rect.left + borderRadius.topLeft.horizontal, rect.top);
-
- // Top edge to top-right corner
- ctx.lineTo(
- rect.left + rect.width - borderRadius.topRight.horizontal,
- rect.top,
- );
-
- // Top-right corner (elliptical arc)
- if (
- borderRadius.topRight.horizontal > 0 ||
- borderRadius.topRight.vertical > 0
- ) {
- ctx.ellipse(
- rect.left + rect.width - borderRadius.topRight.horizontal,
- rect.top + borderRadius.topRight.vertical,
- borderRadius.topRight.horizontal,
- borderRadius.topRight.vertical,
- 0,
- -Math.PI / 2,
- 0,
- );
- }
-
- // Right edge to bottom-right corner
- ctx.lineTo(
- rect.left + rect.width,
- rect.top + rect.height - borderRadius.bottomRight.vertical,
- );
-
- // Bottom-right corner (elliptical arc)
- if (
- borderRadius.bottomRight.horizontal > 0 ||
- borderRadius.bottomRight.vertical > 0
- ) {
- ctx.ellipse(
- rect.left + rect.width - borderRadius.bottomRight.horizontal,
- rect.top + rect.height - borderRadius.bottomRight.vertical,
- borderRadius.bottomRight.horizontal,
- borderRadius.bottomRight.vertical,
- 0,
- 0,
- Math.PI / 2,
- );
- }
- // Bottom edge to bottom-left corner
- ctx.lineTo(
- rect.left + borderRadius.bottomLeft.horizontal,
- rect.top + rect.height,
- );
-
- // Bottom-left corner (elliptical arc)
- if (
- borderRadius.bottomLeft.horizontal > 0 ||
- borderRadius.bottomLeft.vertical > 0
- ) {
- ctx.ellipse(
- rect.left + borderRadius.bottomLeft.horizontal,
- rect.top + rect.height - borderRadius.bottomLeft.vertical,
- borderRadius.bottomLeft.horizontal,
- borderRadius.bottomLeft.vertical,
- 0,
- Math.PI / 2,
- Math.PI,
- );
- }
-
- // Left edge to top-left corner
- ctx.lineTo(rect.left, rect.top + borderRadius.topLeft.vertical);
-
- // Top-left corner (elliptical arc)
- if (
- borderRadius.topLeft.horizontal > 0 ||
- borderRadius.topLeft.vertical > 0
- ) {
- ctx.ellipse(
- rect.left + borderRadius.topLeft.horizontal,
- rect.top + borderRadius.topLeft.vertical,
- borderRadius.topLeft.horizontal,
- borderRadius.topLeft.vertical,
- 0,
- Math.PI,
- (Math.PI * 3) / 2,
- );
- }
-
- ctx.closePath();
+ drawRoundedRectPath({
+ ctx,
+ x: rect.left,
+ y: rect.top,
+ width: rect.width,
+ height: rect.height,
+ borderRadius,
+ });
ctx.clip();
return () => {
diff --git a/packages/web-renderer/src/drawing/draw-box-shadow.ts b/packages/web-renderer/src/drawing/draw-box-shadow.ts
index ad81e3c853..99acdc76c5 100644
--- a/packages/web-renderer/src/drawing/draw-box-shadow.ts
+++ b/packages/web-renderer/src/drawing/draw-box-shadow.ts
@@ -1,10 +1,10 @@
import type {BorderRadiusCorners} from './border-radius';
+import {drawRoundedRectPath} from './draw-rounded';
interface BoxShadow {
offsetX: number;
offsetY: number;
blurRadius: number;
- spreadRadius: number;
color: string;
inset: boolean;
}
@@ -33,7 +33,6 @@ export const parseBoxShadow = (boxShadowValue: string): BoxShadow[] => {
offsetX: 0,
offsetY: 0,
blurRadius: 0,
- spreadRadius: 0,
color: 'rgba(0, 0, 0, 0.5)',
inset: false,
};
@@ -64,10 +63,6 @@ export const parseBoxShadow = (boxShadowValue: string): BoxShadow[] => {
if (values.length >= 3) {
shadow.blurRadius = Math.max(0, values[2]); // Blur cannot be negative
}
-
- if (values.length >= 4) {
- shadow.spreadRadius = values[3]; // Spread can be negative
- }
}
shadows.push(shadow);
@@ -76,152 +71,6 @@ export const parseBoxShadow = (boxShadowValue: string): BoxShadow[] => {
return shadows;
};
-/**
- * Draws a path following the border radius of a rectangle
- */
-const drawRoundedRectPath = (
- ctx: OffscreenCanvasRenderingContext2D,
- x: number,
- y: number,
- width: number,
- height: number,
- borderRadius: BorderRadiusCorners,
-) => {
- ctx.beginPath();
-
- // Start from top-left corner, after the radius
- ctx.moveTo(x + borderRadius.topLeft.horizontal, y);
-
- // Top edge
- ctx.lineTo(x + width - borderRadius.topRight.horizontal, y);
-
- // Top-right corner
- 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
- ctx.lineTo(x + width, y + height - borderRadius.bottomRight.vertical);
-
- // Bottom-right corner
- 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
- ctx.lineTo(x + borderRadius.bottomLeft.horizontal, y + height);
-
- // Bottom-left corner
- 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
- ctx.lineTo(x, y + borderRadius.topLeft.vertical);
-
- // Top-left corner
- 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();
-};
-
-/**
- * Adjusts border radius for spread radius
- */
-const adjustBorderRadiusForSpread = (
- borderRadius: BorderRadiusCorners,
- spread: number,
-): BorderRadiusCorners => {
- return {
- topLeft: {
- horizontal:
- borderRadius.topLeft.horizontal === 0
- ? 0
- : Math.max(0, borderRadius.topLeft.horizontal + spread),
- vertical:
- borderRadius.topLeft.vertical === 0
- ? 0
- : Math.max(0, borderRadius.topLeft.vertical + spread),
- },
- topRight: {
- horizontal:
- borderRadius.topRight.horizontal === 0
- ? 0
- : Math.max(0, borderRadius.topRight.horizontal + spread),
- vertical:
- borderRadius.topRight.vertical === 0
- ? 0
- : Math.max(0, borderRadius.topRight.vertical + spread),
- },
- bottomRight: {
- horizontal:
- borderRadius.bottomRight.horizontal === 0
- ? 0
- : Math.max(0, borderRadius.bottomRight.horizontal + spread),
- vertical:
- borderRadius.bottomRight.vertical === 0
- ? 0
- : Math.max(0, borderRadius.bottomRight.vertical + spread),
- },
- bottomLeft: {
- horizontal:
- borderRadius.bottomLeft.horizontal === 0
- ? 0
- : Math.max(0, borderRadius.bottomLeft.horizontal + spread),
- vertical:
- borderRadius.bottomLeft.vertical === 0
- ? 0
- : Math.max(0, borderRadius.bottomLeft.vertical + spread),
- },
- };
-};
-
export const drawBoxShadow = ({
ctx,
rect,
@@ -261,28 +110,16 @@ export const drawBoxShadow = ({
ctx.shadowOffsetX = shadow.offsetX;
ctx.shadowOffsetY = shadow.offsetY;
- // Calculate shadow rectangle with spread
- const shadowX = rect.left + shadow.spreadRadius;
- const shadowY = rect.top + shadow.spreadRadius;
- const shadowW = rect.width - shadow.spreadRadius * 2;
- const shadowH = rect.height - shadow.spreadRadius * 2;
-
- // Adjust border radius for spread
- const adjustedBorderRadius = adjustBorderRadiusForSpread(
- borderRadius,
- -shadow.spreadRadius,
- );
-
// Fill the shape (this will cast the shadow)
- ctx.fillStyle = shadow.color;
- drawRoundedRectPath(
+ ctx.fillStyle = 'red';
+ drawRoundedRectPath({
ctx,
- shadowX,
- shadowY,
- shadowW,
- shadowH,
- adjustedBorderRadius,
- );
+ x: rect.left,
+ y: rect.top,
+ width: rect.width,
+ height: rect.height,
+ borderRadius,
+ });
ctx.fill();
}
diff --git a/packages/web-renderer/src/drawing/draw-outline.ts b/packages/web-renderer/src/drawing/draw-outline.ts
index b2892a7380..866c96cc5e 100644
--- a/packages/web-renderer/src/drawing/draw-outline.ts
+++ b/packages/web-renderer/src/drawing/draw-outline.ts
@@ -1,4 +1,5 @@
import type {BorderRadiusCorners} from './border-radius';
+import {drawRoundedRectPath} from './draw-rounded';
export const parseOutlineWidth = (value: string): number => {
return parseFloat(value) || 0;
@@ -50,7 +51,6 @@ export const drawOutline = ({
const originalLineWidth = ctx.lineWidth;
const originalLineDash = ctx.getLineDash();
- ctx.beginPath();
ctx.strokeStyle = outlineColor;
ctx.lineWidth = outlineWidth;
ctx.setLineDash(getLineDashPattern(outlineStyle, outlineWidth));
@@ -64,142 +64,14 @@ export const drawOutline = ({
const outlineY = rect.top - offset;
const outlineW = rect.width + offset * 2;
const outlineH = rect.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:
- borderRadius.topLeft.horizontal === 0
- ? 0
- : Math.max(0, borderRadius.topLeft.horizontal + offset),
- vertical:
- borderRadius.topLeft.vertical === 0
- ? 0
- : Math.max(0, borderRadius.topLeft.vertical + offset),
- },
- topRight: {
- horizontal:
- borderRadius.topRight.horizontal === 0
- ? 0
- : Math.max(0, borderRadius.topRight.horizontal + offset),
- vertical:
- borderRadius.topRight.vertical === 0
- ? 0
- : Math.max(0, borderRadius.topRight.vertical + offset),
- },
- bottomRight: {
- horizontal:
- borderRadius.bottomRight.horizontal === 0
- ? 0
- : Math.max(0, borderRadius.bottomRight.horizontal + offset),
- vertical:
- borderRadius.bottomRight.vertical === 0
- ? 0
- : Math.max(0, borderRadius.bottomRight.vertical + offset),
- },
- bottomLeft: {
- horizontal:
- borderRadius.bottomLeft.horizontal === 0
- ? 0
- : Math.max(0, borderRadius.bottomLeft.horizontal + offset),
- vertical:
- borderRadius.bottomLeft.vertical === 0
- ? 0
- : 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();
+ drawRoundedRectPath({
+ ctx,
+ x: outlineX,
+ y: outlineY,
+ width: outlineW,
+ height: outlineH,
+ borderRadius,
+ });
ctx.stroke();
// Restore original canvas state
diff --git a/packages/web-renderer/src/drawing/draw-rounded.ts b/packages/web-renderer/src/drawing/draw-rounded.ts
new file mode 100644
index 0000000000..ad369e91ea
--- /dev/null
+++ b/packages/web-renderer/src/drawing/draw-rounded.ts
@@ -0,0 +1,100 @@
+import type {BorderRadiusCorners} from './border-radius';
+
+export const drawRoundedRectPath = ({
+ ctx,
+ x,
+ y,
+ width,
+ height,
+ borderRadius,
+}: {
+ ctx: OffscreenCanvasRenderingContext2D;
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+ borderRadius: BorderRadiusCorners;
+}) => {
+ ctx.beginPath();
+
+ // Start from top-left corner, after the radius
+ ctx.moveTo(x + borderRadius.topLeft.horizontal, y);
+
+ // Top edge
+ ctx.lineTo(x + width - borderRadius.topRight.horizontal, y);
+
+ // Top-right corner
+ 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
+ ctx.lineTo(x + width, y + height - borderRadius.bottomRight.vertical);
+
+ // Bottom-right corner
+ 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
+ ctx.lineTo(x + borderRadius.bottomLeft.horizontal, y + height);
+
+ // Bottom-left corner
+ 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
+ ctx.lineTo(x, y + borderRadius.topLeft.vertical);
+
+ // Top-left corner
+ 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();
+};
diff --git a/packages/web-renderer/src/test/fixtures/box-shadow.tsx b/packages/web-renderer/src/test/fixtures/box-shadow.tsx
index 60a87027b7..504f63bae4 100644
--- a/packages/web-renderer/src/test/fixtures/box-shadow.tsx
+++ b/packages/web-renderer/src/test/fixtures/box-shadow.tsx
@@ -32,6 +32,16 @@ const Component: React.FC = () => {
}}
/>
+ {/* Box shadow with spread radius */}
+ <div
+ style={{
+ backgroundColor: 'white',
+ width: 100,
+ height: 80,
+ boxShadow: '0 4px 10px 8px rgba(0, 0, 0, 0.3)',
+ }}
+ />
+
{/* Box shadow with overflow: hidden */}
<div
style={{