animatable-js
Version:
This package allows easy and light implementation of linear or curved animation in javascript. (Especially suitable in a development environment on web components or canvas.)
57 lines (50 loc) • 2.39 kB
text/typescript
import { Animatable, AnimationStatus } from "./animatable";
import { AnimationListener, AnimationStatusListener } from "./types";
/**
* This abstract class implements the animation related listener that
* is called when the current animation value and status changed.
*/
export abstract class AnimationListenable extends Animatable {
private listeners: AnimationListener[] = [];
private statusListeners: AnimationStatusListener[] = [];
/**
* Registers the given listener that is called when the current
* animation value changed in this animation instance.
*/
addListener(listener: AnimationListener) {
console.assert(!this.listeners.includes(listener), "Already a given listener does exist.");
this.listeners.push(listener);
}
/**
* Deregisters the given listener that is called when the current
* animation value changed in this animation instance.
*/
removeListener(listener: AnimationListener) {
console.assert(this.listeners.includes(listener), "Already a given listener does not exist.");
this.listeners = this.listeners.filter(l => l != listener);
}
/**
* Registers the given status listener that is called when the current
* animation status value changed in this animation instance.
*/
addStatusListener(listener: AnimationStatusListener) {
console.assert(!this.statusListeners.includes(listener), "Already a given status listener does exist.");
this.statusListeners.push(listener);
}
/**
* Deregisters the given status listener that is called when the current
* animation status value changed in this animation instance.
*/
removeStatusListener(listener: AnimationStatusListener) {
console.assert(this.statusListeners.includes(listener), "Already a given status listener does not exist.");
this.statusListeners = this.statusListeners.filter(l => l != listener);
};
/** Notifies a new value updated for a registered animation listeners. */
notifyListeners(value: number) {
this.listeners.forEach(l => l(value));
}
/** Notifies a new status updated for a registered animation status listeners. */
notifyStatusListeners(status: AnimationStatus) {
this.statusListeners.forEach(l => l(status));
}
}