-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrack-progress.js
48 lines (39 loc) · 1.3 KB
/
track-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
const trackProgressThroughEffect = (animation, cb, precision) => {
let progress = null;
const updateValue = () => {
if (animation && animation.currentTime) {
let newProgress = animation.effect.getComputedTiming().progress * 1;
if (animation.playState === 'finished') newProgress = 1;
newProgress = Math.max(0.0, Math.min(1.0, newProgress)).toFixed(precision);
if (newProgress != progress) {
progress = newProgress;
cb(progress);
}
}
requestAnimationFrame(updateValue);
};
requestAnimationFrame(updateValue);
};
// Note: once we have a `progress` event on an Animation, we can drop the rAF
const trackProgressThroughAnimation = (animation, cb, precision) => {
let progress = null;
const updateValue = () => {
if (animation && animation.currentTime) {
let newProgress = animation.overallProgress.toFixed(precision);
if (newProgress != progress) {
progress = newProgress;
cb(progress);
}
}
requestAnimationFrame(updateValue);
};
requestAnimationFrame(updateValue);
};
const trackProgress = (animation, cb, precision = 5) => {
if (("Animation" in globalThis && "overallProgress" in Animation.prototype)) {
trackProgressThroughAnimation(animation, cb, precision);
} else {
trackProgressThroughEffect(animation, cb, precision);
}
};
export default trackProgress;