Skip to content

🎥 Make videos programmatically with React

allow interpolation

Jonny Burger186f30a9a2e4
diff --git a/packages/animation-utils/package.json b/packages/animation-utils/package.json
index fd8f028078..7a0d39c68e 100644
--- a/packages/animation-utils/package.json
+++ b/packages/animation-utils/package.json
@@ -4,7 +4,7 @@
"name": "Chetan Karwa",
"email": "[email protected]"
},
- "version": "4.0.88",
+ "version": "4.0.89",
"description": "A set of animation utilities for Remotion",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
diff --git a/packages/animation-utils/src/test/interpolate-styles.test.ts b/packages/animation-utils/src/test/interpolate-styles.test.ts
index c8138851e5..f7de158bd8 100644
--- a/packages/animation-utils/src/test/interpolate-styles.test.ts
+++ b/packages/animation-utils/src/test/interpolate-styles.test.ts
@@ -124,6 +124,29 @@ test('Should be able to interpolate transform strings - edge', () => {
},
],
),
+ ).toEqual({transform: `scale(2.5) translateX(400px) rotate(80deg)`});
+});
+
+test('Should be able to use extrapolateRight: "clamp"', () => {
+ expect(
+ interpolateStyles(
+ 2.5,
+ [0, 1, 2],
+ [
+ {
+ transform: `scale(0.5)`,
+ },
+ {
+ transform: `scale(1) translateX(100px) rotate(20deg)`,
+ },
+ {
+ transform: `scale(2) translateX(300px) rotate(60deg)`,
+ },
+ ],
+ {
+ extrapolateRight: 'clamp',
+ },
+ ),
).toEqual({transform: `scale(2) translateX(300px) rotate(60deg)`});
});
diff --git a/packages/animation-utils/src/transformation-helpers/interpolate-styles/index.tsx b/packages/animation-utils/src/transformation-helpers/interpolate-styles/index.tsx
index 6f3bf41409..883780a4e6 100644
--- a/packages/animation-utils/src/transformation-helpers/interpolate-styles/index.tsx
+++ b/packages/animation-utils/src/transformation-helpers/interpolate-styles/index.tsx
@@ -1,3 +1,4 @@
+import type {InterpolateOptions} from 'remotion';
import {interpolate, interpolateColors} from 'remotion';
import type {
CSSPropertiesKey,
@@ -7,6 +8,8 @@ import type {
} from '../../type';
import {breakDownValueIntoUnitNumberAndFunctions} from './utils';
+type ExtrapolateType = 'extend' | 'identity' | 'clamp';
+
const interpolatedPropertyPart = ({
inputValue,
inputRange,
@@ -14,6 +17,7 @@ const interpolatedPropertyPart = ({
finalStylePropertyPart,
initialStyleProperty,
finalStyleProperty,
+ options,
}: {
inputValue: number;
inputRange: number[];
@@ -21,6 +25,7 @@ const interpolatedPropertyPart = ({
finalStylePropertyPart: UnitNumberAndFunction;
initialStyleProperty: CSSPropertiesValue;
finalStyleProperty: CSSPropertiesValue;
+ options: Required<InterpolateOptions>;
}): string | number => {
if (finalStylePropertyPart === undefined) {
throw new TypeError(
@@ -66,6 +71,7 @@ const interpolatedPropertyPart = ({
finalStylePropertyPart: endValuePartFunctionArg,
initialStyleProperty,
finalStyleProperty,
+ options,
});
return `${acc}, ${interpolatedArg}`;
},
@@ -98,10 +104,12 @@ const interpolatedPropertyPart = ({
const startNumber = initialStylePropertyPart.number;
const endNumber = finalStylePropertyPart.number || 0;
- const interpolatedNumber = interpolate(inputValue, inputRange, [
- startNumber,
- endNumber,
- ]);
+ const interpolatedNumber = interpolate(
+ inputValue,
+ inputRange,
+ [startNumber, endNumber],
+ options,
+ );
const interpolatedUnit =
initialStylePropertyPart.unit || finalStylePropertyPart.unit || '';
@@ -112,12 +120,19 @@ const interpolatedPropertyPart = ({
return `${interpolatedNumber}${interpolatedUnit}`;
};
-const interpolateProperty = (
- inputValue: number,
- inputRange: number[],
- initialStyleProperty: CSSPropertiesValue,
- finalStyleProperty: CSSPropertiesValue,
-) => {
+const interpolateProperty = ({
+ inputValue,
+ inputRange,
+ initialStyleProperty,
+ finalStyleProperty,
+ options,
+}: {
+ inputValue: number;
+ inputRange: number[];
+ initialStyleProperty: CSSPropertiesValue;
+ finalStyleProperty: CSSPropertiesValue;
+ options: Required<InterpolateOptions>;
+}) => {
if (
typeof initialStyleProperty !== typeof finalStyleProperty &&
initialStyleProperty !== 0 &&
@@ -148,6 +163,7 @@ const interpolateProperty = (
finalStylePropertyPart: finalStylePropertyParts[index],
initialStyleProperty,
finalStyleProperty,
+ options,
})}`;
},
'',
@@ -155,12 +171,20 @@ const interpolateProperty = (
return interpolatedValue.slice(1);
};
-const interpolateStylesFunction = (
- inputValue: number,
- [startingValue, endingValue]: number[],
- initialStyle: Style,
- finalStyle: Style,
-): Style => {
+const interpolateStylesFunction = ({
+ inputValue,
+ inputRange,
+ initialStyle,
+ finalStyle,
+ options,
+}: {
+ inputValue: number;
+ inputRange: number[];
+ initialStyle: Style;
+ finalStyle: Style;
+ options: Required<InterpolateOptions>;
+}): Style => {
+ const [startingValue, endingValue] = inputRange;
return Object.keys(initialStyle).reduce((acc, key) => {
if (!finalStyle[key as CSSPropertiesKey]) {
return {
@@ -169,12 +193,13 @@ const interpolateStylesFunction = (
};
}
- const finalStyleValue = interpolateProperty(
+ const finalStyleValue = interpolateProperty({
inputValue,
- [startingValue, endingValue],
- initialStyle[key as CSSPropertiesKey],
- finalStyle[key as CSSPropertiesKey],
- );
+ inputRange: [startingValue, endingValue],
+ initialStyleProperty: initialStyle[key as CSSPropertiesKey],
+ finalStyleProperty: finalStyle[key as CSSPropertiesKey],
+ options,
+ });
// Avoid number to be a string
if (!isNaN(Number(finalStyleValue))) {
@@ -239,6 +264,7 @@ export const interpolateStyles = (
input: number,
inputRange: number[],
outputStylesRange: Style[],
+ options?: InterpolateOptions,
) => {
if (typeof input === 'undefined') {
throw new Error('input can not be undefined');
@@ -265,24 +291,32 @@ export const interpolateStyles = (
checkInputRange(inputRange);
checkStylesRange(outputStylesRange);
- if (input < inputRange[0]) {
- return outputStylesRange[0];
+ let startIndex = inputRange.findIndex((step) => input < step) - 1;
+ if (startIndex === -2) {
+ startIndex = inputRange.length - 2;
}
- if (input >= inputRange[inputRange.length - 1]) {
- return outputStylesRange[outputStylesRange.length - 1];
- }
-
- const startIndex = inputRange.findIndex((step) => input < step) - 1;
const endIndex = startIndex + 1;
const startingValue = inputRange[startIndex];
const endingValue = inputRange[endIndex];
const initialStyle = outputStylesRange[startIndex];
const finalStyle = outputStylesRange[endIndex];
- return interpolateStylesFunction(
- input,
- [startingValue, endingValue],
+
+ const easing = options?.easing ?? ((num: number): number => num);
+
+ const extrapolateLeft: ExtrapolateType = options?.extrapolateLeft ?? 'extend';
+ const extrapolateRight: ExtrapolateType =
+ options?.extrapolateRight ?? 'extend';
+
+ return interpolateStylesFunction({
+ inputValue: input,
+ inputRange: [startingValue, endingValue],
initialStyle,
finalStyle,
- );
+ options: {
+ easing,
+ extrapolateLeft,
+ extrapolateRight,
+ },
+ });
};
diff --git a/packages/docs/components/TableOfContents/animation-utils.tsx b/packages/docs/components/TableOfContents/animation-utils.tsx
index 43d34c7914..8d6082fbd1 100644
--- a/packages/docs/components/TableOfContents/animation-utils.tsx
+++ b/packages/docs/components/TableOfContents/animation-utils.tsx
@@ -9,12 +9,14 @@ export const TableOfContents: React.FC = () => {
<TOCItem link="/docs/animation-utils/make-transform">
<strong>makeTransform()</strong>
<div>
- Typesafe function to create a string for the CSS <code>transform</code> property
+ Create a value for the CSS <code>transform</code> property
</div>
</TOCItem>
<TOCItem link="/docs/animation-utils/interpolate-styles">
<strong>interpolateStyles()</strong>
- <div>Map a range of values to CSS <code>style</code> values</div>
+ <div>
+ Map a range of values to CSS <code>style</code> values
+ </div>
</TOCItem>
</Grid>
</div>
diff --git a/packages/docs/docs/animation-utils/index.md b/packages/docs/docs/animation-utils/index.md
index d503c88a7c..94b1e5b14a 100644
--- a/packages/docs/docs/animation-utils/index.md
+++ b/packages/docs/docs/animation-utils/index.md
@@ -3,9 +3,9 @@ image: /generated/articles-docs-animation-utils-index.png
title: "@remotion/animation-utils"
---
-_Available from v4.0.89_
+_Available from v4.0.90_
-Utility functions that create a remotion animation.
+Functions that support you in animating CSS styles.
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
diff --git a/packages/docs/docs/animation-utils/interpolate-styles.md b/packages/docs/docs/animation-utils/interpolate-styles.md
index 4b92a7c800..5b6563974e 100644
--- a/packages/docs/docs/animation-utils/interpolate-styles.md
+++ b/packages/docs/docs/animation-utils/interpolate-styles.md
@@ -9,21 +9,7 @@ _Part of the [`@remotion/animation-utils`](/docs/animation-utils) package._
This function provides a convenient way to interpolate styles based on a specified range of values, allowing for smooth animations between different styles.
-## API
-
-### `interpolateStyles(options: InterpolateStylesParams): Style`
-
-### Params
-
-1. The input value.
-2. The range of values that you expect the input to assume.
-3. The range of output styles that you want the input to map to.
-
-### Return value
-
-- A style object representing the interpolated styles based on the current frame.
-
-### Example
+## Example
```tsx twoslash
// ---cut---
@@ -44,11 +30,23 @@ const MyComponent: React.FC = () => {
],
);
- return <div style={animatedStyles}>Animating Styles!</div>;
+ return <div style={animatedStyles} />;
};
```
-### Usage Notes
+## API
+
+A function that takes three arguments:
+
+1. The input value.
+2. The range of values that you expect the input to assume.
+3. The range of output styles that you want the input to map to.
+
+## Return value
+
+- A style object representing the interpolated styles based on the current frame.
+
+## Usage Notes
- Ensure that the `inputRange` and `outputStylesRange` arrays contain at least two values to facilitate interpolation between styles.