-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.js
51 lines (44 loc) · 1.17 KB
/
progress.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* This module captures the a precomputed Progress object.
* Each property of the progress object represents a different curve.
* The progress object avoids repetition of common interpolation functions,
* since in Emoji Quest, every entity moves in tandem after each turn.
*/
// @ts-check
import { clamp } from './lib/math.js';
/**
* @typedef {Object} Progress
* @prop {number} elapsed
* @prop {number} linear
* @prop {number} sinusoidal
* @prop {number} sinusoidalQuarterTurn
* @prop {number} bounce
* @prop {number} enter
* @prop {number} exit
*/
/**
* @callback AnimateFn
* @param {Progress} progress
*/
/**
* @param {number} elapsed
* @param {number} turns
* @returns {Progress}
*/
export function makeProgress(elapsed, turns) {
const linear = clamp(0, 1, turns);
const sinusoidal = (1 - Math.cos(Math.PI * linear)) / 2;
const bounce = (1 - Math.cos(Math.PI * 2 * sinusoidal)) / 16;
const sinusoidalQuarterTurn = (-Math.PI / 2) * sinusoidal;
const enter = Math.max(0, sinusoidal * 2 - 1);
const exit = Math.max(0, 1 - sinusoidal * 2);
return {
elapsed,
linear,
sinusoidal,
sinusoidalQuarterTurn,
bounce,
exit,
enter,
};
}