Skip to content

🎥 Make videos programmatically with React

`@remotion/studio-server`: Use interpolateTranslate for translate keyframes (#8025)

Jonny Burger1a06d0fce0db
diff --git a/packages/core/src/interpolate-keyframed-status.ts b/packages/core/src/interpolate-keyframed-status.ts
index 49b6be3d16..b40caebc25 100644
--- a/packages/core/src/interpolate-keyframed-status.ts
+++ b/packages/core/src/interpolate-keyframed-status.ts
@@ -1,6 +1,7 @@
import {bezier} from './bezier.js';
import {Easing} from './easing.js';
import {interpolateColors} from './interpolate-colors.js';
+import {interpolateTranslate} from './interpolate-translate.js';
import type {EasingFunction} from './interpolate.js';
import {interpolate} from './interpolate.js';
import type {ScaleValue} from './scale-value.js';
@@ -55,6 +56,27 @@ export const interpolateKeyframedStatus = ({
}
}
+ if (interpolationFunction === 'interpolateTranslate') {
+ if (!outputs.every((v) => typeof v === 'string')) {
+ return null;
+ }
+
+ if (keyframes.length === 1) {
+ return outputs[0] as string;
+ }
+
+ try {
+ return interpolateTranslate(frame, inputRange, outputs as string[], {
+ easing: easing.map(easingToFn),
+ extrapolateLeft: clamping.left,
+ extrapolateRight: clamping.right,
+ posterize: status.posterize,
+ });
+ } catch {
+ return null;
+ }
+ }
+
if (interpolationFunction !== 'interpolate') {
return null;
}
diff --git a/packages/core/src/test/interpolate-keyframed-status.test.ts b/packages/core/src/test/interpolate-keyframed-status.test.ts
index 3b7b0bc6e3..d0ac266649 100644
--- a/packages/core/src/test/interpolate-keyframed-status.test.ts
+++ b/packages/core/src/test/interpolate-keyframed-status.test.ts
@@ -113,6 +113,25 @@ test('posterizes the frame before interpolating color keyframes', () => {
expect(result).toBe('rgba(64, 64, 64, 1)');
});
+test('interpolates translate keyframes', () => {
+ const result = interpolateKeyframedStatus({
+ frame: 30,
+ status: {
+ status: 'keyframed',
+ codeValue: undefined,
+ interpolationFunction: 'interpolateTranslate',
+ keyframes: [
+ {frame: 0, value: '0px 0px'},
+ {frame: 60, value: '120px 60px'},
+ ],
+ easing: ['linear'],
+ clamping: {left: 'extend', right: 'extend'},
+ posterize: undefined,
+ },
+ });
+ expect(result).toBe('60px 30px');
+});
+
test('uses bezier easing', () => {
const result = interpolateKeyframedStatus({
frame: 30,
diff --git a/packages/core/src/use-schema.ts b/packages/core/src/use-schema.ts
index a7ca5590e0..037d8f2dd7 100644
--- a/packages/core/src/use-schema.ts
+++ b/packages/core/src/use-schema.ts
@@ -32,7 +32,8 @@ export type CanUpdateSequencePropStatusClamping = {
export type CanUpdateSequencePropStatusInterpolationFunction =
| 'interpolate'
- | 'interpolateColors';
+ | 'interpolateColors'
+ | 'interpolateTranslate';
export type CanUpdateSequencePropStatusComputed = {
status: 'computed';
diff --git a/packages/studio-server/src/codemods/update-keyframes/update-keyframes.ts b/packages/studio-server/src/codemods/update-keyframes/update-keyframes.ts
index 3aa4b82622..2c29e868e3 100644
--- a/packages/studio-server/src/codemods/update-keyframes/update-keyframes.ts
+++ b/packages/studio-server/src/codemods/update-keyframes/update-keyframes.ts
@@ -10,9 +10,15 @@ import type {
ObjectProperty,
StringLiteral,
} from '@babel/types';
+import {
+ getKeyframeInterpolationFunction,
+ getKeyframeInterpolationFunctionForSchemaField,
+ isKeyframeInterpolationFunction,
+ type KeyframeInterpolationFunction,
+} from '@remotion/studio-shared';
import type {ExpressionKind, SpreadElementKind} from 'ast-types/lib/gen/kinds';
import * as recast from 'recast';
-import type {SequenceNodePath} from 'remotion';
+import type {SequenceNodePath, SequenceSchema} from 'remotion';
import {getAstNodePath} from '../../helpers/get-ast-node-path';
import {
extractStaticValue,
@@ -118,8 +124,7 @@ const getInterpolationExpression = (
if (
node.type !== 'CallExpression' ||
node.callee.type !== 'Identifier' ||
- (node.callee.name !== 'interpolate' &&
- node.callee.name !== 'interpolateColors')
+ !isKeyframeInterpolationFunction(node.callee.name)
) {
return null;
}
@@ -186,16 +191,23 @@ const getInterpolationExpression = (
};
const getInterpolationCalleeForValues = ({
+ schema,
+ key,
staticValue,
newValue,
}: {
+ schema: SequenceSchema | null;
+ key: string;
staticValue: unknown;
newValue: unknown;
}): ExpressionKind => {
return b.identifier(
- typeof staticValue === 'string' && typeof newValue === 'string'
- ? 'interpolateColors'
- : 'interpolate',
+ getKeyframeInterpolationFunction({
+ schema,
+ key,
+ staticValue,
+ newValue,
+ }),
);
};
@@ -228,7 +240,7 @@ const createInterpolateExpression = ({
};
export type IntroducedKeyframeIdentifiers = {
- calleeName: 'interpolate' | 'interpolateColors' | null;
+ calleeName: KeyframeInterpolationFunction | null;
needsFrameHook: boolean;
};
@@ -239,17 +251,30 @@ const noIntroducedIdentifiers: IntroducedKeyframeIdentifiers = {
const addKeyframe = ({
expression,
+ key,
frame,
value,
+ schema,
}: {
expression: Expression;
+ key: string;
frame: number;
value: unknown;
+ schema: SequenceSchema | null;
}): {expression: ExpressionKind; introduced: IntroducedKeyframeIdentifiers} => {
const existing = getInterpolationExpression(expression);
const newOutput = parseValueExpression(value);
if (existing) {
+ const existingCalleeName =
+ existing.callee.type === 'Identifier'
+ ? (existing.callee.name as KeyframeInterpolationFunction)
+ : 'interpolate';
+ const schemaCalleeName = getKeyframeInterpolationFunctionForSchemaField({
+ schema,
+ key,
+ });
+ const nextCalleeName = schemaCalleeName ?? existingCalleeName;
const existingIndex = existing.keyframes.findIndex(
(keyframe) => keyframe.frame === frame,
);
@@ -264,12 +289,18 @@ const addKeyframe = ({
return {
expression: createInterpolateExpression({
- callee: existing.callee,
+ callee: b.identifier(nextCalleeName),
input: existing.input,
extraArgs: existing.extraArgs,
keyframes: nextKeyframes,
}),
- introduced: noIntroducedIdentifiers,
+ introduced: {
+ calleeName:
+ schemaCalleeName && schemaCalleeName !== existingCalleeName
+ ? schemaCalleeName
+ : null,
+ needsFrameHook: false,
+ },
};
}
@@ -281,6 +312,8 @@ const addKeyframe = ({
const keyframes: InterpolateKeyframe[] = [{frame, output: newOutput, value}];
const callee = getInterpolationCalleeForValues({
+ schema,
+ key,
staticValue,
newValue: value,
});
@@ -295,7 +328,7 @@ const addKeyframe = ({
introduced: {
calleeName:
callee.type === 'Identifier'
- ? (callee.name as 'interpolate' | 'interpolateColors')
+ ? (callee.name as KeyframeInterpolationFunction)
: null,
needsFrameHook: true,
},
@@ -339,16 +372,22 @@ const removeKeyframe = ({
const applyKeyframeOperation = ({
expression,
+ key,
operation,
+ schema,
}: {
expression: Expression;
+ key: string;
operation: KeyframeOperation;
+ schema: SequenceSchema | null;
}): {expression: ExpressionKind; introduced: IntroducedKeyframeIdentifiers} => {
if (operation.type === 'add') {
return addKeyframe({
expression,
+ key,
frame: operation.frame,
value: operation.value,
+ schema,
});
}
@@ -501,10 +540,12 @@ export const updateSequenceKeyframesAst = ({
input,
nodePath,
updates,
+ schema,
}: {
input: string;
nodePath: SequenceNodePath;
updates: SequenceKeyframeUpdate[];
+ schema?: SequenceSchema;
}): {
serialized: string;
oldValueStrings: string[];
@@ -538,7 +579,9 @@ export const updateSequenceKeyframesAst = ({
oldValueStrings.push(recast.print(prop.expression).code);
const {expression: nextExpression, introduced} = applyKeyframeOperation({
expression: prop.expression,
+ key: update.key,
operation: update.operation,
+ schema: schema ?? null,
});
newValueStrings.push(recast.print(nextExpression).code);
prop.setExpression(nextExpression);
@@ -582,11 +625,13 @@ export const updateSequenceKeyframes = async ({
input,
nodePath,
updates,
+ schema,
prettierConfigOverride,
}: {
input: string;
nodePath: SequenceNodePath;
updates: SequenceKeyframeUpdate[];
+ schema?: SequenceSchema;
prettierConfigOverride?: Record<string, unknown> | null;
}): Promise<{
output: string;
@@ -606,6 +651,7 @@ export const updateSequenceKeyframes = async ({
input,
nodePath,
updates,
+ schema,
});
const {output, formatted} = await formatFileContent({
input: serialized,
@@ -627,11 +673,13 @@ export const updateEffectKeyframesAst = ({
sequenceNodePath,
effectIndex,
updates,
+ schema,
}: {
input: string;
sequenceNodePath: SequenceNodePath;
effectIndex: number;
updates: EffectKeyframeUpdate[];
+ schema?: SequenceSchema;
}): {
serialized: string;
oldValueStrings: string[];
@@ -681,7 +729,9 @@ export const updateEffectKeyframesAst = ({
oldValueStrings.push(recast.print(prop.value).code);
const {expression: nextExpression, introduced} = applyKeyframeOperation({
expression: prop.value as Expression,
+ key: update.key,
operation: update.operation,
+ schema: schema ?? null,
});
newValueStrings.push(recast.print(nextExpression).code);
prop.value = nextExpression as ObjectProperty['value'];
@@ -727,12 +777,14 @@ export const updateEffectKeyframes = async ({
sequenceNodePath,
effectIndex,
updates,
+ schema,
prettierConfigOverride,
}: {
input: string;
sequenceNodePath: SequenceNodePath;
effectIndex: number;
updates: EffectKeyframeUpdate[];
+ schema?: SequenceSchema;
prettierConfigOverride?: Record<string, unknown> | null;
}): Promise<{
output: string;
@@ -755,6 +807,7 @@ export const updateEffectKeyframes = async ({
sequenceNodePath,
effectIndex,
updates,
+ schema,
});
const {output, formatted} = await formatFileContent({
input: serialized,
diff --git a/packages/studio-server/src/preview-server/routes/add-effect-keyframe.ts b/packages/studio-server/src/preview-server/routes/add-effect-keyframe.ts
index 2bd5f44910..111db5b591 100644
--- a/packages/studio-server/src/preview-server/routes/add-effect-keyframe.ts
+++ b/packages/studio-server/src/preview-server/routes/add-effect-keyframe.ts
@@ -64,6 +64,7 @@ export const addEffectKeyframeHandler: ApiHandler<
input: fileContents,
sequenceNodePath: sequenceNodePath.nodePath,
effectIndex,
+ schema,
updates: [
{
key,
diff --git a/packages/studio-server/src/preview-server/routes/add-sequence-keyframe.ts b/packages/studio-server/src/preview-server/routes/add-sequence-keyframe.ts
index 6297090f72..07636a2f5c 100644
--- a/packages/studio-server/src/preview-server/routes/add-sequence-keyframe.ts
+++ b/packages/studio-server/src/preview-server/routes/add-sequence-keyframe.ts
@@ -51,6 +51,7 @@ export const addSequenceKeyframeHandler: ApiHandler<
} = await updateSequenceKeyframes({
input: fileContents,
nodePath: nodePath.nodePath,
+ schema,
updates: [
{
key,
diff --git a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
index c3597a2763..de07c4d4a5 100644
--- a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
+++ b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
@@ -11,6 +11,7 @@ import type {
UnaryExpression,
} from '@babel/types';
import {RenderInternals} from '@remotion/renderer';
+import {isKeyframeInterpolationFunction} from '@remotion/studio-shared';
import type {SubscribeToSequencePropsResponse} from '@remotion/studio-shared';
import * as recast from 'recast';
import type {
@@ -410,8 +411,7 @@ const getInterpolationKeyframes = (
const callExpression = node as CallExpression;
if (
callExpression.callee.type !== 'Identifier' ||
- (callExpression.callee.name !== 'interpolate' &&
- callExpression.callee.name !== 'interpolateColors')
+ !isKeyframeInterpolationFunction(callExpression.callee.name)
) {
return undefined;
}
diff --git a/packages/studio-server/src/test/compute-sequence-props-status.test.ts b/packages/studio-server/src/test/compute-sequence-props-status.test.ts
index 4a2f9a647e..47c4c01cc7 100644
--- a/packages/studio-server/src/test/compute-sequence-props-status.test.ts
+++ b/packages/studio-server/src/test/compute-sequence-props-status.test.ts
@@ -94,6 +94,41 @@ export const Example: React.FC = () => {
});
});
+test('computeSequencePropsStatus should return keyframes for interpolated translate props', () => {
+ const input = `import React from 'react';
+import {Sequence, interpolateTranslate, useCurrentFrame} from 'remotion';
+
+export const Example: React.FC = () => {
+\tconst frame = useCurrentFrame();
+\treturn (
+\t\t<Sequence style={{translate: interpolateTranslate(frame, [0, 100], ['0px 59px', '100px 20px'])}} />
+\t);
+};
+`;
+ const result = computeSequencePropsStatusFromContent({
+ fileContents: input,
+ nodePath: getNodePathFromContent(input, 7),
+ keys: ['style.translate'],
+ effects: [],
+ });
+
+ expect(result.canUpdate).toBe(true);
+ if (!result.canUpdate) throw new Error('Expected canUpdate to be true');
+
+ expect(result.props['style.translate']).toEqual({
+ status: 'keyframed',
+ codeValue: undefined,
+ interpolationFunction: 'interpolateTranslate',
+ keyframes: [
+ {frame: 0, value: '0px 59px'},
+ {frame: 100, value: '100px 20px'},
+ ],
+ easing: ['linear'],
+ clamping: {left: 'extend', right: 'extend'},
+ posterize: undefined,
+ });
+});
+
test('computeSequencePropsStatus should explain why outside-project file reads were blocked', () => {
const remotionRoot = path.join(__dirname, 'snapshots');
const fileName = '../outside.tsx';
diff --git a/packages/studio-server/src/test/update-keyframes-imports.test.ts b/packages/studio-server/src/test/update-keyframes-imports.test.ts
index 6ec4de3be7..dd698df65f 100644
--- a/packages/studio-server/src/test/update-keyframes-imports.test.ts
+++ b/packages/studio-server/src/test/update-keyframes-imports.test.ts
@@ -1,4 +1,5 @@
import {expect, test} from 'bun:test';
+import type {SequenceSchema} from 'remotion';
import {
updateEffectKeyframesAst,
updateSequenceKeyframesAst,
@@ -16,6 +17,13 @@ const getLine = (input: string, needle: string): number => {
return lineIndex + 1;
};
+const translateSchema = {
+ 'style.translate': {
+ type: 'translate',
+ default: '0px 0px',
+ },
+} satisfies SequenceSchema;
+
// ---------------------------------------------------------------------------
// Sequence: imports
// ---------------------------------------------------------------------------
@@ -69,6 +77,32 @@ export const Example: React.FC = () => {
expect(serialized).toContain('const frame = useCurrentFrame();');
});
+test('adds interpolateTranslate import for translate conversion', () => {
+ const input = `import React from 'react';
+import {AbsoluteFill} from 'remotion';
+
+export const Example: React.FC = () => {
+\treturn <AbsoluteFill style={{translate: '0px 59px'}} />;
+};
+`;
+ const {serialized} = updateSequenceKeyframesAst({
+ input,
+ nodePath: lineColumnToNodePath(input, getLine(input, '<AbsoluteFill')),
+ schema: translateSchema,
+ updates: [
+ {
+ key: 'style.translate',
+ operation: {type: 'add', frame: 44, value: '0px 59px'},
+ },
+ ],
+ });
+
+ expect(serialized).toContain('interpolateTranslate');
+ expect(serialized).not.toContain('interpolateColors');
+ expect(serialized).toContain('useCurrentFrame');
+ expect(serialized).toContain('const frame = useCurrentFrame();');
+});
+
test('creates a new remotion import when one is missing entirely', () => {
const input = `import React from 'react';
diff --git a/packages/studio-server/src/test/update-keyframes.test.ts b/packages/studio-server/src/test/update-keyframes.test.ts
index ae2a1c95a6..e7252fd1e7 100644
--- a/packages/studio-server/src/test/update-keyframes.test.ts
+++ b/packages/studio-server/src/test/update-keyframes.test.ts
@@ -1,4 +1,5 @@
import {expect, test} from 'bun:test';
+import type {SequenceSchema} from 'remotion';
import {
updateEffectKeyframesAst,
updateSequenceKeyframes,
@@ -31,6 +32,25 @@ export const Example: React.FC = () => {
};
`;
+const translateSchema = {
+ 'style.translate': {
+ type: 'translate',
+ default: '0px 0px',
+ },
+} satisfies SequenceSchema;
+
+const translateInput = `import React from 'react';
+import {AbsoluteFill} from 'remotion';
+
+export const Example: React.FC = () => {
+\treturn (
+\t\t<AbsoluteFill>
+\t\t\t<div style={{translate: '0px 59px'}} />
+\t\t</AbsoluteFill>
+\t);
+};
+`;
+
const effectInput = `import {tint} from '@remotion/effects/tint';
import {HtmlInCanvas} from '@remotion/html-in-canvas';
import {interpolate, useCurrentFrame} from 'remotion';
@@ -157,6 +177,60 @@ test('updateSequenceKeyframes converts a static string value to a color interpol
expect(output).toContain("color={interpolateColors(frame, [50], ['blue'])}");
});
+test('updateSequenceKeyframes converts static translate to interpolateTranslate', async () => {
+ const {output, oldValueStrings} = await updateSequenceKeyframes({
+ input: translateInput,
+ nodePath: lineColumnToNodePath(
+ translateInput,
+ getLine(translateInput, 'translate'),
+ ),
+ schema: translateSchema,
+ updates: [
+ {
+ key: 'style.translate',
+ operation: {type: 'add', frame: 44, value: '0px 59px'},
+ },
+ ],
+ });
+
+ expect(oldValueStrings).toEqual(["'0px 59px'"]);
+ expect(output).toContain(
+ "translate: interpolateTranslate(frame, [44], ['0px 59px'])",
+ );
+ expect(output).toContain('interpolateTranslate');
+});
+
+test('updateSequenceKeyframes migrates translate away from interpolateColors', async () => {
+ const input = translateInput
+ .replace(
+ "import {AbsoluteFill} from 'remotion';",
+ "import {AbsoluteFill, interpolateColors, useCurrentFrame} from 'remotion';",
+ )
+ .replace('return (', 'const frame = useCurrentFrame();\n\treturn (')
+ .replace(
+ "translate: '0px 59px'",
+ "translate: interpolateColors(frame, [44], ['0px 59px'])",
+ );
+ const {output, oldValueStrings} = await updateSequenceKeyframes({
+ input,
+ nodePath: lineColumnToNodePath(input, getLine(input, 'translate')),
+ schema: translateSchema,
+ updates: [
+ {
+ key: 'style.translate',
+ operation: {type: 'add', frame: 88, value: '100px 20px'},
+ },
+ ],
+ });
+
+ expect(oldValueStrings).toEqual([
+ "interpolateColors(frame, [44], ['0px 59px'])",
+ ]);
+ expect(output).toContain('translate: interpolateTranslate(');
+ expect(output).toContain('[44, 88]');
+ expect(output).toContain("['0px 59px', '100px 20px']");
+});
+
test('updateSequenceKeyframes returns a node path that still resolves after inserting a frame hook', async () => {
const input = `import {starburst} from '@remotion/starburst';
import React from 'react';
diff --git a/packages/studio-shared/src/index.ts b/packages/studio-shared/src/index.ts
index 4562d1ed0d..0ce0898235 100644
--- a/packages/studio-shared/src/index.ts
+++ b/packages/studio-shared/src/index.ts
@@ -104,6 +104,13 @@ export {
ModuleMap,
hotMiddlewareOptions,
} from './hot-middleware';
+export {
+ getKeyframeInterpolationFunction,
+ getKeyframeInterpolationFunctionForSchemaField,
+ isKeyframeInterpolationFunction,
+ keyframeInterpolationFunctions,
+ type KeyframeInterpolationFunction,
+} from './keyframe-interpolation-function';
export {DEFAULT_TIMELINE_TRACKS} from './max-timeline-tracks';
export {
Pkgs,
diff --git a/packages/studio-shared/src/keyframe-interpolation-function.ts b/packages/studio-shared/src/keyframe-interpolation-function.ts
new file mode 100644
index 0000000000..b565acb1aa
--- /dev/null
+++ b/packages/studio-shared/src/keyframe-interpolation-function.ts
@@ -0,0 +1,86 @@
+import type {SequenceFieldSchema, SequenceSchema} from 'remotion';
+
+export const keyframeInterpolationFunctions = [
+ 'interpolate',
+ 'interpolateColors',
+ 'interpolateTranslate',
+] as const;
+
+export type KeyframeInterpolationFunction =
+ (typeof keyframeInterpolationFunctions)[number];
+
+export const isKeyframeInterpolationFunction = (
+ name: string,
+): name is KeyframeInterpolationFunction => {
+ return keyframeInterpolationFunctions.includes(
+ name as KeyframeInterpolationFunction,
+ );
+};
+
+const findFieldInSchema = (
+ schema: SequenceSchema,
+ key: string,
+): SequenceFieldSchema | undefined => {
+ if (key in schema) {
+ return schema[key];
+ }
+
+ for (const field of Object.values(schema)) {
+ if (field.type !== 'enum') {
+ continue;
+ }
+
+ for (const variant of Object.values(field.variants)) {
+ const found = findFieldInSchema(variant, key);
+ if (found) {
+ return found;
+ }
+ }
+ }
+
+ return undefined;
+};
+
+export const getKeyframeInterpolationFunctionForSchemaField = ({
+ schema,
+ key,
+}: {
+ schema: SequenceSchema | null;
+ key: string;
+}): KeyframeInterpolationFunction | null => {
+ const field = schema ? findFieldInSchema(schema, key) : undefined;
+
+ if (field?.type === 'translate') {
+ return 'interpolateTranslate';
+ }
+
+ if (field?.type === 'color') {
+ return 'interpolateColors';
+ }
+
+ return null;
+};
+
+export const getKeyframeInterpolationFunction = ({
+ schema,
+ key,
+ staticValue,
+ newValue,
+}: {
+ schema: SequenceSchema | null;
+ key: string;
+ staticValue: unknown;
+ newValue: unknown;
+}): KeyframeInterpolationFunction => {
+ const schemaFunction = getKeyframeInterpolationFunctionForSchemaField({
+ schema,
+ key,
+ });
+ if (schemaFunction) {
+ return schemaFunction;
+ }
+
+ return typeof staticValue === 'string' && typeof newValue === 'string'
+ ? 'interpolateColors'
+ : 'interpolate';
+};
diff --git a/packages/studio-shared/src/optimistic-add-keyframe.ts b/packages/studio-shared/src/optimistic-add-keyframe.ts
index 5a32c0e767..fc68070996 100644
--- a/packages/studio-shared/src/optimistic-add-keyframe.ts
+++ b/packages/studio-shared/src/optimistic-add-keyframe.ts
@@ -1,25 +1,22 @@
import {
type CanUpdateSequencePropStatus,
type CanUpdateSequencePropsResponse,
+ type SequenceSchema,
} from 'remotion';
-
-const getInterpolationFunction = (
- staticValue: unknown,
- newValue: unknown,
-): 'interpolate' | 'interpolateColors' => {
- return typeof staticValue === 'string' && typeof newValue === 'string'
- ? 'interpolateColors'
- : 'interpolate';
-};
+import {getKeyframeInterpolationFunction} from './keyframe-interpolation-function';
const addKeyframeToPropStatus = ({
status,
+ fieldKey,
frame,
value,
+ schema,
}: {
status: CanUpdateSequencePropStatus;
+ fieldKey: string;
frame: number;
value: unknown;
+ schema: SequenceSchema | null;
}): CanUpdateSequencePropStatus => {
if (status.status === 'keyframed') {
const existingIndex = status.keyframes.findIndex(
@@ -57,7 +54,12 @@ const addKeyframeToPropStatus = ({
return {
status: 'keyframed',
codeValue: undefined,
- interpolationFunction: getInterpolationFunction(staticValue, value),
+ interpolationFunction: getKeyframeInterpolationFunction({
+ schema,
+ key: fieldKey,
+ staticValue,
+ newValue: value,
+ }),
keyframes: [{frame, value}],
easing: [],
clamping: {left: 'extend', right: 'extend'},
@@ -73,11 +75,13 @@ export const optimisticAddSequenceKeyframe = ({
fieldKey,
frame,
value,
+ schema,
}: {
previous: CanUpdateSequencePropsResponse;
fieldKey: string;
frame: number;
value: unknown;
+ schema?: SequenceSchema;
}): CanUpdateSequencePropsResponse => {
if (!previous.canUpdate) {
return previous;
@@ -92,7 +96,13 @@ export const optimisticAddSequenceKeyframe = ({
...previous,
props: {
...previous.props,
- [fieldKey]: addKeyframeToPropStatus({status, frame, value}),
+ [fieldKey]: addKeyframeToPropStatus({
+ status,
+ fieldKey,
+ frame,
+ value,
+ schema: schema ?? null,
+ }),
},
};
};
@@ -103,12 +113,14 @@ export const optimisticAddEffectKeyframe = ({
fieldKey,
frame,
value,
+ schema,
}: {
previous: CanUpdateSequencePropsResponse;
effectIndex: number;
fieldKey: string;
frame: number;
value: unknown;
+ schema?: SequenceSchema;
}): CanUpdateSequencePropsResponse => {
if (!previous.canUpdate) {
return previous;
@@ -135,7 +147,13 @@ export const optimisticAddEffectKeyframe = ({
...target,
props: {
...target.props,
- [fieldKey]: addKeyframeToPropStatus({status, frame, value}),
+ [fieldKey]: addKeyframeToPropStatus({
+ status,
+ fieldKey,
+ frame,
+ value,
+ schema: schema ?? null,
+ }),
},
};
diff --git a/packages/studio-shared/src/test/optimistic-add-keyframe.test.ts b/packages/studio-shared/src/test/optimistic-add-keyframe.test.ts
index 5c71da6d5f..fe5a92b10f 100644
--- a/packages/studio-shared/src/test/optimistic-add-keyframe.test.ts
+++ b/packages/studio-shared/src/test/optimistic-add-keyframe.test.ts
@@ -1,5 +1,5 @@
import {expect, test} from 'bun:test';
-import type {CanUpdateSequencePropsResponse} from 'remotion';
+import type {CanUpdateSequencePropsResponse, SequenceSchema} from 'remotion';
import {
optimisticAddEffectKeyframe,
optimisticAddSequenceKeyframe,
@@ -37,6 +37,45 @@ test('optimisticAddSequenceKeyframe converts a static prop to a single keyframe'
expect(status.easing).toEqual([]);
});
+test('optimisticAddSequenceKeyframe uses interpolateTranslate for translate fields', () => {
+ const previous: CanUpdateSequencePropsResponse = {
+ canUpdate: true,
+ props: {
+ 'style.translate': {
+ status: 'static',
+ codeValue: '0px 59px',
+ },
+ },
+ effects: [],
+ };
+ const schema = {
+ 'style.translate': {
+ type: 'translate',
+ default: '0px 0px',
+ },
+ } satisfies SequenceSchema;
+
+ const updated = optimisticAddSequenceKeyframe({
+ previous,
+ fieldKey: 'style.translate',
+ frame: 44,
+ value: '0px 59px',
+ schema,
+ });
+
+ if (!updated.canUpdate) {
+ throw new Error('expected updateable sequence');
+ }
+
+ const status = updated.props['style.translate'];
+ if (!status || status.status !== 'keyframed') {
+ throw new Error('expected keyframed status');
+ }
+
+ expect(status.interpolationFunction).toBe('interpolateTranslate');
+ expect(status.keyframes).toEqual([{frame: 44, value: '0px 59px'}]);
+});
+
test('optimisticAddSequenceKeyframe appends a keyframe to an existing interpolation', () => {
const previous: CanUpdateSequencePropsResponse = {
canUpdate: true,
diff --git a/packages/studio/src/components/Timeline/call-add-keyframe.ts b/packages/studio/src/components/Timeline/call-add-keyframe.ts
index 69dab56f5f..4f4c585b32 100644
--- a/packages/studio/src/components/Timeline/call-add-keyframe.ts
+++ b/packages/studio/src/components/Timeline/call-add-keyframe.ts
@@ -35,6 +35,7 @@ export const callAddSequenceKeyframe = ({
fieldKey,
frame: sourceFrame,
value,
+ schema,
}),
apiCall: () =>
callApi('/api/add-sequence-keyframe', {
@@ -81,6 +82,7 @@ export const callAddEffectKeyframe = ({
fieldKey,
frame: sourceFrame,
value,
+ schema,
}),
apiCall: () =>
callApi('/api/add-effect-keyframe', {