Skip to content

🎥 Make videos programmatically with React

`@remotion/web-renderer`: Support gradient background size and position (#9737)

Jonny Burgercb287394e126
diff --git a/packages/docs/docs/client-side-rendering/limitations.mdx b/packages/docs/docs/client-side-rendering/limitations.mdx
index 8837cc52ad..1639e9384b 100644
--- a/packages/docs/docs/client-side-rendering/limitations.mdx
+++ b/packages/docs/docs/client-side-rendering/limitations.mdx
@@ -40,6 +40,7 @@ The `backface-visibility` property is <a style={{color: 'green'}}>supported</a>.
The `background-color` property is <a style={{color: 'green'}}>supported</a>.
`background-image` with a linear gradient is <a style={{color: 'green'}}>supported</a>.
+The `background-size` and `background-position` properties are <a style={{color: 'green'}}>supported</a> for linear gradients with pixel or percentage values.<br />
The `background-clip` property is <a style={{color: 'green'}}>supported</a>.
Other `background-image` values and other `background-*` properties are <a style={{color: 'red'}}>not supported</a>.
diff --git a/packages/web-renderer/src/drawing/background-image-rect.ts b/packages/web-renderer/src/drawing/background-image-rect.ts
new file mode 100644
index 0000000000..c2143e4592
--- /dev/null
+++ b/packages/web-renderer/src/drawing/background-image-rect.ts
@@ -0,0 +1,138 @@
+type LengthPercentage = {
+ unit: '%' | 'px';
+ value: number;
+};
+
+const firstLayer = (value: string) => value.split(',')[0].trim();
+
+const parseLengthPercentage = (value: string): LengthPercentage | null => {
+ const match = value.match(/^(-?(?:\d+\.?\d*|\.\d+))(%|px)$/);
+ if (match) {
+ return {
+ unit: match[2] as LengthPercentage['unit'],
+ value: Number(match[1]),
+ };
+ }
+
+ if (value === '0') {
+ return {unit: 'px', value: 0};
+ }
+
+ return null;
+};
+
+const resolveSize = ({
+ containerSize,
+ value,
+}: {
+ containerSize: number;
+ value: string | undefined;
+}) => {
+ if (value === undefined || value === 'auto') {
+ return containerSize;
+ }
+
+ const parsed = parseLengthPercentage(value);
+ if (!parsed) {
+ return containerSize;
+ }
+
+ return parsed.unit === '%'
+ ? containerSize * (parsed.value / 100)
+ : parsed.value;
+};
+
+const positionKeywordToPercentage = (value: string) => {
+ if (value === 'left' || value === 'top') {
+ return 0;
+ }
+
+ if (value === 'center') {
+ return 50;
+ }
+
+ if (value === 'right' || value === 'bottom') {
+ return 100;
+ }
+
+ return null;
+};
+
+const resolvePosition = ({
+ containerSize,
+ imageSize,
+ value,
+}: {
+ containerSize: number;
+ imageSize: number;
+ value: string;
+}) => {
+ const keywordPercentage = positionKeywordToPercentage(value);
+ if (keywordPercentage !== null) {
+ return (containerSize - imageSize) * (keywordPercentage / 100);
+ }
+
+ const parsed = parseLengthPercentage(value);
+ if (!parsed) {
+ return 0;
+ }
+
+ return parsed.unit === '%'
+ ? (containerSize - imageSize) * (parsed.value / 100)
+ : parsed.value;
+};
+
+const parsePosition = (value: string) => {
+ const parts = firstLayer(value).split(/\s+/);
+ if (parts.length === 1) {
+ if (parts[0] === 'top' || parts[0] === 'bottom') {
+ return {x: 'center', y: parts[0]};
+ }
+
+ return {x: parts[0], y: 'center'};
+ }
+
+ if (parts[0] === 'top' || parts[0] === 'bottom') {
+ return {x: parts[1], y: parts[0]};
+ }
+
+ return {x: parts[0], y: parts[1]};
+};
+
+export const getBackgroundImageRect = ({
+ backgroundPosition,
+ backgroundSize,
+ positioningArea,
+}: {
+ backgroundPosition: string;
+ backgroundSize: string;
+ positioningArea: DOMRect;
+}) => {
+ const sizeParts = firstLayer(backgroundSize).split(/\s+/);
+ const width = resolveSize({
+ containerSize: positioningArea.width,
+ value: sizeParts[0],
+ });
+ const height = resolveSize({
+ containerSize: positioningArea.height,
+ value: sizeParts[1],
+ });
+ const position = parsePosition(backgroundPosition);
+
+ return new DOMRect(
+ positioningArea.left +
+ resolvePosition({
+ containerSize: positioningArea.width,
+ imageSize: width,
+ value: position.x,
+ }),
+ positioningArea.top +
+ resolvePosition({
+ containerSize: positioningArea.height,
+ imageSize: height,
+ value: position.y,
+ }),
+ width,
+ height,
+ );
+};
diff --git a/packages/web-renderer/src/drawing/draw-background.ts b/packages/web-renderer/src/drawing/draw-background.ts
index 513a96bcda..075654bb5c 100644
--- a/packages/web-renderer/src/drawing/draw-background.ts
+++ b/packages/web-renderer/src/drawing/draw-background.ts
@@ -92,6 +92,8 @@ export const drawBackground = async ({
const backgroundFill = getBackgroundFill({
backgroundImage,
+ backgroundPosition: computedStyle.backgroundPosition,
+ backgroundSize: computedStyle.backgroundSize,
backgroundColor,
contextToDraw,
boundingRect,
diff --git a/packages/web-renderer/src/drawing/get-background-fill.ts b/packages/web-renderer/src/drawing/get-background-fill.ts
index d14353e77b..e589432782 100644
--- a/packages/web-renderer/src/drawing/get-background-fill.ts
+++ b/packages/web-renderer/src/drawing/get-background-fill.ts
@@ -1,3 +1,4 @@
+import {getBackgroundImageRect} from './background-image-rect';
import {
createCanvasGradient,
parseLinearGradient,
@@ -14,12 +15,16 @@ const isColorTransparent = (color: string) => {
export const getBackgroundFill = ({
backgroundColor,
backgroundImage,
+ backgroundPosition,
+ backgroundSize,
contextToDraw,
boundingRect,
offsetLeft,
offsetTop,
}: {
backgroundImage: string;
+ backgroundPosition: string;
+ backgroundSize: string;
backgroundColor: string;
contextToDraw: OffscreenCanvasRenderingContext2D;
boundingRect: DOMRect;
@@ -29,9 +34,14 @@ export const getBackgroundFill = ({
if (backgroundImage && backgroundImage !== 'none') {
const gradientInfo = parseLinearGradient(backgroundImage);
if (gradientInfo) {
+ const backgroundImageRect = getBackgroundImageRect({
+ backgroundPosition,
+ backgroundSize,
+ positioningArea: boundingRect,
+ });
const gradient = createCanvasGradient({
ctx: contextToDraw,
- rect: boundingRect,
+ rect: backgroundImageRect,
gradientInfo,
offsetLeft,
offsetTop,
diff --git a/packages/web-renderer/src/test/Root.tsx b/packages/web-renderer/src/test/Root.tsx
index 889ad0cabe..8f4b8dfa4f 100644
--- a/packages/web-renderer/src/test/Root.tsx
+++ b/packages/web-renderer/src/test/Root.tsx
@@ -40,6 +40,7 @@ import {issue7199ScaleAndDropShadow} from './fixtures/issue-7199-scale-and-drop-
import {issue7243SvgJapaneseText} from './fixtures/issue-7243-svg-japanese-text';
import {issue7489Minimal} from './fixtures/issue-7489-minimal';
import {issue8650LottieControlChars} from './fixtures/issue-8650-lottie-control-chars';
+import {issue9736BackgroundPosition} from './fixtures/issue-9736-background-position';
import {lineHeight} from './fixtures/line-height';
import {linearGradient} from './fixtures/linear-gradient';
import {manyLayers} from './fixtures/many-layers';
@@ -149,6 +150,7 @@ export const Root: React.FC = () => {
<Composition {...backfaceVisibilityMask} />
<Composition {...linearGradient} />
<Composition {...gradientTransparentKeyword} />
+ <Composition {...issue9736BackgroundPosition} />
</Folder>
<Composition {...outline} />
<Composition {...inlineScaleAfterOutline} />
diff --git a/packages/web-renderer/src/test/__screenshots__/issue-9736-background-position.test.tsx/issue-9736-background-position-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/issue-9736-background-position.test.tsx/issue-9736-background-position-chromium-darwin.png
new file mode 100644
index 0000000000..dc5da47993
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/issue-9736-background-position.test.tsx/issue-9736-background-position-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/issue-9736-background-position.test.tsx/issue-9736-background-position-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/issue-9736-background-position.test.tsx/issue-9736-background-position-firefox-darwin.png
new file mode 100644
index 0000000000..b6b0f20f02
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/issue-9736-background-position.test.tsx/issue-9736-background-position-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/issue-9736-background-position.test.tsx/issue-9736-background-position-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/issue-9736-background-position.test.tsx/issue-9736-background-position-webkit-darwin.png
new file mode 100644
index 0000000000..999cc7dad9
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/issue-9736-background-position.test.tsx/issue-9736-background-position-webkit-darwin.png differ
diff --git a/packages/web-renderer/src/test/background-image-rect.test.ts b/packages/web-renderer/src/test/background-image-rect.test.ts
new file mode 100644
index 0000000000..6197687420
--- /dev/null
+++ b/packages/web-renderer/src/test/background-image-rect.test.ts
@@ -0,0 +1,35 @@
+import {expect, test} from 'vitest';
+import {getBackgroundImageRect} from '../drawing/background-image-rect';
+
+test('positions an oversized percentage background image', () => {
+ const rect = getBackgroundImageRect({
+ backgroundPosition: '25% 0%',
+ backgroundSize: '220% 100%',
+ positioningArea: new DOMRect(10, 20, 100, 50),
+ });
+
+ expect(rect.x).toBeCloseTo(-20);
+ expect(rect.y).toBe(20);
+ expect(rect.width).toBeCloseTo(220);
+ expect(rect.height).toBe(50);
+});
+
+test('supports pixel sizes and positions', () => {
+ const rect = getBackgroundImageRect({
+ backgroundPosition: '12px 8px',
+ backgroundSize: '40px 30px',
+ positioningArea: new DOMRect(10, 20, 100, 50),
+ });
+
+ expect(rect).toEqual(new DOMRect(22, 28, 40, 30));
+});
+
+test('uses the positioning area for auto sizes', () => {
+ const rect = getBackgroundImageRect({
+ backgroundPosition: 'center',
+ backgroundSize: 'auto',
+ positioningArea: new DOMRect(10, 20, 100, 50),
+ });
+
+ expect(rect).toEqual(new DOMRect(10, 20, 100, 50));
+});
diff --git a/packages/web-renderer/src/test/fixtures/issue-9736-background-position.tsx b/packages/web-renderer/src/test/fixtures/issue-9736-background-position.tsx
new file mode 100644
index 0000000000..d909fc35ef
--- /dev/null
+++ b/packages/web-renderer/src/test/fixtures/issue-9736-background-position.tsx
@@ -0,0 +1,52 @@
+import {AbsoluteFill} from 'remotion';
+
+const Component: React.FC = () => {
+ return (
+ <AbsoluteFill
+ style={{
+ alignItems: 'center',
+ backgroundColor: '#f6f4ee',
+ justifyContent: 'center',
+ }}
+ >
+ <div
+ style={{
+ color: '#8c8880',
+ fontFamily: 'sans-serif',
+ fontSize: 72,
+ fontWeight: 600,
+ lineHeight: 1.18,
+ position: 'relative',
+ transform: 'rotateX(-12deg) rotateY(-16deg) rotateZ(-3deg)',
+ }}
+ >
+ <span>Thinking</span>
+ <span
+ style={{
+ backgroundClip: 'text',
+ backgroundImage:
+ 'linear-gradient(90deg, transparent 0%, transparent 34%, rgba(255, 255, 255, 0.8) 50%, transparent 66%, transparent 100%)',
+ backgroundPosition: '86% 0',
+ backgroundSize: '220% 100%',
+ color: 'transparent',
+ inset: 0,
+ lineHeight: 1.18,
+ position: 'absolute',
+ WebkitBackgroundClip: 'text',
+ }}
+ >
+ Thinking
+ </span>
+ </div>
+ </AbsoluteFill>
+ );
+};
+
+export const issue9736BackgroundPosition = {
+ component: Component,
+ durationInFrames: 1,
+ fps: 30,
+ height: 240,
+ id: 'issue-9736-background-position',
+ width: 480,
+} as const;
diff --git a/packages/web-renderer/src/test/issue-9736-background-position.test.tsx b/packages/web-renderer/src/test/issue-9736-background-position.test.tsx
new file mode 100644
index 0000000000..662799a208
--- /dev/null
+++ b/packages/web-renderer/src/test/issue-9736-background-position.test.tsx
@@ -0,0 +1,27 @@
+import {test} from 'vitest';
+import {page} from 'vitest/browser';
+import {renderStillOnWeb} from '../render-still-on-web';
+import '../symbol-dispose';
+import {issue9736BackgroundPosition} from './fixtures/issue-9736-background-position';
+import {testImage} from './utils';
+
+test('positions an oversized gradient clipped to text', async () => {
+ await page.viewport(
+ issue9736BackgroundPosition.width,
+ issue9736BackgroundPosition.height,
+ );
+ const still = await renderStillOnWeb({
+ licenseKey: 'free-license',
+ composition: issue9736BackgroundPosition,
+ frame: 0,
+ inputProps: {},
+ });
+ const blob = await still.blob({format: 'png'});
+
+ await testImage({
+ blob,
+ testId: 'issue-9736-background-position',
+ threshold: 0.02,
+ allowedMismatchedPixelRatio: 0.02,
+ });
+});