Skip to content

Commit

Permalink
Merge pull request #3875 from remotion-dev/spring-from-to
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyBurger committed May 15, 2024
2 parents 1e77c05 + c2275d0 commit 3338706
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
2 changes: 0 additions & 2 deletions packages/core/src/spring/index.ts
Expand Up @@ -59,8 +59,6 @@ export function spring({
? measureSpring({
fps,
config,
from,
to,
threshold: durationRestThreshold,
})
: undefined;
Expand Down
30 changes: 14 additions & 16 deletions packages/core/src/spring/measure-spring.ts
@@ -1,9 +1,21 @@
import type {ENABLE_V5_BREAKING_CHANGES} from '../v5-flag.js';
import {validateFps} from '../validation/validate-fps.js';
import type {SpringConfig} from './spring-utils';
import {springCalculation} from './spring-utils.js';

const cache = new Map<string, number>();

type V4Props = {
from?: number;
to?: number;
};

type MeasureSpringProps = {
fps: number;
config?: Partial<SpringConfig>;
threshold?: number;
} & (false extends typeof ENABLE_V5_BREAKING_CHANGES ? V4Props : {});

/**
* @description The function returns how long it takes for a spring animation to settle
* @see [Documentation](https://www.remotion.dev/docs/measure-spring)
Expand All @@ -12,15 +24,7 @@ export function measureSpring({
fps,
config = {},
threshold = 0.005,
from = 0,
to = 1,
}: {
fps: number;
config?: Partial<SpringConfig>;
threshold?: number;
from?: number;
to?: number;
}): number {
}: MeasureSpringProps): number {
if (typeof threshold !== 'number') {
throw new TypeError(
`threshold must be a number, got ${threshold} of type ${typeof threshold}`,
Expand Down Expand Up @@ -53,8 +57,6 @@ export function measureSpring({
config.mass,
config.overshootClamping,
config.stiffness,
from,
to,
threshold,
].join('-');
if (cache.has(cacheKey)) {
Expand All @@ -63,7 +65,6 @@ export function measureSpring({

validateFps(fps, 'to the measureSpring() function', false);

const range = Math.abs(from - to);
let frame = 0;
let finishedFrame = 0;
const calc = () => {
Expand All @@ -76,10 +77,7 @@ export function measureSpring({

let animation = calc();
const calcDifference = () => {
return (
Math.abs(animation.current - animation.toValue) /
(range === 0 ? 1 : range)
);
return Math.abs(animation.current - animation.toValue);
};

let difference = calcDifference();
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/test/spring.test.ts
Expand Up @@ -223,3 +223,17 @@ test('spring should not end too early', () => {
}),
).toBeCloseTo(1);
});

test('from / to', () => {
expect(
spring({
fps: 30,
frame: 10,
from: 0,
to: 200,
config: {
damping: 200,
},
}),
).toBeCloseTo(169.082);
});

0 comments on commit 3338706

Please sign in to comment.