Skip to content

🎥 Make videos programmatically with React

`@remotion/web-renderer`: Support multi-component scale values (#9438)

Jonny Burger98135f24ec82
diff --git a/packages/web-renderer/src/drawing/calculate-transforms.ts b/packages/web-renderer/src/drawing/calculate-transforms.ts
index 31f38a84fe..072516b66d 100644
--- a/packages/web-renderer/src/drawing/calculate-transforms.ts
+++ b/packages/web-renderer/src/drawing/calculate-transforms.ts
@@ -1,4 +1,5 @@
import {hasAnyTransformCssValue, hasTransformCssValue} from './has-transform';
+import {makeDOMMatrix} from './make-dom-matrix';
import {getMaskImageValue, parseMaskImage} from './mask-image';
import type {LinearGradientInfo} from './parse-linear-gradient';
import {parseTransformOrigin} from './parse-transform-origin';
@@ -208,7 +209,7 @@ export const calculateTransforms = ({
hasApplicableTransformCssValue && hasTransformCssValue(transformStyle)
? transformStyle.transform
: undefined;
- const matrix = new DOMMatrix(toParse);
+ const matrix = makeDOMMatrix(toParse);
const resetTransforms = makeTransformResetter(parent);
const {scale, rotate} = parent.style;
@@ -224,11 +225,11 @@ export const calculateTransforms = ({
rotate !== '' &&
rotate !== 'none'
) {
- additionalMatrices.push(new DOMMatrix(`rotate(${rotate})`));
+ additionalMatrices.push(makeDOMMatrix(`rotate(${rotate})`));
}
if (hasApplicableTransformCssValue && scale !== '' && scale !== 'none') {
- additionalMatrices.push(new DOMMatrix(`scale(${scale})`));
+ additionalMatrices.push(makeDOMMatrix(`scale(${scale})`));
}
additionalMatrices.push(matrix);
diff --git a/packages/web-renderer/src/drawing/make-dom-matrix.ts b/packages/web-renderer/src/drawing/make-dom-matrix.ts
new file mode 100644
index 0000000000..05787e4356
--- /dev/null
+++ b/packages/web-renderer/src/drawing/make-dom-matrix.ts
@@ -0,0 +1,84 @@
+const parseScaleComponent = (component: string) => {
+ if (component.endsWith('%')) {
+ return Number(component.slice(0, -1)) / 100;
+ }
+
+ return Number(component);
+};
+
+const parseScale = (transform: string) => {
+ const match = /^scale\((.*)\)$/.exec(transform);
+ if (!match) {
+ return null;
+ }
+
+ const scaleValue = match[1].trim();
+ if (scaleValue === '') {
+ return null;
+ }
+
+ const components = scaleValue.split(/\s+/).map(parseScaleComponent);
+ if (
+ components.length < 1 ||
+ components.length > 3 ||
+ components.some((component) => !Number.isFinite(component))
+ ) {
+ return null;
+ }
+
+ return {
+ x: components[0],
+ y: components[1] ?? components[0],
+ z: components[2] ?? 1,
+ };
+};
+
+const parseAxisRotate = (transform: string) => {
+ const match = /^rotate\((.*)\)$/.exec(transform);
+ if (!match) {
+ return null;
+ }
+
+ const rotateValue = match[1].trim();
+ const keywordAxis = /^(x|y|z)\s+(.+)$/i.exec(rotateValue);
+ if (keywordAxis) {
+ const axisKeyword = keywordAxis[1].toLowerCase();
+ const angle = keywordAxis[2];
+ const rotateFunction =
+ axisKeyword === 'x'
+ ? 'rotateX'
+ : axisKeyword === 'y'
+ ? 'rotateY'
+ : 'rotate';
+
+ return `${rotateFunction}(${angle})`;
+ }
+
+ const vectorAxis = /^(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/.exec(rotateValue);
+ if (!vectorAxis) {
+ return null;
+ }
+
+ const axisVector = vectorAxis.slice(1, 4).map(Number);
+ if (axisVector.some((component) => !Number.isFinite(component))) {
+ return null;
+ }
+
+ return `rotate3d(${axisVector.join(', ')}, ${vectorAxis[4]})`;
+};
+
+export const makeDOMMatrix = (transform?: string) => {
+ if (transform) {
+ const scale = parseScale(transform);
+ if (scale) {
+ return new DOMMatrix().scale(scale.x, scale.y, scale.z);
+ }
+
+ const axisRotate = parseAxisRotate(transform);
+ if (axisRotate) {
+ return new DOMMatrix(axisRotate);
+ }
+ }
+
+ return new DOMMatrix(transform);
+};
diff --git a/packages/web-renderer/src/test/Root.tsx b/packages/web-renderer/src/test/Root.tsx
index b16e7255b5..46002d5223 100644
--- a/packages/web-renderer/src/test/Root.tsx
+++ b/packages/web-renderer/src/test/Root.tsx
@@ -91,6 +91,8 @@ import {threeDTransformOutOfBounds} from './fixtures/three-d-transform-out-of-bo
import {threeLevelTransformOrigins} from './fixtures/three-level-transform-origins';
import {orthographic} from './fixtures/transforms/orthographic';
import {transformWithAllShorthands} from './fixtures/transforms/transform-with-all-shorthands';
+import {transformWithAxisRotate} from './fixtures/transforms/transform-with-axis-rotate';
+import {transformWithMultiComponentScale} from './fixtures/transforms/transform-with-multi-component-scale';
import {transformWithRotate} from './fixtures/transforms/transform-with-rotate';
import {transformWithScale} from './fixtures/transforms/transform-with-scale';
import {transformWithTranslate} from './fixtures/transforms/transform-with-translate';
@@ -193,7 +195,9 @@ export const Root: React.FC = () => {
<Composition {...withMargin} />
<Composition {...withNegativeMargin} />
<Composition {...transformWithScale} />
+ <Composition {...transformWithMultiComponentScale} />
<Composition {...transformWithRotate} />
+ <Composition {...transformWithAxisRotate} />
<Composition {...transformWithTranslate} />
<Composition {...transformWithAllShorthands} />
<Composition {...inside3dTransform} />
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-axis-rotate-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-axis-rotate-chromium-darwin.png
new file mode 100644
index 0000000000..6d455d3b43
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-axis-rotate-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-axis-rotate-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-axis-rotate-firefox-darwin.png
new file mode 100644
index 0000000000..ba4d5eb874
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-axis-rotate-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-axis-rotate-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-axis-rotate-webkit-darwin.png
new file mode 100644
index 0000000000..c6ec03ba55
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-axis-rotate-webkit-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-multi-component-scale-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-multi-component-scale-chromium-darwin.png
new file mode 100644
index 0000000000..e5ad4f7d8b
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-multi-component-scale-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-multi-component-scale-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-multi-component-scale-firefox-darwin.png
new file mode 100644
index 0000000000..af1a358c93
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-multi-component-scale-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-multi-component-scale-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-multi-component-scale-webkit-darwin.png
new file mode 100644
index 0000000000..b13b24221d
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/transform-shorthands.test.tsx/transform-with-multi-component-scale-webkit-darwin.png differ
diff --git a/packages/web-renderer/src/test/fixtures/transforms/transform-with-axis-rotate.tsx b/packages/web-renderer/src/test/fixtures/transforms/transform-with-axis-rotate.tsx
new file mode 100644
index 0000000000..dd30420295
--- /dev/null
+++ b/packages/web-renderer/src/test/fixtures/transforms/transform-with-axis-rotate.tsx
@@ -0,0 +1,35 @@
+const TransformWithAxisRotate: 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)',
+ rotate: 'x 45deg',
+ }}
+ />
+ </div>
+ );
+};
+
+export const transformWithAxisRotate = {
+ component: TransformWithAxisRotate,
+ id: 'transform-with-axis-rotate',
+ width: 1080,
+ height: 1080,
+ fps: 30,
+ durationInFrames: 1,
+} as const;
diff --git a/packages/web-renderer/src/test/fixtures/transforms/transform-with-multi-component-scale.tsx b/packages/web-renderer/src/test/fixtures/transforms/transform-with-multi-component-scale.tsx
new file mode 100644
index 0000000000..e6121c6e6f
--- /dev/null
+++ b/packages/web-renderer/src/test/fixtures/transforms/transform-with-multi-component-scale.tsx
@@ -0,0 +1,35 @@
+const TransformWithMultiComponentScale: 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)',
+ scale: '0.5 1.5',
+ }}
+ />
+ </div>
+ );
+};
+
+export const transformWithMultiComponentScale = {
+ component: TransformWithMultiComponentScale,
+ id: 'transform-with-multi-component-scale',
+ width: 1080,
+ height: 1080,
+ fps: 30,
+ durationInFrames: 1,
+} as const;
diff --git a/packages/web-renderer/src/test/make-dom-matrix.test.ts b/packages/web-renderer/src/test/make-dom-matrix.test.ts
new file mode 100644
index 0000000000..c607e29337
--- /dev/null
+++ b/packages/web-renderer/src/test/make-dom-matrix.test.ts
@@ -0,0 +1,58 @@
+import {expect, test} from 'vitest';
+import {makeDOMMatrix} from '../drawing/make-dom-matrix';
+
+test('Should make a matrix from a multi-component scale', () => {
+ const matrix = makeDOMMatrix('scale(2 3)');
+
+ expect(matrix.m11).toBe(2);
+ expect(matrix.m22).toBe(3);
+ expect(matrix.m33).toBe(1);
+ expect(matrix.is2D).toBe(true);
+});
+
+test('Should make a matrix from a three-dimensional scale', () => {
+ const matrix = makeDOMMatrix('scale(2 3 4)');
+
+ expect(matrix.m11).toBe(2);
+ expect(matrix.m22).toBe(3);
+ expect(matrix.m33).toBe(4);
+ expect(matrix.is2D).toBe(false);
+});
+
+test('Should make a matrix from percentage scales', () => {
+ const matrix = makeDOMMatrix('scale(50% 75%)');
+
+ expect(matrix.m11).toBe(0.5);
+ expect(matrix.m22).toBe(0.75);
+});
+
+test.each([
+ ['x', 'rotateX(45deg)'],
+ ['y', 'rotateY(45deg)'],
+ ['z', 'rotate(45deg)'],
+])('Should make a matrix from a %s-axis rotation', (axis, transform) => {
+ const matrix = makeDOMMatrix(`rotate(${axis} 45deg)`);
+ const expected = new DOMMatrix(transform);
+
+ expect(Array.from(matrix.toFloat64Array())).toEqual(
+ Array.from(expected.toFloat64Array()),
+ );
+});
+
+test('Should make a matrix from a vector-axis rotation', () => {
+ const matrix = makeDOMMatrix('rotate(1 2 3 45deg)');
+ const expected = new DOMMatrix('rotate3d(1, 2, 3, 45deg)');
+
+ expect(Array.from(matrix.toFloat64Array())).toEqual(
+ Array.from(expected.toFloat64Array()),
+ );
+});
+
+test('Should pass other transforms to DOMMatrix', () => {
+ const matrix = makeDOMMatrix('matrix(2, 0, 0, 3, 4, 5)');
+
+ expect(matrix.m11).toBe(2);
+ expect(matrix.m22).toBe(3);
+ expect(matrix.m41).toBe(4);
+ expect(matrix.m42).toBe(5);
+});
diff --git a/packages/web-renderer/src/test/transform-shorthands.test.tsx b/packages/web-renderer/src/test/transform-shorthands.test.tsx
index 83bd3c4bef..dea8cade0c 100644
--- a/packages/web-renderer/src/test/transform-shorthands.test.tsx
+++ b/packages/web-renderer/src/test/transform-shorthands.test.tsx
@@ -3,6 +3,8 @@ import {page} from 'vitest/browser';
import {renderStillOnWeb} from '../render-still-on-web';
import '../symbol-dispose';
import {transformWithAllShorthands} from './fixtures/transforms/transform-with-all-shorthands';
+import {transformWithAxisRotate} from './fixtures/transforms/transform-with-axis-rotate';
+import {transformWithMultiComponentScale} from './fixtures/transforms/transform-with-multi-component-scale';
import {transformWithRotate} from './fixtures/transforms/transform-with-rotate';
import {transformWithScale} from './fixtures/transforms/transform-with-scale';
import {transformWithTranslate} from './fixtures/transforms/transform-with-translate';
@@ -23,6 +25,21 @@ test('Should combine transform property with scale shorthand', async () => {
await testImage({blob, testId: 'transform-with-scale'});
});
+test('Should support a scale shorthand with multiple components', async () => {
+ await page.viewport(1080, 1080);
+
+ const blob = await (
+ await renderStillOnWeb({
+ licenseKey: 'free-license',
+ composition: transformWithMultiComponentScale,
+ frame: 0,
+ inputProps: {},
+ })
+ ).blob({format: 'png'});
+
+ await testImage({blob, testId: 'transform-with-multi-component-scale'});
+});
+
test('Should combine transform property with rotate shorthand', async () => {
await page.viewport(1080, 1080);
@@ -38,6 +55,21 @@ test('Should combine transform property with rotate shorthand', async () => {
await testImage({blob, testId: 'transform-with-rotate'});
});
+test('Should support a rotate shorthand with an axis', async () => {
+ await page.viewport(1080, 1080);
+
+ const blob = await (
+ await renderStillOnWeb({
+ licenseKey: 'free-license',
+ composition: transformWithAxisRotate,
+ frame: 0,
+ inputProps: {},
+ })
+ ).blob({format: 'png'});
+
+ await testImage({blob, testId: 'transform-with-axis-rotate'});
+});
+
test('Should combine transform property with translate shorthand', async () => {
await page.viewport(1080, 1080);