Skip to content

🎥 Make videos programmatically with React

refactor makeTransform

Jonny Burgerab2ec109df7b
diff --git a/packages/animation-utils/src/test/make-transform.test.ts b/packages/animation-utils/src/test/make-transform.test.ts
index e6857dfba6..f805ba7e23 100644
--- a/packages/animation-utils/src/test/make-transform.test.ts
+++ b/packages/animation-utils/src/test/make-transform.test.ts
@@ -1,8 +1,17 @@
import {expect, test} from 'vitest';
import {
makeTransform,
+ perspective,
+ rotate,
+ rotate3d,
scale,
+ skew,
+ skewX,
+ translate,
+ translate3d,
translateX,
+ translateY,
+ translateZ,
} from '../transformation-helpers/make-transform';
test('makeTransform() should work well - base case', () => {
@@ -17,3 +26,60 @@ test('makeTransform() should work well - forgetting to pass a param', () => {
'Argument passed to "scale" for param "x" is undefined',
);
});
+
+test('makeTransform() should allow strings with units', () => {
+ expect(makeTransform([rotate('12rad'), rotate3d(10, 20, 40, '12deg')])).toBe(
+ 'rotate(12rad) rotate3d(10, 20, 40, 12deg)',
+ );
+});
+
+test('makeTransform() should handle skew', () => {
+ expect(skew(10)).toBe('skew(10deg, 10deg)');
+ expect(skew(10, 'rad')).toBe('skew(10rad, 10rad)');
+ // @ts-expect-error
+ expect(() => skew(10, 'rad', 1)).toThrow(/supports only the following /);
+ expect(skewX(10, 'rad')).toBe('skewX(10rad)');
+ expect(skewX('10rad')).toBe('skewX(10rad)');
+});
+
+test('makeTransform() should handle rotate', () => {
+ expect(rotate(10)).toBe('rotate(10deg)');
+ expect(rotate('10deg')).toBe('rotate(10deg)');
+ expect(rotate(12, 'rad')).toBe('rotate(12rad)');
+});
+
+test('makeTransform() should handle translate', () => {
+ expect(translate(10)).toBe('translate(10px)');
+ expect(translate('12rem')).toBe('translate(12rem)');
+ expect(translate(10, 10)).toBe('translate(10px, 10px)');
+ expect(translate(10, '%')).toBe('translate(10%)');
+ expect(translate(0, '%', 10, '%')).toBe('translate(0%, 10%)');
+ expect(translate('10px', '30%')).toBe('translate(10px, 30%)');
+ // @ts-expect-error
+ expect(() => translate(0, '%', 10)).toThrow(
+ /supports only the following signatures/,
+ );
+});
+
+test('makeTransform() should handle translate3d', () => {
+ expect(translate3d('12px', '12px', '10px')).toBe(
+ 'translate3d(12px, 12px, 10px)',
+ );
+ expect(translate3d(10, 0, 0)).toBe('translate3d(10px, 0px, 0px)');
+ expect(translate3d(10, '%', 0, '%', 0, 'px')).toBe(
+ 'translate3d(10%, 0%, 0px)',
+ );
+});
+
+test('makeTransform() should handle translations', () => {
+ expect(translateX(10)).toBe('translateX(10px)');
+ expect(translateX(10, '%')).toBe('translateX(10%)');
+ expect(translateY(10)).toBe('translateY(10px)');
+ expect(translateY(10, '%')).toBe('translateY(10%)');
+ expect(translateZ(10, 'px')).toBe('translateZ(10px)');
+ expect(translateZ('12px')).toBe('translateZ(12px)');
+});
+
+test('makeTransform() should handle perspective', () => {
+ expect(perspective(100)).toBe('perspective(100px)');
+});
diff --git a/packages/animation-utils/src/transformation-helpers/make-transform/is-unit-with-string.ts b/packages/animation-utils/src/transformation-helpers/make-transform/is-unit-with-string.ts
new file mode 100644
index 0000000000..12dc184892
--- /dev/null
+++ b/packages/animation-utils/src/transformation-helpers/make-transform/is-unit-with-string.ts
@@ -0,0 +1,26 @@
+import {lengthUnits} from '../../type';
+
+export const isUnitWithString = (input: unknown, units: readonly string[]) => {
+ if (typeof input !== 'string') {
+ return false;
+ }
+
+ if (!units.find((u) => input.endsWith(u))) {
+ throw new Error(
+ `input ${input} does not end with a valid unit. Valid units are: ${units.join(
+ ', ',
+ )}`,
+ );
+ }
+
+ const match = input.match(/([0-9.]+)([a-z%]+)/);
+ if (!match) {
+ throw new Error(
+ `input ${input} is not a valid transform. Must be a number or a string ending in one of the following units: ${lengthUnits.join(
+ ', ',
+ )}`,
+ );
+ }
+
+ return true;
+};
diff --git a/packages/animation-utils/src/transformation-helpers/make-transform/transform-functions.ts b/packages/animation-utils/src/transformation-helpers/make-transform/transform-functions.ts
index 5323bad229..e21e38e553 100644
--- a/packages/animation-utils/src/transformation-helpers/make-transform/transform-functions.ts
+++ b/packages/animation-utils/src/transformation-helpers/make-transform/transform-functions.ts
@@ -1,6 +1,16 @@
+/* eslint-disable no-redeclare */
/* eslint-disable max-params */
-import type {AngleUnit, LengthPercentageUnit, LengthUnit} from '../../type';
+import type {
+ AngleUnit,
+ AngleUnitString,
+ LengthPercentageUnit,
+ LengthPercentageUnitString,
+ LengthUnit,
+ LengthUnitString,
+} from '../../type';
+import {angleUnits, lengthPercentageUnits, lengthUnits} from '../../type';
+import {isUnitWithString} from './is-unit-with-string';
/* Matrix transformation */
@@ -93,47 +103,95 @@ function matrix3d(
/* Perspective */
-function perspective(length: number, unit: LengthUnit = 'px'): string {
+function perspective(length: LengthPercentageUnitString): string;
+function perspective(length: number, unit?: LengthUnit): string;
+function perspective(length: unknown, unit: LengthUnit = 'px'): string {
+ if (isUnitWithString(length, lengthUnits)) {
+ return `perspective(${length})`;
+ }
+
checkNumber({num: length, param: 'length', api: 'perspective'});
return `perspective(${length}${unit})`;
}
/* Rotation */
+function rotate(angle: AngleUnitString): string;
+function rotate(angle: number, unit?: AngleUnit): string;
+function rotate(angle: unknown, unit: AngleUnit = 'deg'): string {
+ if (isUnitWithString(angle, angleUnits)) {
+ return `rotate(${angle})`;
+ }
-function rotate(angle: number, unit: AngleUnit = 'deg'): string {
checkNumber({num: angle, param: 'angle', api: 'rotate'});
return `rotate(${angle}${unit})`;
}
+function rotate3d(x: number, y: number, z: number, angle: number): string;
+function rotate3d(
+ x: number,
+ y: number,
+ z: number,
+ angle: AngleUnitString,
+): string;
function rotate3d(
x: number,
y: number,
z: number,
angle: number,
+ unit?: AngleUnit,
+): string;
+function rotate3d(
+ x: number,
+ y: number,
+ z: number,
+ angle: unknown,
unit: AngleUnit = 'deg',
): string {
checkNumber({num: x, param: 'x', api: 'rotate3d'});
checkNumber({num: y, param: 'y', api: 'rotate3d'});
checkNumber({num: z, param: 'z', api: 'rotate3d'});
+
+ if (isUnitWithString(angle, angleUnits)) {
+ return `rotate3d(${x}, ${y}, ${z}, ${angle})`;
+ }
+
checkNumber({num: angle, param: 'angle', api: 'rotate3d'});
return `rotate3d(${x}, ${y}, ${z}, ${angle}${unit})`;
}
-function rotateX(angle: number, unit: AngleUnit = 'deg'): string {
+function rotateX(angle: AngleUnitString): string;
+function rotateX(angle: number, unit?: AngleUnit): string;
+function rotateX(angle: unknown, unit: AngleUnit = 'deg'): string {
+ if (isUnitWithString(angle, angleUnits)) {
+ return `rotateX(${angle})`;
+ }
+
checkNumber({num: angle, param: 'angle', api: 'rotateX'});
return `rotateX(${angle}${unit})`;
}
-function rotateY(angle: number, unit: AngleUnit = 'deg'): string {
+function rotateY(angle: AngleUnitString): string;
+function rotateY(angle: number, unit?: AngleUnit): string;
+function rotateY(angle: unknown, unit: AngleUnit = 'deg'): string {
+ if (isUnitWithString(angle, angleUnits)) {
+ return `rotateY(${angle})`;
+ }
+
checkNumber({num: angle, param: 'angle', api: 'rotateY'});
return `rotateY(${angle}${unit})`;
}
-function rotateZ(angle: number, unit: AngleUnit = 'deg'): string {
+function rotateZ(angle: AngleUnitString): string;
+function rotateZ(angle: number, unit?: AngleUnit): string;
+function rotateZ(angle: unknown, unit: AngleUnit = 'deg'): string {
+ if (isUnitWithString(angle, angleUnits)) {
+ return `rotateZ(${angle})`;
+ }
+
checkNumber({num: angle, param: 'angle', api: 'rotateZ'});
return `rotateZ(${angle}${unit})`;
@@ -174,64 +232,278 @@ function scaleZ(z: number): string {
/* Skew */
-function skew(x: number, y: number = x, unit: AngleUnit = 'deg'): string {
- checkNumber({num: x, param: 'x', api: 'skew'});
+// Case Z
+function skew(angle: number): string;
+// Case A
+function skew(angle: AngleUnitString): string;
+// Case B
+function skew(angle: AngleUnitString, angle2: AngleUnitString): string;
+// Case C
+function skew(angle: number, unit: AngleUnit): string;
+// Case D
+function skew(angleX: number, angleY: number): string;
+// Case E
+function skew(
+ angleX: number,
+ unitX: AngleUnit,
+ angleY: number,
+ unitY: AngleUnit,
+): string;
+function skew(...args: unknown[]): string {
+ const [arg1, arg2, arg3, arg4] = args;
+ if (arguments.length === 1) {
+ // Case A
+ if (isUnitWithString(arg1, angleUnits)) {
+ return `skew(${arg1}, ${arg1})`;
+ }
+
+ // Case Z
+ checkNumber({num: arg1, param: 'angle', api: 'skew'});
+
+ return `skew(${arg1}deg, ${arg1}deg)`;
+ }
+
+ if (arguments.length === 2) {
+ // Case B
+ if (
+ isUnitWithString(arg1, angleUnits) &&
+ isUnitWithString(arg2, angleUnits)
+ ) {
+ return `skew(${arg1}, ${arg2})`;
+ }
+
+ // Case C
+ if (typeof arg1 === 'number' && typeof arg2 !== 'number') {
+ checkNumber({num: arg1, param: 'angle', api: 'skew'});
+ return `skew(${arg1}${arg2}, ${arg1}${arg2})`;
+ }
+
+ // Case D
+ if (typeof arg1 === 'number' && typeof arg2 === 'number') {
+ checkNumber({num: arg1, param: 'angle', api: 'skew'});
+ checkNumber({num: arg2, param: 'angle', api: 'skew'});
+ return `skew(${arg1}deg, ${arg2}deg)`;
+ }
+ }
+
+ if (arguments.length === 4) {
+ // Case E
+ if (
+ typeof arg1 === 'number' &&
+ isUnitWithString(arg2, angleUnits) &&
+ typeof arg3 === 'number' &&
+ isUnitWithString(arg4, angleUnits)
+ ) {
+ checkNumber({num: arg1, param: 'angle', api: 'skew'});
+ checkNumber({num: arg3, param: 'angle', api: 'skew'});
+ return `skew(${arg1}${arg2}, ${arg3}${arg4})`;
+ }
+ }
- return `skew(${x}${unit}, ${y}${unit})`;
+ throw new TypeError(
+ [
+ 'skew() supports only the following signatures:',
+ 'skew(angle: AngleUnitString): string;',
+ 'skew(angle: AngleUnitString, angle2: AngleUnitString): string;',
+ 'skew(angle: number, unit: AngleUnit): string;',
+ 'skew(angleX: number, angleY: number): string;',
+ 'skew(angleX: number, unitX: AngleUnit, angleY: number, unitY: AngleUnit): string;',
+ ].join('\n'),
+ );
}
-function skewX(angle: number, unit: AngleUnit = 'deg'): string {
+function skewX(angle: AngleUnitString): string;
+function skewX(angle: number, unit?: AngleUnit): string;
+function skewX(angle: unknown, unit: AngleUnit = 'deg'): string {
+ if (isUnitWithString(angle, angleUnits)) {
+ return `skewX(${angle})`;
+ }
+
checkNumber({num: angle, param: 'angle', api: 'skewX'});
return `skewX(${angle}${unit})`;
}
-function skewY(angle: number, unit: AngleUnit = 'deg'): string {
+function skewY(angle: AngleUnitString): string;
+function skewY(angle: number, unit?: AngleUnit): string;
+function skewY(angle: unknown, unit: AngleUnit = 'deg'): string {
+ if (isUnitWithString(angle, angleUnits)) {
+ return `skewY(${angle})`;
+ }
+
checkNumber({num: angle, param: 'angle', api: 'skewY'});
return `skewY(${angle}${unit})`;
}
/* Translation */
-
+// Case A
+function translate(x: LengthPercentageUnitString): string;
+// Case B
+function translate(x: number): string;
+// Case C
+function translate(x: number, y: number): string;
+// Case C.1
+function translate(
+ x: LengthPercentageUnitString,
+ y: LengthPercentageUnitString,
+): string;
+// Case D
+function translate(translation: number, unit: LengthPercentageUnit): string;
+// Case E
function translate(
x: number,
- y = 0,
- unitX: LengthPercentageUnit = 'px',
- unitY: LengthPercentageUnit = unitX,
-): string {
- checkNumber({num: x, param: 'x', api: 'translate'});
+ unitX: LengthPercentageUnit,
+ y: number,
+ unitY: LengthPercentageUnit,
+): string;
+function translate(...args: unknown[]): string {
+ const [arg1, arg2, arg3, arg4] = args;
+ if (arguments.length === 1) {
+ // Case A
+ if (isUnitWithString(arg1, lengthPercentageUnits)) {
+ return `translate(${arg1})`;
+ }
+
+ // Case B
+ checkNumber({num: arg1, param: 'x', api: 'translate'});
+
+ return `translate(${arg1}px)`;
+ }
+
+ if (arguments.length === 2) {
+ // Case C
+ if (typeof arg1 === 'number' && typeof arg2 === 'number') {
+ checkNumber({num: arg1, param: 'x', api: 'translate'});
+ checkNumber({num: arg2, param: 'y', api: 'translate'});
+ return `translate(${arg1}px, ${arg2}px)`;
+ }
+
+ // Case C.1
+ if (
+ isUnitWithString(arg1, lengthPercentageUnits) &&
+ isUnitWithString(arg2, lengthPercentageUnits)
+ ) {
+ return `translate(${arg1}, ${arg2})`;
+ }
+
+ // Case D
+ if (typeof arg1 === 'number' && typeof arg2 !== 'number') {
+ checkNumber({num: arg1, param: 'x', api: 'translate'});
+ return `translate(${arg1}${arg2})`;
+ }
+ }
+
+ if (arguments.length === 4) {
+ // Case E
+ if (typeof arg1 === 'number' && typeof arg3 === 'number') {
+ checkNumber({num: arg1, param: 'x', api: 'translate'});
+ checkNumber({num: arg3, param: 'y', api: 'translate'});
+ return `translate(${arg1}${arg2}, ${arg3}${arg4})`;
+ }
+ }
- return `translate(${x}${unitX}, ${y}${unitY})`;
+ throw new TypeError(
+ [
+ `translate() supports only the following signatures:`,
+ `translate(x: LengthPercentageUnitString)`,
+ `translate(x: number)`,
+ `translate(x: number, y: number)`,
+ `translate(translation: number, unit: LengthPercentageUnit)`,
+ `translate(x: number, unitX: LengthPercentageUnit, y: number, unitY: LengthPercentageUnit): string;`,
+ ].join('\n'),
+ );
}
+function translate3d(
+ x: LengthPercentageUnitString | number,
+ y: LengthPercentageUnitString | number,
+ z: LengthPercentageUnitString | number,
+): string;
function translate3d(
x: number,
+ unitX: LengthPercentageUnit,
y: number,
+ unitY: LengthPercentageUnit,
z: number,
- unitX: LengthPercentageUnit = 'px',
- unitY: LengthPercentageUnit = unitX,
- unitZ: LengthUnit = 'px',
-): string {
- checkNumber({num: x, param: 'x', api: 'translate3d'});
- checkNumber({num: y, param: 'y', api: 'translate3d'});
- checkNumber({num: z, param: 'z', api: 'translate3d'});
+ unitZ: LengthUnit,
+): string;
+function translate3d(...args: unknown[]): string {
+ if (arguments.length === 3) {
+ const [x, y, z] = args;
+ const vars = [x, y, z].map((arg, i) => {
+ if (isUnitWithString(arg, lengthPercentageUnits)) {
+ return arg;
+ }
+
+ checkNumber({
+ num: arg,
+ param: i === 0 ? 'x' : i === 1 ? 'y' : 'z',
+ api: 'translate3d',
+ });
+ if (typeof arg === 'number') {
+ return `${arg}px`;
+ }
+
+ return arg;
+ });
+
+ return `translate3d(${vars.join(', ')})`;
+ }
+
+ if (arguments.length === 6) {
+ const [x, unitX, y, unitY, z, unitZ] = args;
+ if (
+ typeof x === 'number' &&
+ typeof y === 'number' &&
+ typeof z === 'number'
+ ) {
+ checkNumber({num: x, param: 'x', api: 'translate3d'});
+ checkNumber({num: y, param: 'y', api: 'translate3d'});
+ checkNumber({num: z, param: 'z', api: 'translate3d'});
+ return `translate3d(${x}${unitX}, ${y}${unitY}, ${z}${unitZ})`;
+ }
+ }
- return `translate3d(${x}${unitX}, ${y}${unitY}, ${z}${unitZ})`;
+ throw new TypeError(
+ [
+ `translate3d() supports only the following signatures:`,
+ `translate3d(x: LengthPercentageUnitString, y: LengthPercentageUnitString, z: LengthPercentageUnitString)`,
+ `translate3d(x: number, unitX: LengthPercentageUnit, y: number, unitY: LengthPercentageUnit, z: number, unitZ: LengthUnit)`,
+ ].join('\n'),
+ );
}
-function translateX(x: number, unit: LengthPercentageUnit = 'px'): string {
+function translateX(x: LengthPercentageUnitString): string;
+function translateX(x: number, unit?: LengthPercentageUnit): string;
+function translateX(x: unknown, unit: LengthPercentageUnit = 'px'): string {
+ if (isUnitWithString(x, lengthPercentageUnits)) {
+ return `translateX(${x})`;
+ }
+
checkNumber({num: x, param: 'x', api: 'translateX'});
return `translateX(${x}${unit})`;
}
-function translateY(y: number, unit: LengthPercentageUnit = 'px'): string {
+function translateY(y: LengthPercentageUnitString): string;
+function translateY(y: number, unit?: LengthPercentageUnit): string;
+function translateY(y: unknown, unit: LengthPercentageUnit = 'px'): string {
+ if (isUnitWithString(y, lengthPercentageUnits)) {
+ return `translateY(${y})`;
+ }
+
checkNumber({num: y, param: 'y', api: 'translateY'});
return `translateY(${y}${unit})`;
}
-function translateZ(z: number, unit: LengthUnit = 'px'): string {
+function translateZ(z: LengthUnitString): string;
+function translateZ(z: number, unit?: LengthUnit): string;
+function translateZ(z: unknown, unit: LengthUnit = 'px'): string {
+ if (isUnitWithString(z, lengthUnits)) {
+ return `translateZ(${z})`;
+ }
+
checkNumber({num: z, param: 'z', api: 'translateZ'});
return `translateZ(${z}${unit})`;
diff --git a/packages/animation-utils/src/type.ts b/packages/animation-utils/src/type.ts
index 1b71772247..c014a089b1 100644
--- a/packages/animation-utils/src/type.ts
+++ b/packages/animation-utils/src/type.ts
@@ -23,40 +23,46 @@ import type {
translateZ,
} from './transformation-helpers/make-transform';
-type LengthUnit =
- | 'cap'
- | 'ch'
- | 'em'
- | 'ex'
- | 'ic'
- | 'lh'
- | 'rem'
- | 'rlh'
- | 'vh'
- | 'vw'
- | 'vmax'
- | 'vmin'
- | 'vb'
- | 'vi'
- | 'cqw'
- | 'cqh'
- | 'cqi'
- | 'cqb'
- | 'cqmin'
- | 'cqmax'
- | 'px'
- | 'cm'
- | 'mm'
- | 'Q'
- | 'in'
- | 'pc'
- | 'pt';
+export const lengthUnits = [
+ 'px',
+ 'em',
+ 'rem',
+ 'pt',
+ 'cap',
+ 'ch',
+ 'ex',
+ 'ic',
+ 'lh',
+ 'rlh',
+ 'vmax',
+ 'vmin',
+ 'vb',
+ 'vi',
+ 'cqw',
+ 'vh',
+ 'vw',
+ 'cqh',
+ 'cqi',
+ 'cqb',
+ 'cqmin',
+ 'cqmax',
+ 'cm',
+ 'mm',
+ 'Q',
+ 'in',
+ 'pc',
+] as const;
+export const angleUnits = ['rad', 'deg', 'grad', 'turn'] as const;
-export type LengthUnitString = `${number}${LengthUnit}`;
+export const lengthPercentageUnits = ['%', ...lengthUnits] as const;
-type LengthPercentageUnit = LengthUnit | '%';
+type LengthUnit = (typeof lengthUnits)[number];
+type AngleUnit = (typeof angleUnits)[number];
+type LengthPercentageUnit = (typeof lengthPercentageUnits)[number];
-type AngleUnit = 'deg' | 'grad' | 'rad' | 'turn';
+export type LengthUnitString = `${number}${LengthUnit}`;
+export type LengthPercentageUnitString = `${number}${LengthPercentageUnit}`;
+export type AngleUnitString = `${number}${AngleUnit}`;
type TransformFunctionReturnType = ReturnType<
| typeof matrix
diff --git a/packages/docs/components/ColorDemo/Name.tsx b/packages/docs/components/ColorDemo/Name.tsx
index d3e60f0b0d..1299f02e7a 100644
--- a/packages/docs/components/ColorDemo/Name.tsx
+++ b/packages/docs/components/ColorDemo/Name.tsx
@@ -1,12 +1,12 @@
+import { interpolateStyles, translateY } from "@remotion/animation-utils";
import React from "react";
import {
AbsoluteFill,
- interpolate,
spring,
useCurrentFrame,
useVideoConfig,
} from "remotion";
-import { defaultStyles } from './styles';
+import { defaultStyles } from "./styles";
export const Name: React.FC<{
name: string;
@@ -29,13 +29,14 @@ export const Name: React.FC<{
}}
>
<div
- style={{
- transform: `translateY(${interpolate(
- progress,
- [0, 1],
- [1000, 0]
- )}px)`,
- }}
+ style={interpolateStyles(
+ progress,
+ [0, 1],
+ [
+ { transform: translateY(1000), opacity: 1 },
+ { transform: translateY(0), opacity: 0 },
+ ],
+ )}
>
<span
style={{
diff --git a/packages/docs/docs/animation-utils/make-transform.md b/packages/docs/docs/animation-utils/make-transform.md
index 9ad5612ae1..a6be7af1b4 100644
--- a/packages/docs/docs/animation-utils/make-transform.md
+++ b/packages/docs/docs/animation-utils/make-transform.md
@@ -11,51 +11,287 @@ Applies a sequence of transformation functions to generate a combined CSS `trans
## API
-### `makeTransform(transforms: TransformFunctionReturnType[]): string`
-
-Generates a combined CSS transform property string by applying an array of transformation functions.
-
-#### Parameters
-
-- **transforms**: _Required_.
- An array of transformation functions obtained from the provided utility functions.
+Takes an array of strings (generated from the below transformation functions) and combines them into a single string.
## Usage
-```ts twoslash
+```tsx twoslash
import { makeTransform, rotate, translate } from "@remotion/animation-utils";
-const transforms = [rotate(45), translate(50, 50)];
+const transform = makeTransform([rotate(45), translate(50, 50)]);
+// => "rotate(45deg) translate(50px, 50px)"
-const combinedTransform = makeTransform(transforms);
-// Result: "rotate(45deg) translate(50px, 50px)"
+const markup = <div style={{ transform }} />;
```
## Transformation Functions
-| Function | Description |
-| ---------------------------------------------------- | ------------------------------------------------------------ |
-| **matrix(a, b, c, d, tx, ty)** | Applies a 2D transformation with a matrix of six values. |
-| **matrix3d(a1, b1, ..., d4)** | Applies a 3D transformation with a matrix of sixteen values. |
-| **perspective(length, unit = 'px')** | Sets the perspective value for 3D transformations. |
-| **rotate(angle, unit = 'deg')** | Rotates an element around a fixed point on the 2D plane. |
-| **rotate3d(x, y, z, angle, unit = 'deg')** | Rotates an element around a vector in 3D space. |
-| **rotateX(angle, unit = 'deg')** | Rotates an element around the horizontal axis in 3D space. |
-| **rotateY(angle, unit = 'deg')** | Rotates an element around the vertical axis in 3D space. |
-| **rotateZ(angle, unit = 'deg')** | Rotates an element around the z-axis in 3D space. |
-| **scale(x, y = x)** | Scales an element in the 2D plane. |
-| **scale3d(x, y, z)** | Scales an element in 3D space. |
-| **scaleX(x)** | Scales an element along the horizontal axis in 3D space. |
-| **scaleY(y)** | Scales an element along the vertical axis in 3D space. |
-| **scaleZ(z)** | Scales an element along the z-axis in 3D space. |
-| **skew(x, y = x, unit = 'deg')** | Skews an element in the 2D plane. |
-| **skewX(angle, unit = 'deg')** | Skews an element along the horizontal axis in 2D space. |
-| **skewY(angle, unit = 'deg')** | Skews an element along the vertical axis in 2D space. |
-| **translate(x, y = 0, unitX = 'px', unitY = unitX)** | Translates an element in the 2D plane. |
-| **translate3d(x, y, z, unitX = 'px', ...)** | Translates an element in 3D space. |
-| **translateX(x, unit = 'px')** | Translates an element along the horizontal axis in 2D space. |
-| **translateY(y, unit = 'px')** | Translates an element along the vertical axis in 2D space. |
-| **translateZ(z, unit = 'px')** | Translates an element along the z-axis in 3D space. |
+### matrix()
+
+```tsx twoslash
+import { matrix } from "@remotion/animation-utils";
+
+const transform = matrix(1, 0, 0, 1, 50, 50);
+// => "matrix(1, 0, 0, 1, 50, 50)"
+```
+
+### matrix3d()
+
+```tsx twoslash
+import { matrix3d } from "@remotion/animation-utils";
+
+const transform = matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 50, 50, 0, 1);
+// => "matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 50, 50, 0, 1)"
+```
+
+### perspective()
+
+```tsx twoslash
+import { perspective } from "@remotion/animation-utils";
+
+const transform = perspective(100);
+// => "perspective(100px)"
+```
+
+### rotate()
+
+```tsx twoslash
+import { rotate } from "@remotion/animation-utils";
+
+const transform = rotate(45);
+// => "rotate(45deg)"
+```
+
+### rotate3d()
+
+```tsx twoslash
+import { rotate3d } from "@remotion/animation-utils";
+
+const transform = rotate3d(1, 0, 0, 45);
+// => "rotate3d(1, 0, 0, 45deg)"
+
+const transform2 = rotate3d(1, 0, 0, "45deg");
+// => "rotate3d(1, 0, 0, 45deg)"
+
+const transform3 = rotate3d(1, 0, 0, 45, "deg");
+// => "rotate3d(1, 0, 0, 45deg)"
+```
+
+### rotateX()
+
+```tsx twoslash
+import { rotateX } from "@remotion/animation-utils";
+
+const transform = rotateX(45);
+// => "rotateX(45deg)"
+
+const transform2 = rotateX("45deg");
+// => "rotateX(45deg)"
+
+const transform3 = rotateX(1, "rad");
+// => "rotateX(45rad)"
+```
+
+### rotateY()
+
+```tsx twoslash
+import { rotateY } from "@remotion/animation-utils";
+
+const transform = rotateY(45);
+// => "rotateY(45deg)"
+
+const transform2 = rotateY("45deg");
+// => "rotateY(45deg)"
+
+const transform3 = rotateY(1, "rad");
+// => "rotateY(1rad)"
+```
+
+### rotateZ()
+
+```tsx twoslash
+import { rotateZ } from "@remotion/animation-utils";
+
+const transform = rotateZ(45);
+// => "rotateZ(45deg)"
+
+const transform2 = rotateZ("45deg");
+// => "rotateZ(45deg)"
+
+const transform3 = rotateZ(1, "rad");
+// => "rotateZ(1rad)"
+```
+
+### scale()
+
+```tsx twoslash
+import { scale } from "@remotion/animation-utils";
+
+const transform = scale(2);
+// => "scale(2, 2)"
+
+const transform2 = scale(2, 3);
+// => "scale(2, 3)"
+```
+
+### scale3d()
+
+```tsx twoslash
+import { scale3d } from "@remotion/animation-utils";
+
+const transform = scale3d(2, 3, 4);
+// => "scale3d(2, 3, 4)"
+```
+
+### scaleX()
+
+```tsx twoslash
+import { scaleX } from "@remotion/animation-utils";
+
+const transform = scaleX(2);
+// => "scaleX(2)"
+```
+
+### scaleY()
+
+```tsx twoslash
+import { scaleY } from "@remotion/animation-utils";
+
+const transform = scaleY(2);
+// => "scaleY(2)"
+```
+
+### scaleZ()
+
+```tsx twoslash
+import { scaleZ } from "@remotion/animation-utils";
+
+const transform = scaleZ(2);
+// => "scaleZ(2)"
+```
+
+### skew()
+
+```tsx twoslash
+import { skew } from "@remotion/animation-utils";
+
+const transform = skew(45);
+// => "skew(45deg)"
+```
+
+### skewX()
+
+```tsx twoslash
+import { skewX } from "@remotion/animation-utils";
+
+const transform = skewX(45);
+// => "skewX(45deg)"
+
+const transform2 = skewX("45deg");
+// => "skewX(45deg)"
+
+const transform3 = skewX(1, "rad");
+// => "skewX(1rad)"
+```
+
+### skewY()
+
+```tsx twoslash
+import { skewY } from "@remotion/animation-utils";
+
+const transform = skewY(45);
+// => "skewY(45deg)"
+
+const transform2 = skewY("45deg");
+// => "skewY(45deg)"
+
+const transform3 = skewY(1, "rad");
+// => "skewY(1rad)"
+```
+
+### translate()
+
+```tsx twoslash
+import { translate } from "@remotion/animation-utils";
+
+const transform = translate(10);
+// => "translate(10px)"
+
+const transform2 = translate("12rem");
+// => "translate(12rem)"
+
+const transform3 = translate(10, 20);
+// => "translate(10px, 20px)"
+
+const transform4 = translate(10, "%");
+// => "translate(10%)"
+
+const transform5 = translate(0, "%", 10, "%");
+// => "translate(0%, 10%)"
+
+const transform6 = translate("10px", "30%");
+// => "translate(10px, 20%)"
+```
+
+### translate3d()
+
+```tsx twoslash
+import { translate3d } from "@remotion/animation-utils";
+
+const transform = translate3d(10, 20, 30);
+// => "translate3d(10px, 20px, 30px)"
+
+const transform2 = translate3d("10px", "20%", "30rem");
+// => "translate3d(10px, 20%, 30rem)"
+
+const transform3 = translate3d(10, "%", 20, "px", 30, "px");
+// => "translate3d(10%, 20px, 30px)"
+```
+
+### translateX()
+
+```tsx twoslash
+import { translateX } from "@remotion/animation-utils";
+
+const transform = translateX(10);
+// => "translateX(10px)"
+
+const transform2 = translateX("12rem");
+// => "translateX(12rem)"
+
+const transform3 = translateX(10, "%");
+// => "translateX(10%)"
+```
+
+### translateY()
+
+```tsx twoslash
+import { translateY } from "@remotion/animation-utils";
+
+const transform = translateY(10);
+// => "translateY(10px)"
+
+const transform2 = translateY("12rem");
+// => "translateY(12rem)"
+
+const transform3 = translateY(10, "px");
+// => "translateY(10px)"
+```
+
+### translateZ()
+
+```tsx twoslash
+import { translateZ } from "@remotion/animation-utils";
+
+const transform = translateZ(10);
+// => "translateZ(10px)"
+
+const transform2 = translateZ("12rem");
+// => "translateZ(12rem)"
+
+const transform3 = translateZ(10, "px");
+// => "translateZ(10px)"
+```
## See also
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index c5ced3bae9..d7fdefc763 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -178,18 +178,7 @@ module.exports = {
"visualize-audio",
],
},
- {
- type: "category",
- label: "@remotion/animation-utils",
- link: {
- type: "doc",
- id: "animation-utils/index",
- },
- items: [
- "animation-utils/make-transform",
- "animation-utils/interpolate-styles",
- ],
- },
+
{
type: "category",
label: "@remotion/motion-blur",
@@ -475,6 +464,18 @@ module.exports = {
},
items: ["layout-utils/measure-text", "layout-utils/fill-text-box"],
},
+ {
+ type: "category",
+ label: "@remotion/animation-utils",
+ link: {
+ type: "doc",
+ id: "animation-utils/index",
+ },
+ items: [
+ "animation-utils/make-transform",
+ "animation-utils/interpolate-styles",
+ ],
+ },
],
mainSidebar: [
{