Skip to content

🎥 Make videos programmatically with React

Add clamp defaults for new Studio keyframes

Cursor Agent04370706e3e1
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 fd1338c08a..5cb483d79f 100644
--- a/packages/studio-server/src/codemods/update-keyframes/update-keyframes.ts
+++ b/packages/studio-server/src/codemods/update-keyframes/update-keyframes.ts
@@ -216,6 +216,16 @@ const createFrameExpression = (frame: number): ExpressionKind => {
return parseValueExpression(frame);
};
+const createClampOptionsExpression = (): ExpressionKind => {
+ return b.objectExpression([
+ b.objectProperty(b.identifier('extrapolateLeft'), b.stringLiteral('clamp')),
+ b.objectProperty(
+ b.identifier('extrapolateRight'),
+ b.stringLiteral('clamp'),
+ ),
+ ]) as ExpressionKind;
+};
+
const createInterpolateExpression = ({
callee,
input,
@@ -322,12 +332,16 @@ const addKeyframe = ({
staticValue,
newValue: value,
});
+ const extraArgs =
+ callee.type === 'Identifier' && callee.name === 'interpolateColors'
+ ? []
+ : [createClampOptionsExpression()];
return {
expression: createInterpolateExpression({
callee,
input: b.identifier('frame'),
- extraArgs: [],
+ extraArgs,
keyframes,
}),
introduced: {
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 6985d7eb74..606cea3e34 100644
--- a/packages/studio-server/src/test/update-keyframes-imports.test.ts
+++ b/packages/studio-server/src/test/update-keyframes-imports.test.ts
@@ -59,7 +59,9 @@ export const Example: React.FC = () => {
/import\s*\{[^}]*\bAbsoluteFill\b[^}]*\binterpolate\b[^}]*\buseCurrentFrame\b[^}]*\}\s*from\s*['"]remotion['"]/,
);
expect(serialized).toContain('const frame = useCurrentFrame();');
- expect(serialized).toContain('interpolate(frame, [30], [1])');
+ expect(serialized).toContain('interpolate(frame, [30], [1], {');
+ expect(serialized).toContain('extrapolateLeft: "clamp"');
+ expect(serialized).toContain('extrapolateRight: "clamp"');
});
test('adds interpolateColors import (not interpolate) for color conversion', () => {
@@ -396,7 +398,9 @@ export const Example: React.FC = () => (
expect(serialized).toContain('const frame = useCurrentFrame();');
expect(serialized).toContain('return');
- expect(serialized).toContain('interpolate(frame, [20], [1])');
+ expect(serialized).toContain('interpolate(frame, [20], [1], {');
+ expect(serialized).toContain('extrapolateLeft: "clamp"');
+ expect(serialized).toContain('extrapolateRight: "clamp"');
});
test('does not add a duplicate frame hook when user already has one with a different intermediate statement', () => {
@@ -455,7 +459,9 @@ export const Comp = () => {
/import\s*\{[^}]*\binterpolate\b[^}]*\buseCurrentFrame\b[^}]*\}\s*from\s*['"]remotion['"]/,
);
expect(serialized).toContain('const frame = useCurrentFrame();');
- expect(serialized).toContain('interpolate(frame, [100], [1])');
+ expect(serialized).toContain('interpolate(frame, [100], [1], {');
+ expect(serialized).toContain('extrapolateLeft: "clamp"');
+ expect(serialized).toContain('extrapolateRight: "clamp"');
});
test('effect keyframe remove does not modify imports or insert a frame hook', () => {
diff --git a/packages/studio-server/src/test/update-keyframes.test.ts b/packages/studio-server/src/test/update-keyframes.test.ts
index 64835fdc2d..ef83870fb9 100644
--- a/packages/studio-server/src/test/update-keyframes.test.ts
+++ b/packages/studio-server/src/test/update-keyframes.test.ts
@@ -134,7 +134,9 @@ test('updateSequenceKeyframes converts a static value to an interpolation', asyn
});
expect(oldValueStrings).toEqual(['0.5']);
- expect(output).toContain('opacity: interpolate(frame, [25], [0.75])');
+ expect(output).toContain('opacity: interpolate(frame, [25], [0.75], {');
+ expect(output).toContain("extrapolateLeft: 'clamp'");
+ expect(output).toContain("extrapolateRight: 'clamp'");
});
test('updateSequenceKeyframes rejects non-keyframable fields', async () => {
@@ -181,7 +183,9 @@ test('updateSequenceKeyframes converts a static value to a single-keyframe inter
});
expect(oldValueStrings).toEqual(['0.5']);
- expect(output).toContain('opacity: interpolate(frame, [0], [0.75])');
+ expect(output).toContain('opacity: interpolate(frame, [0], [0.75], {');
+ expect(output).toContain("extrapolateLeft: 'clamp'");
+ expect(output).toContain("extrapolateRight: 'clamp'");
});
test('updateSequenceKeyframes adds a keyframe to an existing color interpolation', async () => {
@@ -242,8 +246,10 @@ test('updateSequenceKeyframes converts static translate to interpolateTranslate'
expect(oldValueStrings).toEqual(["'0px 59px'"]);
expect(output).toContain(
- "translate: interpolateTranslate(frame, [44], ['0px 59px'])",
+ "translate: interpolateTranslate(frame, [44], ['0px 59px'], {",
);
+ expect(output).toContain("extrapolateLeft: 'clamp'");
+ expect(output).toContain("extrapolateRight: 'clamp'");
expect(output).toContain('interpolateTranslate');
});
@@ -368,8 +374,10 @@ export default CenteredSolid;
],
});
- expect(output).toContain('width={interpolate(frame, [11], [240])}');
expect(updatedNodePath).toEqual(nodePath);
+ expect(output).toContain('width={interpolate(frame, [11], [240], {');
+ expect(output).toContain("extrapolateLeft: 'clamp'");
+ expect(output).toContain("extrapolateRight: 'clamp'");
const status = computeSequencePropsStatusFromContent({
fileContents: output,
nodePath: updatedNodePath,
@@ -382,7 +390,7 @@ export default CenteredSolid;
interpolationFunction: 'interpolate',
keyframes: [{frame: 11, value: 240}],
easing: [],
- clamping: {left: 'extend', right: 'extend'},
+ clamping: {left: 'clamp', right: 'clamp'},
posterize: undefined,
});
});
@@ -456,6 +464,32 @@ test('updateSequenceKeyframes keeps a color interpolation when one keyframe rema
expect(output).toContain("color={interpolateColors(frame, [100], ['blue'])}");
});
+test('updateEffectKeyframes converts a static value to a clamped interpolation', () => {
+ const input = effectInput.replace(
+ 'interpolate(frame, [0, 50, 100], [0.2, 0.5, 0.8])',
+ '0.2',
+ );
+ const {serialized, oldValueStrings} = updateEffectKeyframesAst({
+ input,
+ sequenceNodePath: lineColumnToNodePath(
+ input,
+ getLine(input, '<HtmlInCanvas'),
+ ),
+ effectIndex: 0,
+ updates: [
+ {
+ key: 'amount',
+ operation: {type: 'add', frame: 40, value: 0.6},
+ },
+ ],
+ });
+
+ expect(oldValueStrings).toEqual(['0.2']);
+ expect(serialized).toContain('amount: interpolate(frame, [40], [0.6], {');
+ expect(serialized).toContain('extrapolateLeft: "clamp"');
+ expect(serialized).toContain('extrapolateRight: "clamp"');
+});
+
test('updateSequenceKeyframes converts the last color keyframe to a static value', async () => {
const input = colorInput.replace(
"interpolateColors(frame, [0, 100], ['red', 'blue'])",
diff --git a/packages/studio-shared/src/optimistic-add-keyframe.ts b/packages/studio-shared/src/optimistic-add-keyframe.ts
index 0aaf1583d8..1dd951d9e5 100644
--- a/packages/studio-shared/src/optimistic-add-keyframe.ts
+++ b/packages/studio-shared/src/optimistic-add-keyframe.ts
@@ -65,7 +65,7 @@ const addKeyframeToPropStatus = ({
}),
keyframes: [{frame, value}],
easing: [],
- clamping: {left: 'extend', right: 'extend'},
+ clamping: {left: 'clamp', right: 'clamp'},
posterize: undefined,
};
}
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 407f8ba48f..7579fe3ae3 100644
--- a/packages/studio-shared/src/test/optimistic-add-keyframe.test.ts
+++ b/packages/studio-shared/src/test/optimistic-add-keyframe.test.ts
@@ -35,6 +35,7 @@ test('optimisticAddSequenceKeyframe converts a static prop to a single keyframe'
expect(status.keyframes).toEqual([{frame: 25, value: 0.75}]);
expect(status.easing).toEqual([]);
+ expect(status.clamping).toEqual({left: 'clamp', right: 'clamp'});
});
test('optimisticAddSequenceKeyframe uses interpolateTranslate for translate fields', () => {
@@ -74,6 +75,7 @@ test('optimisticAddSequenceKeyframe uses interpolateTranslate for translate fiel
expect(status.interpolationFunction).toBe('interpolateTranslate');
expect(status.keyframes).toEqual([{frame: 44, value: '0px 59px'}]);
+ expect(status.clamping).toEqual({left: 'clamp', right: 'clamp'});
});
test('optimisticAddSequenceKeyframe uses interpolateRotate for rotation-css fields', () => {
@@ -287,4 +289,5 @@ test('optimisticAddEffectKeyframe converts a static prop to a single keyframe',
expect(status.keyframes).toEqual([{frame: 30, value: 0.5}]);
expect(status.easing).toEqual([]);
+ expect(status.clamping).toEqual({left: 'clamp', right: 'clamp'});
});