Skip to content

🎥 Make videos programmatically with React

gotcha

Jonny Burger3031258141ef
diff --git a/packages/web-renderer/src/drawing/border-radius.ts b/packages/web-renderer/src/drawing/border-radius.ts
index f6b3f59cd6..9dc1f9d3e0 100644
--- a/packages/web-renderer/src/drawing/border-radius.ts
+++ b/packages/web-renderer/src/drawing/border-radius.ts
@@ -1,4 +1,5 @@
import {drawRoundedRectPath} from './draw-rounded';
+import {getBoxBasedOnBackgroundClip} from './get-padding-box';
export type BorderRadiusCorners = {
topLeft: {horizontal: number; vertical: number};
@@ -158,11 +159,15 @@ export function setBorderRadius({
rect,
borderRadius,
forceClipEvenWhenZero = false,
+ computedStyle,
+ backgroundClip,
}: {
ctx: OffscreenCanvasRenderingContext2D;
rect: DOMRect;
borderRadius: BorderRadiusCorners;
forceClipEvenWhenZero: boolean;
+ computedStyle: CSSStyleDeclaration;
+ backgroundClip: string;
}) {
if (
borderRadius.topLeft.horizontal === 0 &&
@@ -180,13 +185,63 @@ export function setBorderRadius({
ctx.save();
+ const boundingRect = getBoxBasedOnBackgroundClip(
+ rect,
+ computedStyle,
+ backgroundClip,
+ );
+
+ // See background-clip tests for why this logic matters!
+ const actualBorderRadius: BorderRadiusCorners = {
+ topLeft: {
+ horizontal: Math.max(
+ 0,
+ borderRadius.topLeft.horizontal - (boundingRect.left - rect.left),
+ ),
+ vertical: Math.max(
+ 0,
+ borderRadius.topLeft.vertical - (boundingRect.top - rect.top),
+ ),
+ },
+ topRight: {
+ horizontal: Math.max(
+ 0,
+ borderRadius.topRight.horizontal - (rect.right - boundingRect.right),
+ ),
+ vertical: Math.max(
+ 0,
+ borderRadius.topRight.vertical - (boundingRect.top - rect.top),
+ ),
+ },
+ bottomRight: {
+ horizontal: Math.max(
+ 0,
+ borderRadius.bottomRight.horizontal - (rect.right - boundingRect.right),
+ ),
+ vertical: Math.max(
+ 0,
+ borderRadius.bottomRight.vertical - (rect.bottom - boundingRect.bottom),
+ ),
+ },
+ bottomLeft: {
+ horizontal: Math.max(
+ 0,
+ borderRadius.bottomLeft.horizontal - (boundingRect.left - rect.left),
+ ),
+ vertical: Math.max(
+ 0,
+ borderRadius.bottomLeft.vertical - (rect.bottom - boundingRect.bottom),
+ ),
+ },
+ };
+
drawRoundedRectPath({
ctx,
- x: rect.left,
- y: rect.top,
- width: rect.width,
- height: rect.height,
- borderRadius,
+ x: boundingRect.left,
+ y: boundingRect.top,
+ width: boundingRect.width,
+ height: boundingRect.height,
+ borderRadius: actualBorderRadius,
});
ctx.clip();
diff --git a/packages/web-renderer/src/drawing/draw-background.ts b/packages/web-renderer/src/drawing/draw-background.ts
index e98510cb6b..c6d246ba66 100644
--- a/packages/web-renderer/src/drawing/draw-background.ts
+++ b/packages/web-renderer/src/drawing/draw-background.ts
@@ -47,75 +47,73 @@ export const drawBackground = async ({
}
};
- const boundingRects = getBoxBasedOnBackgroundClip(
+ const boundingRect = getBoxBasedOnBackgroundClip(
rect,
computedStyle,
backgroundClip,
);
- for (const boundingRect of boundingRects) {
- if (backgroundClip.includes('text')) {
- offsetLeft = boundingRect.left;
- offsetTop = boundingRect.top;
- const originalBackgroundClip = element.style.backgroundClip;
- const originalWebkitBackgroundClip = element.style.webkitBackgroundClip;
- element.style.backgroundClip = 'initial';
- element.style.webkitBackgroundClip = 'initial';
- const drawn = await getClippedBackground({
- element,
- boundingRect,
- logLevel,
- internalState,
- });
- element.style.backgroundClip = originalBackgroundClip;
- element.style.webkitBackgroundClip = originalWebkitBackgroundClip;
- contextToDraw = drawn;
- contextToDraw.globalCompositeOperation = 'source-in';
- }
-
- if (backgroundImage && backgroundImage !== 'none') {
- const gradientInfo = parseLinearGradient(backgroundImage);
- if (gradientInfo) {
- const gradient = createCanvasGradient({
- ctx: contextToDraw,
- rect: boundingRect,
- gradientInfo,
- offsetLeft,
- offsetTop,
- });
- const originalFillStyle = contextToDraw.fillStyle;
- contextToDraw.fillStyle = gradient;
- contextToDraw.fillRect(
- boundingRect.left - offsetLeft,
- boundingRect.top - offsetTop,
- boundingRect.width,
- boundingRect.height,
- );
- contextToDraw.fillStyle = originalFillStyle;
- continue;
- }
- }
+ if (backgroundClip.includes('text')) {
+ offsetLeft = boundingRect.left;
+ offsetTop = boundingRect.top;
+ const originalBackgroundClip = element.style.backgroundClip;
+ const originalWebkitBackgroundClip = element.style.webkitBackgroundClip;
+ element.style.backgroundClip = 'initial';
+ element.style.webkitBackgroundClip = 'initial';
+ const drawn = await getClippedBackground({
+ element,
+ boundingRect,
+ logLevel,
+ internalState,
+ });
+ element.style.backgroundClip = originalBackgroundClip;
+ element.style.webkitBackgroundClip = originalWebkitBackgroundClip;
+ contextToDraw = drawn;
+ contextToDraw.globalCompositeOperation = 'source-in';
+ }
- // Fallback to solid background color if no gradient was drawn
- if (
- backgroundColor &&
- backgroundColor !== 'transparent' &&
- !(
- backgroundColor.startsWith('rgba') &&
- (backgroundColor.endsWith(', 0)') || backgroundColor.endsWith(',0'))
- )
- ) {
+ if (backgroundImage && backgroundImage !== 'none') {
+ const gradientInfo = parseLinearGradient(backgroundImage);
+ if (gradientInfo) {
+ const gradient = createCanvasGradient({
+ ctx: contextToDraw,
+ rect: boundingRect,
+ gradientInfo,
+ offsetLeft,
+ offsetTop,
+ });
const originalFillStyle = contextToDraw.fillStyle;
- contextToDraw.fillStyle = backgroundColor;
+ contextToDraw.fillStyle = gradient;
contextToDraw.fillRect(
boundingRect.left - offsetLeft,
- boundingRect.top - offsetLeft,
+ boundingRect.top - offsetTop,
boundingRect.width,
boundingRect.height,
);
contextToDraw.fillStyle = originalFillStyle;
+ return finish();
}
}
+ // Fallback to solid background color if no gradient was drawn
+ if (
+ backgroundColor &&
+ backgroundColor !== 'transparent' &&
+ !(
+ backgroundColor.startsWith('rgba') &&
+ (backgroundColor.endsWith(', 0)') || backgroundColor.endsWith(',0'))
+ )
+ ) {
+ const originalFillStyle = contextToDraw.fillStyle;
+ contextToDraw.fillStyle = backgroundColor;
+ contextToDraw.fillRect(
+ boundingRect.left - offsetLeft,
+ boundingRect.top - offsetLeft,
+ boundingRect.width,
+ boundingRect.height,
+ );
+ contextToDraw.fillStyle = originalFillStyle;
+ }
+
finish();
};
diff --git a/packages/web-renderer/src/drawing/draw-element.ts b/packages/web-renderer/src/drawing/draw-element.ts
index fc5d4e3b1f..878bc24989 100644
--- a/packages/web-renderer/src/drawing/draw-element.ts
+++ b/packages/web-renderer/src/drawing/draw-element.ts
@@ -64,6 +64,8 @@ export const drawElement = async ({
rect,
borderRadius,
forceClipEvenWhenZero: false,
+ computedStyle,
+ backgroundClip,
});
await drawBackground({
@@ -80,6 +82,8 @@ export const drawElement = async ({
await draw({dimensions: rect, computedStyle, contextToDraw: context});
+ finishBorderRadius();
+
drawBorder({
ctx: context,
rect,
@@ -87,8 +91,6 @@ export const drawElement = async ({
computedStyle,
});
- finishBorderRadius();
-
// Drawing outline ignores overflow: hidden, finishing it and starting a new one for the outline
drawOutline({
ctx: context,
diff --git a/packages/web-renderer/src/drawing/get-padding-box.ts b/packages/web-renderer/src/drawing/get-padding-box.ts
index 69dfc8a700..f80ca08ca5 100644
--- a/packages/web-renderer/src/drawing/get-padding-box.ts
+++ b/packages/web-renderer/src/drawing/get-padding-box.ts
@@ -36,19 +36,23 @@ const getContentBox = (
export const getBoxBasedOnBackgroundClip = (
rect: DOMRect,
computedStyle: CSSStyleDeclaration,
- backgroundClip: string,
+ backgroundClip: string | undefined,
) => {
+ if (!backgroundClip) {
+ return rect;
+ }
+
if (backgroundClip.includes('text')) {
- return [rect];
+ return rect;
}
if (backgroundClip.includes('padding-box')) {
- return [getPaddingBox(rect, computedStyle)];
+ return getPaddingBox(rect, computedStyle);
}
if (backgroundClip.includes('content-box')) {
- return [getContentBox(rect, computedStyle)];
+ return getContentBox(rect, computedStyle);
}
- return [rect, getPaddingBox(rect, computedStyle)];
+ return rect;
};
diff --git a/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-chromium-darwin.png
index 8c02279419..d6d9828b15 100644
Binary files a/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-chromium-darwin.png and b/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-firefox-darwin.png
index aef2f4b3c9..6e59c15a95 100644
Binary files a/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-firefox-darwin.png and b/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-webkit-darwin.png
index dcba7929ed..f18c592b62 100644
Binary files a/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-webkit-darwin.png and b/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-webkit-darwin.png differ