Skip to content

🎥 Make videos programmatically with React

hmm interesting!

Jonny Burger3c63b36c008b
diff --git a/packages/web-renderer/src/compose.ts b/packages/web-renderer/src/compose.ts
index 314b9ec6b2..fb8f30ae21 100644
--- a/packages/web-renderer/src/compose.ts
+++ b/packages/web-renderer/src/compose.ts
@@ -14,7 +14,6 @@ const walkOverNode = ({
parentRect,
internalState,
rootElement,
- onlyBackgroundClip,
}: {
node: Node;
context: OffscreenCanvasRenderingContext2D;
@@ -22,7 +21,6 @@ const walkOverNode = ({
parentRect: DOMRect;
internalState: InternalState;
rootElement: HTMLElement | SVGElement;
- onlyBackgroundClip: boolean;
}): Promise<ProcessNodeReturnValue> => {
if (node instanceof HTMLElement || node instanceof SVGElement) {
return processNode({
@@ -33,7 +31,6 @@ const walkOverNode = ({
parentRect,
internalState,
rootElement,
- onlyBackgroundClip,
});
}
@@ -45,7 +42,6 @@ const walkOverNode = ({
parentRect,
internalState,
rootElement,
- onlyBackgroundClip,
});
}
@@ -112,7 +108,6 @@ export const compose = async ({
parentRect,
internalState,
rootElement: element,
- onlyBackgroundClip,
});
if (val.type === 'skip-children') {
if (!skipToNextNonDescendant(treeWalker)) {
diff --git a/packages/web-renderer/src/drawing/draw-background.ts b/packages/web-renderer/src/drawing/draw-background.ts
index 8586bc160e..355b2b8c61 100644
--- a/packages/web-renderer/src/drawing/draw-background.ts
+++ b/packages/web-renderer/src/drawing/draw-background.ts
@@ -1,39 +1,89 @@
+import type {LogLevel} from 'remotion';
+import type {InternalState} from '../internal-state';
+import {getClippedBackground} from './get-clipped-background';
import {
createCanvasGradient,
parseLinearGradient,
} from './parse-linear-gradient';
-export const drawBackground = ({
+export const drawBackground = async ({
backgroundImage,
context,
rect,
backgroundColor,
backgroundClipText,
+ element,
+ logLevel,
+ internalState,
}: {
backgroundImage: string;
context: OffscreenCanvasRenderingContext2D;
rect: DOMRect;
backgroundColor: string;
backgroundClipText: boolean;
+ element: HTMLElement | SVGElement;
+ logLevel: LogLevel;
+ internalState: InternalState;
}) => {
+ let contextToDraw = context;
+
+ const originalCompositeOperation = context.globalCompositeOperation;
+ let offsetLeft = 0;
+ let offsetTop = 0;
+
+ const finish = () => {
+ context.globalCompositeOperation = originalCompositeOperation;
+ if (context !== contextToDraw) {
+ context.drawImage(
+ contextToDraw.canvas,
+ offsetLeft,
+ offsetTop,
+ contextToDraw.canvas.width,
+ contextToDraw.canvas.height,
+ );
+ }
+ };
+
if (backgroundClipText) {
+ offsetLeft = rect.left;
+ offsetTop = rect.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: rect,
+ logLevel,
+ internalState,
+ });
+ element.style.backgroundClip = originalBackgroundClip;
+ element.style.webkitBackgroundClip = originalWebkitBackgroundClip;
+ contextToDraw = drawn;
+
context.globalCompositeOperation = 'source-in';
- return;
}
if (backgroundImage && backgroundImage !== 'none') {
const gradientInfo = parseLinearGradient(backgroundImage);
if (gradientInfo) {
const gradient = createCanvasGradient({
- ctx: context,
+ ctx: contextToDraw,
rect,
gradientInfo,
+ offsetLeft,
+ offsetTop,
});
- const originalFillStyle = context.fillStyle;
- context.fillStyle = gradient;
- context.fillRect(rect.left, rect.top, rect.width, rect.height);
- context.fillStyle = originalFillStyle;
- return;
+ const originalFillStyle = contextToDraw.fillStyle;
+ contextToDraw.fillStyle = gradient;
+ contextToDraw.fillRect(
+ rect.left - offsetLeft,
+ rect.top - offsetTop,
+ rect.width,
+ rect.height,
+ );
+ contextToDraw.fillStyle = originalFillStyle;
+ return finish();
}
}
@@ -46,9 +96,16 @@ export const drawBackground = ({
(backgroundColor.endsWith(', 0)') || backgroundColor.endsWith(',0'))
)
) {
- const originalFillStyle = context.fillStyle;
- context.fillStyle = backgroundColor;
- context.fillRect(rect.left, rect.top, rect.width, rect.height);
- context.fillStyle = originalFillStyle;
+ const originalFillStyle = contextToDraw.fillStyle;
+ contextToDraw.fillStyle = backgroundColor;
+ contextToDraw.fillRect(
+ rect.left - offsetLeft,
+ rect.top - offsetLeft,
+ rect.width,
+ rect.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 80a5775c9c..35005bd230 100644
--- a/packages/web-renderer/src/drawing/draw-element.ts
+++ b/packages/web-renderer/src/drawing/draw-element.ts
@@ -1,4 +1,5 @@
import type {LogLevel} from 'remotion';
+import type {InternalState} from '../internal-state';
import {parseBorderRadius, setBorderRadius} from './border-radius';
import {drawBackground} from './draw-background';
import {drawBorder} from './draw-border';
@@ -18,7 +19,8 @@ export const drawElement = async ({
totalMatrix,
parentRect,
logLevel,
- onlyBackgroundClip,
+ element,
+ internalState,
}: {
rect: DOMRect;
computedStyle: CSSStyleDeclaration;
@@ -28,7 +30,8 @@ export const drawElement = async ({
draw: DrawFn;
parentRect: DOMRect;
logLevel: LogLevel;
- onlyBackgroundClip: boolean;
+ element: HTMLElement | SVGElement;
+ internalState: InternalState;
}) => {
const {backgroundImage, backgroundColor} = computedStyle;
const borderRadius = parseBorderRadius({
@@ -63,24 +66,25 @@ export const drawElement = async ({
forceClipEvenWhenZero: false,
});
- drawBackground({
+ await drawBackground({
backgroundImage,
context,
rect,
backgroundColor,
- backgroundClipText: onlyBackgroundClip,
+ backgroundClipText: computedStyle.backgroundClip.includes('text'),
+ element,
+ logLevel,
+ internalState,
});
- if (!onlyBackgroundClip) {
- await draw({dimensions: rect, computedStyle, contextToDraw: context});
+ await draw({dimensions: rect, computedStyle, contextToDraw: context});
- drawBorder({
- ctx: context,
- rect,
- borderRadius,
- computedStyle,
- });
- }
+ drawBorder({
+ ctx: context,
+ rect,
+ borderRadius,
+ computedStyle,
+ });
finishBorderRadius();
@@ -90,7 +94,6 @@ export const drawElement = async ({
rect,
borderRadius,
computedStyle,
- onlyBackgroundClip,
});
const finishOverflowHidden = setOverflowHidden({
diff --git a/packages/web-renderer/src/drawing/draw-outline.ts b/packages/web-renderer/src/drawing/draw-outline.ts
index 9db156652c..d1528879a5 100644
--- a/packages/web-renderer/src/drawing/draw-outline.ts
+++ b/packages/web-renderer/src/drawing/draw-outline.ts
@@ -26,18 +26,12 @@ export const drawOutline = ({
rect,
borderRadius,
computedStyle,
- onlyBackgroundClip,
}: {
ctx: OffscreenCanvasRenderingContext2D;
rect: DOMRect;
borderRadius: BorderRadiusCorners;
computedStyle: CSSStyleDeclaration;
- onlyBackgroundClip: boolean;
}) => {
- if (onlyBackgroundClip) {
- return;
- }
-
const outlineWidth = parseOutlineWidth(computedStyle.outlineWidth);
const {outlineStyle} = computedStyle;
const outlineColor = computedStyle.outlineColor || 'black';
diff --git a/packages/web-renderer/src/drawing/handle-mask.ts b/packages/web-renderer/src/drawing/handle-mask.ts
index 134a7dd5c3..c4a04a8893 100644
--- a/packages/web-renderer/src/drawing/handle-mask.ts
+++ b/packages/web-renderer/src/drawing/handle-mask.ts
@@ -33,6 +33,8 @@ export const handleMask = ({
ctx: tempContext,
rect: rectToFill,
gradientInfo,
+ offsetLeft: 0,
+ offsetTop: 0,
});
tempContext.globalCompositeOperation = 'destination-in';
diff --git a/packages/web-renderer/src/drawing/parse-linear-gradient.ts b/packages/web-renderer/src/drawing/parse-linear-gradient.ts
index 525a175c44..1f9be198f5 100644
--- a/packages/web-renderer/src/drawing/parse-linear-gradient.ts
+++ b/packages/web-renderer/src/drawing/parse-linear-gradient.ts
@@ -279,10 +279,14 @@ export const createCanvasGradient = ({
ctx,
rect,
gradientInfo,
+ offsetLeft,
+ offsetTop,
}: {
ctx: OffscreenCanvasRenderingContext2D;
rect: DOMRect;
gradientInfo: LinearGradientInfo;
+ offsetLeft: number;
+ offsetTop: number;
}): CanvasGradient => {
// Convert angle to radians
// CSS angles: 0deg = to top, 90deg = to right, 180deg = to bottom, 270deg = to left
@@ -290,8 +294,8 @@ export const createCanvasGradient = ({
const angleRad = ((gradientInfo.angle - 90) * Math.PI) / 180;
- const centerX = rect.left + rect.width / 2;
- const centerY = rect.top + rect.height / 2;
+ const centerX = rect.left - offsetLeft + rect.width / 2;
+ const centerY = rect.top - offsetTop + rect.height / 2;
// Calculate gradient line endpoints
// The gradient line passes through the center and has the specified angle
diff --git a/packages/web-renderer/src/drawing/process-node.ts b/packages/web-renderer/src/drawing/process-node.ts
index da26084e69..7620407eb1 100644
--- a/packages/web-renderer/src/drawing/process-node.ts
+++ b/packages/web-renderer/src/drawing/process-node.ts
@@ -26,7 +26,6 @@ export const processNode = async ({
parentRect,
internalState,
rootElement,
- onlyBackgroundClip,
}: {
element: HTMLElement | SVGElement;
context: OffscreenCanvasRenderingContext2D;
@@ -35,7 +34,6 @@ export const processNode = async ({
parentRect: DOMRect;
internalState: InternalState;
rootElement: HTMLElement | SVGElement;
- onlyBackgroundClip: boolean;
}): Promise<ProcessNodeReturnValue> => {
const {
totalMatrix,
@@ -181,7 +179,8 @@ export const processNode = async ({
totalMatrix,
parentRect,
logLevel,
- onlyBackgroundClip,
+ element,
+ internalState,
});
reset();
diff --git a/packages/web-renderer/src/drawing/text/handle-text-node.ts b/packages/web-renderer/src/drawing/text/handle-text-node.ts
index 5abe7ed301..4b07ba7385 100644
--- a/packages/web-renderer/src/drawing/text/handle-text-node.ts
+++ b/packages/web-renderer/src/drawing/text/handle-text-node.ts
@@ -18,7 +18,6 @@ export const handleTextNode = async ({
parentRect: DOMRect;
internalState: InternalState;
rootElement: HTMLElement | SVGElement;
- onlyBackgroundClip: boolean;
}): Promise<ProcessNodeReturnValue> => {
const span = document.createElement('span');
@@ -38,7 +37,6 @@ export const handleTextNode = async ({
parentRect,
internalState,
rootElement,
- onlyBackgroundClip: false,
});
// Undo the layout manipulation