Skip to content

🎥 Make videos programmatically with React

`@remotion/web-renderer`: Support CSS shorthand transforms (`scale`, `rotate`

Jonny Burger40454b7e6dff
diff --git a/packages/web-renderer/src/drawing/calculate-transforms.ts b/packages/web-renderer/src/drawing/calculate-transforms.ts
index babda2b0a4..999eacf077 100644
--- a/packages/web-renderer/src/drawing/calculate-transforms.ts
+++ b/packages/web-renderer/src/drawing/calculate-transforms.ts
@@ -1,7 +1,7 @@
import {parseTransformOrigin} from './parse-transform-origin';
type Transform = {
- matrix: DOMMatrix;
+ matrices: DOMMatrix[];
rect: HTMLElement | SVGElement;
transformOrigin: string;
boundingClientRect: DOMRect | null;
@@ -59,18 +59,39 @@ export const calculateTransforms = (element: HTMLElement | SVGElement) => {
: computedStyle.transform;
const matrix = new DOMMatrix(toParse);
- const {transform} = parent.style;
+ const {transform, scale, rotate} = parent.style;
+ const additionalMatrices: DOMMatrix[] = [];
+
+ // The order of transformations is:
+ // 1. Translate --> We do not have to consider it since it changes getClientBoundingRect()
+ // 2. Rotate
+ // 3. Scale
+ // 4. CSS "transform"
+ if (rotate !== '') {
+ additionalMatrices.push(new DOMMatrix(`rotate(${rotate})`));
+ }
+
+ if (scale !== '') {
+ additionalMatrices.push(new DOMMatrix(`scale(${scale})`));
+ }
+
+ additionalMatrices.push(matrix);
+
parent.style.transform = 'none';
+ parent.style.scale = 'none';
+ parent.style.rotate = 'none';
transforms.push({
- matrix,
rect: parent,
transformOrigin: computedStyle.transformOrigin,
boundingClientRect: null,
+ matrices: additionalMatrices,
});
const parentRef = parent;
toReset.push(() => {
parentRef!.style.transform = transform;
+ parentRef!.style.scale = scale;
+ parentRef!.style.rotate = rotate;
});
}
@@ -90,14 +111,16 @@ export const calculateTransforms = (element: HTMLElement | SVGElement) => {
throw new Error('Bounding client rect not found');
}
- const globalTransformOrigin = getGlobalTransformOrigin(transform);
+ for (const matrix of transform.matrices) {
+ const globalTransformOrigin = getGlobalTransformOrigin(transform);
- const transformMatrix = new DOMMatrix()
- .translate(globalTransformOrigin.x, globalTransformOrigin.y)
- .multiply(transform.matrix)
- .translate(-globalTransformOrigin.x, -globalTransformOrigin.y);
+ const transformMatrix = new DOMMatrix()
+ .translate(globalTransformOrigin.x, globalTransformOrigin.y)
+ .multiply(matrix)
+ .translate(-globalTransformOrigin.x, -globalTransformOrigin.y);
- totalMatrix.multiplySelf(transformMatrix);
+ totalMatrix.multiplySelf(transformMatrix);
+ }
}
if (!elementComputedStyle) {
diff --git a/packages/web-renderer/src/test/Root.tsx b/packages/web-renderer/src/test/Root.tsx
index 2a8654a103..ed79cbea20 100644
--- a/packages/web-renderer/src/test/Root.tsx
+++ b/packages/web-renderer/src/test/Root.tsx
@@ -32,6 +32,10 @@ import {textFixture} from './fixtures/text/text';
import {textTransform} from './fixtures/text/text-transform';
import {threeLevelTransformOrigins} from './fixtures/three-level-transform-origins';
import {orthographic} from './fixtures/transforms/orthographic';
+import {transformWithAllShorthands} from './fixtures/transforms/transform-with-all-shorthands';
+import {transformWithRotate} from './fixtures/transforms/transform-with-rotate';
+import {transformWithScale} from './fixtures/transforms/transform-with-scale';
+import {transformWithTranslate} from './fixtures/transforms/transform-with-translate';
import {withMargin} from './fixtures/transforms/with-margin';
import {withNegativeMargin} from './fixtures/transforms/with-negative-margin';
import {unwrapped} from './fixtures/unwrapped';
@@ -76,6 +80,10 @@ export const Root: React.FC = () => {
<Composition {...orthographic} />
<Composition {...withMargin} />
<Composition {...withNegativeMargin} />
+ <Composition {...transformWithScale} />
+ <Composition {...transformWithRotate} />
+ <Composition {...transformWithTranslate} />
+ <Composition {...transformWithAllShorthands} />
</Folder>
</>
);
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-all-shorthands-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-all-shorthands-chromium-darwin.png
new file mode 100644
index 0000000000..06a67f717c
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-all-shorthands-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-all-shorthands-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-all-shorthands-firefox-darwin.png
new file mode 100644
index 0000000000..e43c851830
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-all-shorthands-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-all-shorthands-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-all-shorthands-webkit-darwin.png
new file mode 100644
index 0000000000..1a56900246
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-all-shorthands-webkit-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-rotate-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-rotate-chromium-darwin.png
new file mode 100644
index 0000000000..f81cb2362f
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-rotate-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-rotate-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-rotate-firefox-darwin.png
new file mode 100644
index 0000000000..049b86d97b
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-rotate-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-rotate-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-rotate-webkit-darwin.png
new file mode 100644
index 0000000000..34707d0c0c
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-rotate-webkit-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-scale-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-scale-chromium-darwin.png
new file mode 100644
index 0000000000..1d92a9551a
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-scale-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-scale-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-scale-firefox-darwin.png
new file mode 100644
index 0000000000..87830fd26a
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-scale-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-scale-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-scale-webkit-darwin.png
new file mode 100644
index 0000000000..1eae2dfac2
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-scale-webkit-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-translate-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-translate-chromium-darwin.png
new file mode 100644
index 0000000000..93f7111aee
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-translate-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-translate-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-translate-firefox-darwin.png
new file mode 100644
index 0000000000..7ca12c45e3
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-translate-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-translate-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-translate-webkit-darwin.png
new file mode 100644
index 0000000000..46b66af924
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-translate-webkit-darwin.png differ
diff --git a/packages/web-renderer/src/test/fixtures/transforms/transform-with-all-shorthands.tsx b/packages/web-renderer/src/test/fixtures/transforms/transform-with-all-shorthands.tsx
new file mode 100644
index 0000000000..e6b9ca5d07
--- /dev/null
+++ b/packages/web-renderer/src/test/fixtures/transforms/transform-with-all-shorthands.tsx
@@ -0,0 +1,38 @@
+const TransformWithAllShorthands: React.FC = () => {
+ return (
+ <div
+ style={{
+ position: 'absolute',
+ inset: 0,
+ width: 1080,
+ height: 1080,
+ display: 'flex',
+ justifyContent: 'center',
+ alignItems: 'center',
+ overflow: 'hidden',
+ backgroundColor: 'rgb(50, 200, 150)',
+ }}
+ >
+ <div
+ style={{
+ width: 400,
+ height: 400,
+ backgroundColor: 'rgb(100, 255, 150)',
+ transform: 'skewX(10deg)',
+ scale: '0.6',
+ rotate: '45deg',
+ translate: '-50px 100px',
+ }}
+ />
+ </div>
+ );
+};
+
+export const transformWithAllShorthands = {
+ component: TransformWithAllShorthands,
+ id: 'transform-with-all-shorthands',
+ width: 1080,
+ height: 1080,
+ fps: 30,
+ durationInFrames: 1,
+} as const;
diff --git a/packages/web-renderer/src/test/fixtures/transforms/transform-with-rotate.tsx b/packages/web-renderer/src/test/fixtures/transforms/transform-with-rotate.tsx
new file mode 100644
index 0000000000..285114b440
--- /dev/null
+++ b/packages/web-renderer/src/test/fixtures/transforms/transform-with-rotate.tsx
@@ -0,0 +1,36 @@
+const TransformWithRotate: React.FC = () => {
+ return (
+ <div
+ style={{
+ position: 'absolute',
+ inset: 0,
+ width: 1080,
+ height: 1080,
+ display: 'flex',
+ justifyContent: 'center',
+ alignItems: 'center',
+ overflow: 'hidden',
+ backgroundColor: 'rgb(100, 200, 255)',
+ }}
+ >
+ <div
+ style={{
+ width: 400,
+ height: 400,
+ backgroundColor: 'rgb(100, 100, 255)',
+ transform: 'scale(0.7) translateX(100px)',
+ rotate: '30deg',
+ }}
+ />
+ </div>
+ );
+};
+
+export const transformWithRotate = {
+ component: TransformWithRotate,
+ id: 'transform-with-rotate',
+ width: 1080,
+ height: 1080,
+ fps: 30,
+ durationInFrames: 1,
+} as const;
diff --git a/packages/web-renderer/src/test/fixtures/transforms/transform-with-scale.tsx b/packages/web-renderer/src/test/fixtures/transforms/transform-with-scale.tsx
new file mode 100644
index 0000000000..c2292d68e3
--- /dev/null
+++ b/packages/web-renderer/src/test/fixtures/transforms/transform-with-scale.tsx
@@ -0,0 +1,36 @@
+const TransformWithScale: React.FC = () => {
+ return (
+ <div
+ style={{
+ position: 'absolute',
+ inset: 0,
+ width: 1080,
+ height: 1080,
+ display: 'flex',
+ justifyContent: 'center',
+ alignItems: 'center',
+ overflow: 'hidden',
+ backgroundColor: 'rgb(255, 200, 100)',
+ }}
+ >
+ <div
+ style={{
+ width: 400,
+ height: 400,
+ backgroundColor: 'rgb(255, 100, 100)',
+ transform: 'rotate(45deg)',
+ scale: '0.5',
+ }}
+ />
+ </div>
+ );
+};
+
+export const transformWithScale = {
+ component: TransformWithScale,
+ id: 'transform-with-scale',
+ width: 1080,
+ height: 1080,
+ fps: 30,
+ durationInFrames: 1,
+} as const;
diff --git a/packages/web-renderer/src/test/fixtures/transforms/transform-with-translate.tsx b/packages/web-renderer/src/test/fixtures/transforms/transform-with-translate.tsx
new file mode 100644
index 0000000000..bddb501551
--- /dev/null
+++ b/packages/web-renderer/src/test/fixtures/transforms/transform-with-translate.tsx
@@ -0,0 +1,36 @@
+const TransformWithTranslate: React.FC = () => {
+ return (
+ <div
+ style={{
+ position: 'absolute',
+ inset: 0,
+ width: 1080,
+ height: 1080,
+ display: 'flex',
+ justifyContent: 'center',
+ alignItems: 'center',
+ overflow: 'hidden',
+ backgroundColor: 'rgb(200, 100, 255)',
+ }}
+ >
+ <div
+ style={{
+ width: 400,
+ height: 400,
+ backgroundColor: 'rgb(150, 50, 200)',
+ transform: 'scale(0.8) rotate(15deg)',
+ translate: '100px 50px',
+ }}
+ />
+ </div>
+ );
+};
+
+export const transformWithTranslate = {
+ component: TransformWithTranslate,
+ id: 'transform-with-translate',
+ width: 1080,
+ height: 1080,
+ fps: 30,
+ durationInFrames: 1,
+} as const;
diff --git a/packages/web-renderer/src/test/transform-shorthands.test.tsx b/packages/web-renderer/src/test/transform-shorthands.test.tsx
new file mode 100644
index 0000000000..ef4dbf23a6
--- /dev/null
+++ b/packages/web-renderer/src/test/transform-shorthands.test.tsx
@@ -0,0 +1,60 @@
+import {test} from 'vitest';
+import {page} from 'vitest/browser';
+import {renderStillOnWeb} from '../render-still-on-web';
+import {transformWithAllShorthands} from './fixtures/transforms/transform-with-all-shorthands';
+import {transformWithRotate} from './fixtures/transforms/transform-with-rotate';
+import {transformWithScale} from './fixtures/transforms/transform-with-scale';
+import {transformWithTranslate} from './fixtures/transforms/transform-with-translate';
+import {testImage} from './utils';
+
+test('Should combine transform property with scale shorthand', async () => {
+ await page.viewport(1080, 1080);
+
+ const blob = await renderStillOnWeb({
+ composition: transformWithScale,
+ frame: 0,
+ inputProps: {},
+ imageFormat: 'png',
+ });
+
+ await testImage({blob, testId: 'transform-with-scale'});
+});
+
+test('Should combine transform property with rotate shorthand', async () => {
+ await page.viewport(1080, 1080);
+
+ const blob = await renderStillOnWeb({
+ composition: transformWithRotate,
+ frame: 0,
+ inputProps: {},
+ imageFormat: 'png',
+ });
+
+ await testImage({blob, testId: 'transform-with-rotate'});
+});
+
+test('Should combine transform property with translate shorthand', async () => {
+ await page.viewport(1080, 1080);
+
+ const blob = await renderStillOnWeb({
+ composition: transformWithTranslate,
+ frame: 0,
+ inputProps: {},
+ imageFormat: 'png',
+ });
+
+ await testImage({blob, testId: 'transform-with-translate'});
+});
+
+test('Should combine transform property with all CSS transform shorthands', async () => {
+ await page.viewport(1080, 1080);
+
+ const blob = await renderStillOnWeb({
+ composition: transformWithAllShorthands,
+ frame: 0,
+ inputProps: {},
+ imageFormat: 'png',
+ });
+
+ await testImage({blob, testId: 'transform-with-all-shorthands'});
+});