🎥 Make videos programmatically with React
add measureSpring function
Samuel3a540f3a78c7
diff --git a/packages/core/src/spring.ts b/packages/core/src/spring.tsindex 76f0b89c9d..b140b4df25 100644--- a/packages/core/src/spring.ts+++ b/packages/core/src/spring.ts@@ -3,6 +3,8 @@ type AnimationNode = {toValue: number;current: number;velocity: number;+ isFinished?: boolean;+ prevPosition?: number;};export type SpringConfig = {@@ -68,6 +70,27 @@ function advance(criticallyDampedEnvelope *(v0 * (t * omega0 - 1) + t * x0 * omega0 * omega0);++ let isOvershooting+ if(config.overshootClamping && config.stiffness != 0){+ if(animation.prevPosition != null && animation.prevPosition < toValue){+ isOvershooting = current > toValue+ }else{+ isOvershooting = current < toValue+ }+ }else{+ isOvershooting = false+ }++ const isVelocity = Math.abs(animation.velocity) < 0.001;++ const isDisplacement = (config.stiffness == 0) || (Math.abs(toValue - current) < 0.001);++ if(isOvershooting == true || (isVelocity && isDisplacement)){+ copiedAnimated.isFinished = true+ }++ copiedAnimated.prevPosition = currentif (zeta < 1) {copiedAnimated.current = underDampedPosition;copiedAnimated.velocity = underDampedVelocity;@@ -79,24 +102,28 @@ function advance(return copiedAnimated;}-export function spring({+function springCalculation({from = 0,to = 1,frame,fps,config = {},+ prevPosition = 0,}: {from?: number;to?: number;frame: number;fps: number;config?: Partial<SpringConfig>;-}): number {+ prevPosition ?: number;+}): AnimationNode {let animation: AnimationNode = {lastTimestamp: 0,current: from,toValue: to,velocity: 0,+ isFinished: false,+ prevPosition};const frameClamped = Math.max(0, frame);const unevenRest = frameClamped % 1;@@ -110,5 +137,41 @@ export function spring({...config,});}- return animation.current;+ return animation;}++export function spring({+ from = 0,+ to = 1,+ frame,+ fps,+ config = {},+}: {+ from?: number;+ to?: number;+ frame: number;+ fps: number;+ config?: Partial<SpringConfig>;+}): number {+ return springCalculation({fps, frame, config, from, to}).current+}++export function measureSpring({+ from = 0,+ to = 1,+ fps,+ config = {},+}: {+ from?: number;+ to?: number;+ fps: number;+ config?: Partial<SpringConfig>;+}): number {+ let iteration = 0;+ let animation = springCalculation({prevPosition: 0, fps, frame: iteration, config, from, to})+ while(!animation.isFinished){+ animation = springCalculation({prevPosition: animation.prevPosition, fps, frame: iteration++, config, from, to})+ }+ //minus 1 because the last calculate animation was finished+ return iteration - 1;+}\ No newline at end of file