File size: 860 Bytes
9425aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const easing = {
  linear: value => value,
  smooth: value => value * value * (3 - 2 * value),
  outCubic: value => 1 - (1 - value) ** 3,
};

export class AnimationSystem {
  constructor() {
    this.active = new Set();
  }

  to({ duration = 0.4, ease = "smooth", update, complete }) {
    const animation = { elapsed: 0, duration, ease: easing[ease] || easing.smooth, update, complete };
    this.active.add(animation);
    update(0);
    return () => this.active.delete(animation);
  }

  update(delta) {
    for (const animation of this.active) {
      animation.elapsed += delta;
      const progress = Math.min(1, animation.elapsed / animation.duration);
      animation.update(animation.ease(progress));
      if (progress === 1) {
        this.active.delete(animation);
        animation.complete?.();
      }
    }
  }
}