UNPKG

2.05 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.measureSpring = void 0;
4const validate_fps_1 = require("../validation/validate-fps");
5const spring_utils_1 = require("./spring-utils");
6function measureSpring({ fps, config = {}, threshold = 0.005, from = 0, to = 1, }) {
7 if (typeof threshold !== 'number') {
8 throw new TypeError(`threshold must be a number, got ${threshold} of type ${typeof threshold}`);
9 }
10 if (threshold === 0) {
11 return Infinity;
12 }
13 if (threshold === 1) {
14 return 0;
15 }
16 if (isNaN(threshold)) {
17 throw new TypeError('Threshold is NaN');
18 }
19 if (!Number.isFinite(threshold)) {
20 throw new TypeError('Threshold is not finite');
21 }
22 if (threshold < 0) {
23 throw new TypeError('Threshold is below 0');
24 }
25 (0, validate_fps_1.validateFps)(fps, 'to the measureSpring() function');
26 const range = Math.abs(from - to);
27 let frame = 0;
28 let finishedFrame = 0;
29 const calc = () => {
30 return (0, spring_utils_1.springCalculation)({
31 fps,
32 frame,
33 config,
34 from,
35 to,
36 });
37 };
38 let animation = calc();
39 const calcDifference = () => {
40 return (Math.abs(animation.current - animation.toValue) /
41 (range === 0 ? 1 : range));
42 };
43 let difference = calcDifference();
44 while (difference >= threshold) {
45 frame++;
46 animation = calc();
47 difference = calcDifference();
48 }
49 // Since spring is bouncy, just because it's under the threshold we
50 // cannot be sure it's done. We need to animate further until it stays in the
51 // threshold for, say, 20 frames.
52 finishedFrame = frame;
53 for (let i = 0; i < 20; i++) {
54 frame++;
55 animation = calc();
56 difference = calcDifference();
57 if (difference >= threshold) {
58 i = 0;
59 finishedFrame = frame + 1;
60 }
61 }
62 return finishedFrame;
63}
64exports.measureSpring = measureSpring;