-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.js
137 lines (106 loc) · 3.96 KB
/
animation.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
*
* WebGL Core Teaching Framework
* (C)opyright Hartmut Schirmacher, hschirmacher.beuth-hochschule.de
*
* Module: Animation
*
* An Animation object allows to start/stop/resume an animation loop
* in which a user-provided callback function is called.
*
*/
/* requireJS module definition */
define(["util"],
(function(util) {
"use strict";
/*
Animation object allows to start/stop/continue an animation loop
in which a user-provided callback function is called.
The user-provided callback function can accept up to two parameters:
- the total time elapsed since start() of the animation
- the "delta" time elapsed since the last call to the update function
Arguments to the constructor:
- updateFunction: function(totalElapsedTime, timeSinceLastCall)
Methods:
- start() starts the animation and resets the timer
- stop() stops the animation
- resume() resumes the animation after a stop() event.
*/
var Animation = function(updateFunction, startNow) {
// store callback function
this.updateFunction = updateFunction;
// time when the animation was played the first time
this.firstTime = 0;
// the last known time the animation was called
this.lastTime = 0;
// the time the animation was paused
this.waitingTime = 0;
// flag to signal whether the animation is started or stopped
if(startNow) {
this.start();
} else {
this.stop();
};
};
// query animation status
Animation.prototype.isRunning = function() {
return this.isStopped;
};
// start a new animation loop with time = 0
Animation.prototype.start = function() {
this.firstTime = 0;
this.lastTime = 0;
this.waitingTime = 0;
this.isStopped = false;
this.update();
};
// pause / stop the animation
Animation.prototype.stop = function() {
this.isStopped = true;
};
// resume after a pause/stop:
// - the total elapsed time is calculated relative to the
// original start time
// - the time "delta" will start with 0 again
Animation.prototype.resume = function() {
if(this.lastTime == 0) {
// first time? then init firstTime and lastTime
this.firstTime = new Date().getTime();
this.lastTime = new Date().getTime();
} else {
// continuation? then just update the waiting time
var delta = new Date().getTime() - this.lastTime;
this.waitingTime += delta;
this.lastTime += delta;
};
this.isStopped = false;
this.update();
};
// toggle animation on/off
Animation.prototype.toggleAnimation = function() {
if(this.isRunning())
this.stop();
else
this.resume();
};
// update() is called regularly; it calculates the elapsed time
// and then calls the update function provided by the user
Animation.prototype.update = function() {
// only do something if animation is not stopped:
if(this.isStopped) {
return;
};
// calculate elapsed time, remember current time
var timeNow = new Date().getTime();
var totalTime = timeNow - this.firstTime - this.waitingTime;
var timeDelta = timeNow - this.lastTime;
this.lastTime = timeNow;
// register animation callback
var _animation = this; // pass animation object via closure
requestAnimFrame( (function() {_animation.update();}) );
// call the actual update function
this.updateFunction(totalTime, timeDelta);
};
// this module returns only the Animation constructor function
return Animation;
})); // define