Skip to content

🎥 Make videos programmatically with React

different background clip values

Jonny Burger5ca018f25311
diff --git a/packages/web-renderer/src/drawing/draw-background.ts b/packages/web-renderer/src/drawing/draw-background.ts
index 6b796d226a..aeb21397b8 100644
--- a/packages/web-renderer/src/drawing/draw-background.ts
+++ b/packages/web-renderer/src/drawing/draw-background.ts
@@ -1,6 +1,7 @@
import type {LogLevel} from 'remotion';
import type {InternalState} from '../internal-state';
import {getClippedBackground} from './get-clipped-background';
+import {getBoxBasedOnBackgroundClip} from './get-padding-box';
import {
createCanvasGradient,
parseLinearGradient,
@@ -11,19 +12,21 @@ export const drawBackground = async ({
context,
rect,
backgroundColor,
- backgroundClipText,
+ backgroundClip,
element,
logLevel,
internalState,
+ computedStyle,
}: {
backgroundImage: string;
context: OffscreenCanvasRenderingContext2D;
rect: DOMRect;
backgroundColor: string;
- backgroundClipText: boolean;
+ backgroundClip: string;
element: HTMLElement | SVGElement;
logLevel: LogLevel;
internalState: InternalState;
+ computedStyle: CSSStyleDeclaration;
}) => {
let contextToDraw = context;
@@ -44,67 +47,77 @@ export const drawBackground = async ({
}
};
- 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;
- contextToDraw.globalCompositeOperation = 'source-in';
- }
+ const boundingRects = getBoxBasedOnBackgroundClip(
+ rect,
+ computedStyle,
+ backgroundClip,
+ );
- if (backgroundImage && backgroundImage !== 'none') {
- const gradientInfo = parseLinearGradient(backgroundImage);
- if (gradientInfo) {
- const gradient = createCanvasGradient({
- ctx: contextToDraw,
- rect,
- gradientInfo,
- offsetLeft,
- offsetTop,
+ console.log(boundingRects);
+
+ 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;
+ 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 = gradient;
+ contextToDraw.fillStyle = backgroundColor;
contextToDraw.fillRect(
- rect.left - offsetLeft,
- rect.top - offsetTop,
- rect.width,
- rect.height,
+ boundingRect.left - offsetLeft,
+ boundingRect.top - offsetLeft,
+ 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(
- 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 35005bd230..fc5d4e3b1f 100644
--- a/packages/web-renderer/src/drawing/draw-element.ts
+++ b/packages/web-renderer/src/drawing/draw-element.ts
@@ -33,7 +33,7 @@ export const drawElement = async ({
element: HTMLElement | SVGElement;
internalState: InternalState;
}) => {
- const {backgroundImage, backgroundColor} = computedStyle;
+ const {backgroundImage, backgroundColor, backgroundClip} = computedStyle;
const borderRadius = parseBorderRadius({
borderRadius: computedStyle.borderRadius,
width: rect.width,
@@ -71,10 +71,11 @@ export const drawElement = async ({
context,
rect,
backgroundColor,
- backgroundClipText: computedStyle.backgroundClip.includes('text'),
+ backgroundClip,
element,
logLevel,
internalState,
+ computedStyle,
});
await draw({dimensions: rect, computedStyle, contextToDraw: context});
diff --git a/packages/web-renderer/src/drawing/get-padding-box.ts b/packages/web-renderer/src/drawing/get-padding-box.ts
new file mode 100644
index 0000000000..69dfc8a700
--- /dev/null
+++ b/packages/web-renderer/src/drawing/get-padding-box.ts
@@ -0,0 +1,54 @@
+const getPaddingBox = (
+ rect: DOMRect,
+ computedStyle: CSSStyleDeclaration,
+): DOMRect => {
+ const borderLeft = parseFloat(computedStyle.borderLeftWidth);
+ const borderRight = parseFloat(computedStyle.borderRightWidth);
+ const borderTop = parseFloat(computedStyle.borderTopWidth);
+ const borderBottom = parseFloat(computedStyle.borderBottomWidth);
+
+ return new DOMRect(
+ rect.left + borderLeft,
+ rect.top + borderTop,
+ rect.width - borderLeft - borderRight,
+ rect.height - borderTop - borderBottom,
+ );
+};
+
+const getContentBox = (
+ rect: DOMRect,
+ computedStyle: CSSStyleDeclaration,
+): DOMRect => {
+ const paddingBox = getPaddingBox(rect, computedStyle);
+ const paddingLeft = parseFloat(computedStyle.paddingLeft);
+ const paddingRight = parseFloat(computedStyle.paddingRight);
+ const paddingTop = parseFloat(computedStyle.paddingTop);
+ const paddingBottom = parseFloat(computedStyle.paddingBottom);
+
+ return new DOMRect(
+ paddingBox.left + paddingLeft,
+ paddingBox.top + paddingTop,
+ paddingBox.width - paddingLeft - paddingRight,
+ paddingBox.height - paddingTop - paddingBottom,
+ );
+};
+
+export const getBoxBasedOnBackgroundClip = (
+ rect: DOMRect,
+ computedStyle: CSSStyleDeclaration,
+ backgroundClip: string,
+) => {
+ if (backgroundClip.includes('text')) {
+ return [rect];
+ }
+
+ if (backgroundClip.includes('padding-box')) {
+ return [getPaddingBox(rect, computedStyle)];
+ }
+
+ if (backgroundClip.includes('content-box')) {
+ return [getContentBox(rect, computedStyle)];
+ }
+
+ return [rect, getPaddingBox(rect, computedStyle)];
+};
diff --git a/packages/web-renderer/src/test/fixtures/text/background-clip-text.tsx b/packages/web-renderer/src/test/fixtures/text/background-clip-text.tsx
index 9809576e14..eda62042a4 100644
--- a/packages/web-renderer/src/test/fixtures/text/background-clip-text.tsx
+++ b/packages/web-renderer/src/test/fixtures/text/background-clip-text.tsx
@@ -1,28 +1,60 @@
import React from 'react';
import {AbsoluteFill} from 'remotion';
+const baseStyle: React.CSSProperties = {
+ border: '0.8em darkviolet',
+ borderStyle: 'dashed',
+ margin: '1em 0',
+ padding: '1.4em',
+ background: 'linear-gradient(60deg, red, yellow, red, yellow, red)',
+ fontWeight: 900,
+ fontSize: '1.2em',
+ fontFamily: 'sans-serif',
+};
+
const Component: React.FC = () => {
return (
<AbsoluteFill
style={{
- justifyContent: 'center',
- alignItems: 'center',
backgroundColor: 'white',
+ padding: 20,
+ flexDirection: 'column',
}}
>
- <div
+ <p
style={{
- fontSize: 60,
- fontWeight: 'bold',
- background: 'linear-gradient(90deg, #ff0000, #0000ff)',
- WebkitBackgroundClip: 'text',
+ ...baseStyle,
+ backgroundClip: 'border-box',
+ }}
+ >
+ The background extends behind the border.
+ </p>
+ <p
+ style={{
+ ...baseStyle,
+ backgroundClip: 'padding-box',
+ }}
+ >
+ The background extends to the inside edge of the border.
+ </p>
+ <p
+ style={{
+ ...baseStyle,
+ backgroundClip: 'content-box',
+ }}
+ >
+ The background extends only to the edge of the content box.
+ </p>
+ <p
+ style={{
+ ...baseStyle,
backgroundClip: 'text',
- WebkitTextFillColor: 'transparent',
- color: 'transparent',
+ WebkitBackgroundClip: 'text',
+ color: 'rgba(0, 0, 0, 0.2)',
}}
>
- Gradient Text
- </div>
+ The background is clipped to the foreground text.
+ </p>
</AbsoluteFill>
);
};
@@ -31,7 +63,7 @@ export const backgroundClipText = {
component: Component,
id: 'background-clip-text',
width: 400,
- height: 200,
+ height: 700,
fps: 30,
durationInFrames: 1,
} as const;