UNPKG

motion-dom

Version:
89 lines (87 loc) • 2.51 kB
class GroupAnimation { constructor(animations) { // Bound to accommodate common `return animation.stop` pattern this.stop = () => { for (const animation of this.animations) animation.stop(); }; this.animations = animations.filter(Boolean); } get finished() { return Promise.all(this.animations.map((animation) => animation.finished)); } /** * TODO: Filter out cancelled or stopped animations before returning */ getAll(propName) { return this.animations[0][propName]; } attachTimeline(timeline) { const subscriptions = this.animations.map((animation) => animation.attachTimeline(timeline)); return () => { subscriptions.forEach((cancel, i) => { cancel && cancel(); this.animations[i].stop(); }); }; } get time() { return this.getAll("time"); } /** * Setters and methods name the property directly, rather than through * a shared helper, so each call site stays monomorphic. Sequences * call these on every animation, many times a second. */ set time(time) { for (const animation of this.animations) animation.time = time; } get speed() { return this.getAll("speed"); } set speed(speed) { for (const animation of this.animations) animation.speed = speed; } get state() { return this.getAll("state"); } get startTime() { return this.getAll("startTime"); } get duration() { return getMax(this.animations, "duration"); } get iterationDuration() { return getMax(this.animations, "iterationDuration"); } play() { for (const animation of this.animations) animation.play(); } pause() { for (const animation of this.animations) animation.pause(); } cancel() { for (const animation of this.animations) animation.cancel(); } complete() { for (const animation of this.animations) animation.complete(); } } function getMax(animations, propName) { let max = 0; for (let i = 0; i < animations.length; i++) { const value = animations[i][propName]; if (value !== null && value > max) { max = value; } } return max; } export { GroupAnimation }; //# sourceMappingURL=GroupAnimation.mjs.map