🎥 Make videos programmatically with React
Add delay parameter to Spring
Rob Jackson6c9770163377
diff --git a/packages/core/src/spring/index.ts b/packages/core/src/spring/index.tsindex 5a289c4463..ae81ff821b 100644--- a/packages/core/src/spring/index.ts+++ b/packages/core/src/spring/index.ts@@ -19,6 +19,7 @@ import {springCalculation} from './spring-utils.js';* @param {?number} [config.to] The end value of the animation. Default `1`* @param {?number} [config.durationInFrames] Stretch the duration of an animation to a set value.. Default `undefined`* @param {?number} [config.durationThreshold] How close to the end the animation is considered to be done. Default `0.005`+ * @param {?number} [config.delay] Delay the animation for this amount of frames. Default `0`*/export function spring({frame,@@ -28,6 +29,7 @@ export function spring({to = 1,durationInFrames,durationRestThreshold,+ delay = 0,}: {frame: number;fps: number;@@ -36,6 +38,7 @@ export function spring({to?: number;durationInFrames?: number;durationRestThreshold?: number;+ delay?: number;}): number {validateSpringDuration(durationInFrames);validateFrame({frame, durationInFrames: Infinity, allowFloats: true});@@ -53,6 +56,10 @@ export function spring({threshold: durationRestThreshold,});+ // Delay the spring by telling the calculation we're at an earlier frame.+ // All frames up to the delay will be clamped to zero.+ frame = Math.max(0, frame - delay);+const spr = springCalculation({fps,frame: frame / durationRatio,diff --git a/packages/core/src/test/spring.test.ts b/packages/core/src/test/spring.test.tsindex ef6d708adf..41253f5bf6 100644--- a/packages/core/src/test/spring.test.ts+++ b/packages/core/src/test/spring.test.ts@@ -1,4 +1,4 @@-import {expect, test} from 'vitest';+import {describe, expect, test} from 'vitest';import {isApproximatelyTheSame} from '../is-approximately-the-same.js';import {spring} from '../spring/index.js';@@ -52,3 +52,50 @@ test('Should be able to set duration for spring', () => {})).toBeCloseTo(1);});+++describe('Should be able to delay a spring', () => {+ test('Should clamp all frames before the delay to zero', () => {+ expect(+ spring({+ fps: 30,+ frame: 0,+ delay: 25,+ })+ ).toEqual(0);++ expect(+ spring({+ fps: 30,+ frame: 15,+ delay: 25,+ })+ ).toEqual(0);++ expect(+ spring({+ fps: 30,+ frame: 25,+ delay: 25,+ })+ ).toEqual(0);+ });++ test('Should start calculations from the delay + 1', () => {+ expect(spring({+ fps: 30,+ frame: 26,+ delay: 25,+ })).toBeGreaterThan(0)+ });++ test('Should be close to 1', () => {+ expect(+ spring({+ fps: 30,+ frame: 25+30,+ delay: 25,+ })+ ).toBeCloseTo(1);+ });+})\ No newline at end of filediff --git a/packages/docs/docs/spring.md b/packages/docs/docs/spring.mdindex 2a777516ce..72c719e138 100644--- a/packages/docs/docs/spring.md+++ b/packages/docs/docs/spring.md@@ -104,6 +104,14 @@ How close the animation should be to the end in order to be considered finishedFor example, if a `durationRestThreshold` of `0.001` is given, and the durationOfFrames is `30`, it means that after 30 frames, the spring has reached 99.9% (`1 - 0.001 = 0.999`) of it's distance to the end value.+## Â `delay` <AvailableFrom v="3.3.90">++_optional_++How many frames to delay the animation for.++For example, if a `delay` of `25` is given frames 1 through 25 will return `0`, and the calculation will effectively start from frame 26.+## YouTube videoWant to understand the different properties like `mass`, `stiffness`, `damping` etc.? We made a video trying to make sense of all the parameters!