1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- /* creates an animation of a number running either from 0 to max
- * or from max to 0
- */
- class Animation {
- /* by default the animation rests at 0 and is not running */
- constructor(max) {
- /* shows where the animation currently is */
- this.timer = 0;
- /* animation's max value */
- this.max = max;
- /* shows if the animation is moving positively (from 0 to max)
- * or negatively (from max to 0)
- * this also helps determine if the animation is resting or running
- */
- this.positive = false;
- } /* constructor */
- /* if animation is running, move one step
- * returns true if animation moved to a new value
- * returns false if nothing changed
- */
- update() {
- /* animation is running */
- if (this.is_running()) {
- /* advance animation (either positively or negatively) */
- this.timer += this.positive ? 1 : -1;
- return true;
- } /* animation running */
- /* animation is not running */
- return false;
- } /* update */
- /* returns true if animation is currently running
- * (either positively or negatively)
- * returns false otherwise
- */
- is_running() {
- return (this.timer < this.max && this.positive)
- || (this.timer > 0 && !this.positive);
- } /* is running */
- /* change animation's direction between positive/negative */
- flip() { this.positive = !this.positive; };
- /* returns the interpolated value of distance based
- * on animation's timer and max value
- * returns 0 when animation is on starting position
- * 0.5 when animation is half-way done
- * 1.0 when animation is at the end value
- */
- interpolate(distance) {
- return distance *this.timer /this.max;
- }
- /* flips the animation, but only if it is not running */
- start() {
- if (!this.is_running()) {
- this.flip();
- }
- } /* start */
- /* change maximum value */
- set_max(max) {
- /* change value */
- this.max = max;
- /* make sure pointer is not pointing outside of range
- * if max would cause the animation to stop
- * move it one step below it, so the animation updates
- * one last time
- */
- if (this.timer > max) {
- this.timer = max -1;
- }
- } /* set max */
- } /* class Animation */
|