🎥 Make videos programmatically with React
#2210 Add option for rendering a spring animation
tachyonicbytes25a5df4b96fc
diff --git a/packages/core/src/spring/index.ts b/packages/core/src/spring/index.tsindex b2940dd58d..703c67297e 100644--- a/packages/core/src/spring/index.ts+++ b/packages/core/src/spring/index.ts@@ -10,6 +10,7 @@ import {springCalculation} from './spring-utils.js';* @see [Documentation](https://www.remotion.dev/docs/spring)* @param {number} frame The current time value. Most of the time you want to pass in the return value of useCurrentFrame.* @param {number} fps The framerate at which the animation runs. Pass in the value obtained by `useVideoConfig()`.+ * @param {?boolean} reverse Whether the animation plays in reverse or not. Default `false`.* @param {?Object} config optional object that allows you to customize the physical properties of the animation.* @param {number} [config.mass=1] The weight of the spring. If you reduce the mass, the animation becomes faster!* @param {number} [config.damping=10] How hard the animation decelerates.@@ -30,6 +31,7 @@ export function spring({durationInFrames,durationRestThreshold,delay = 0,+ reverse = false}: {frame: number;fps: number;@@ -39,6 +41,7 @@ export function spring({durationInFrames?: number;durationRestThreshold?: number;delay?: number;+ reverse?: boolean;}): number {validateSpringDuration(durationInFrames);validateFrame({@@ -48,20 +51,25 @@ export function spring({});validateFps(fps, 'to spring()', false);- const durationRatio =- durationInFrames === undefined- ? 1- : durationInFrames /- measureSpring({- fps,- config,- from,- to,- threshold: durationRestThreshold,- });+ const duration = measureSpring({+ fps,+ config,+ from,+ to,+ threshold: durationRestThreshold,+ });+ if (durationInFrames === undefined) {+ durationInFrames = duration;+ }++ const durationRatio = durationInFrames / duration;// Delay the spring by telling the calculation we're at an earlier frame.- const frame = passedFrame - delay;+ let frame = passedFrame - delay;++ if (reverse) {+ frame = durationInFrames - frame;+ }const spr = springCalculation({fps,diff --git a/packages/core/src/test/spring.test.ts b/packages/core/src/test/spring.test.tsindex 41253f5bf6..c21b2ddc27 100644--- a/packages/core/src/test/spring.test.ts+++ b/packages/core/src/test/spring.test.ts@@ -1,6 +1,7 @@import {describe, expect, test} from 'vitest';import {isApproximatelyTheSame} from '../is-approximately-the-same.js';import {spring} from '../spring/index.js';+import {measureSpring} from '../spring/measure-spring.js';test('Basic spring to equal 0', () => {expect(@@ -53,6 +54,50 @@ test('Should be able to set duration for spring', () => {).toBeCloseTo(1);});+// Reverse tests+test('Should be able to set duration for spring when in reverse', () => {+ expect(+ spring({+ fps: 30,+ frame: 5,+ durationInFrames: 5,+ reverse: true+ })+ ).toBeCloseTo(0);+});++test('Should be able to set duration for spring when in reverse', () => {+ expect(+ spring({+ fps: 30,+ frame: 0,+ durationInFrames: 5,+ reverse: true+ })+ ).toBeCloseTo(1);+});++// This is the reverse of one of the first tests+test('Should be approxmiately the same when in reverse', () => {+ expect(+ spring({+ fps: 30,+ // duration - 1+ frame: measureSpring({fps: 30}) - 1,+ reverse: true+ }),+ ).toBeCloseTo(0.04941510804510185);+});++test('Should be close to 0 when in reverse', () => {+ expect(+ spring({+ fps: 30,+ frame: 100,+ reverse: true+ })+ ).toBeCloseTo(0);+});describe('Should be able to delay a spring', () => {test('Should clamp all frames before the delay to zero', () => {diff --git a/packages/docs/docs/spring.md b/packages/docs/docs/spring.mdindex 3835434b4b..6318f621fa 100644--- a/packages/docs/docs/spring.md+++ b/packages/docs/docs/spring.md@@ -46,6 +46,12 @@ The end value of the animation. Note that depending on the parameters, spring anFor how many frames per second the spring animation should be calculated. This should always be the `fps` property of the return value of [`useVideoConfig()`](/docs/use-video-config).+### reverse++_Default:_ `false`++Render the animation in reverse. When this option is enabled, passing a `frame` is like passing `duration - frame`.+### configAn optional object that allows you to customize the physical properties of the animation.