Skip to content

🎥 Make videos programmatically with React

`remotion`: Treat undefined spring config properties as defaults

John Costa174f7a0c5d9b
diff --git a/packages/core/src/spring/spring-utils.ts b/packages/core/src/spring/spring-utils.ts
index bf227c65ca..26319161c2 100644
--- a/packages/core/src/spring/spring-utils.ts
+++ b/packages/core/src/spring/spring-utils.ts
@@ -117,13 +117,22 @@ export function springCalculation({
}): AnimationNode {
const from = 0;
const to = 1;
+ // An explicit `undefined` (e.g. a forwarded optional prop) must not
+ // override the defaults, otherwise the physics turn into NaN
+ const resolvedConfig: SpringConfig = {
+ damping: config.damping ?? defaultSpringConfig.damping,
+ mass: config.mass ?? defaultSpringConfig.mass,
+ stiffness: config.stiffness ?? defaultSpringConfig.stiffness,
+ overshootClamping:
+ config.overshootClamping ?? defaultSpringConfig.overshootClamping,
+ };
const cacheKey = [
frame,
fps,
- config.damping,
- config.mass,
- config.overshootClamping,
- config.stiffness,
+ resolvedConfig.damping,
+ resolvedConfig.mass,
+ resolvedConfig.overshootClamping,
+ resolvedConfig.stiffness,
].join('-');
if (calculationCache[cacheKey]) {
return calculationCache[cacheKey];
@@ -143,10 +152,7 @@ export function springCalculation({
animation = advance({
animation,
now: time,
- config: {
- ...defaultSpringConfig,
- ...config,
- },
+ config: resolvedConfig,
});
}
@@ -154,10 +160,7 @@ export function springCalculation({
animation = advance({
animation,
now: (frameClamped / fps) * 1000,
- config: {
- ...defaultSpringConfig,
- ...config,
- },
+ config: resolvedConfig,
});
}
diff --git a/packages/core/src/test/spring.test.ts b/packages/core/src/test/spring.test.ts
index f9ce685adf..b415bf66af 100644
--- a/packages/core/src/test/spring.test.ts
+++ b/packages/core/src/test/spring.test.ts
@@ -237,3 +237,29 @@ test('from / to', () => {
}),
).toBeCloseTo(169.082);
});
+
+describe('Undefined config properties should fall back to the defaults', () => {
+ // Each case uses its own fps so it does not share cache entries with
+ // other tests. The undefined config is evaluated first on purpose: it
+ // shares a cache key with the default config, so a NaN result would
+ // poison every later default-config call at the same frame.
+ test.each([
+ [{damping: undefined}, 23],
+ [{stiffness: undefined}, 25],
+ [{mass: undefined}, 27],
+ [{damping: undefined, stiffness: undefined, mass: undefined}, 29],
+ ])('config %p', (config, fps) => {
+ const value = spring({fps, frame: 10, config});
+ expect(Number.isNaN(value)).toBe(false);
+ // Spelled-out defaults have their own cache key, so this is a fresh
+ // calculation and not a copy of the value under test
+ expect(value).toBe(
+ spring({
+ fps,
+ frame: 10,
+ config: {damping: 10, mass: 1, stiffness: 100},
+ }),
+ );
+ expect(value).toBe(spring({fps, frame: 10}));
+ });
+});