forked from mozilla/BrowserQuest
-
Notifications
You must be signed in to change notification settings - Fork 219
/
transition.js
65 lines (54 loc) · 2 KB
/
transition.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
define(function() {
var Transition = Class.extend({
init: function() {
this.startValue = 0;
this.endValue = 0;
this.duration = 0;
this.inProgress = false;
},
start: function(currentTime, updateFunction, stopFunction, startValue, endValue, duration) {
this.startTime = currentTime;
this.updateFunction = updateFunction;
this.stopFunction = stopFunction;
this.startValue = startValue;
this.endValue = endValue;
this.duration = duration;
this.inProgress = true;
this.count = 0;
},
step: function(currentTime) {
if(this.inProgress) {
if(this.count > 0) {
this.count -= 1;
log.debug(currentTime + ": jumped frame");
}
else {
var elapsed = currentTime - this.startTime;
if(elapsed > this.duration) {
elapsed = this.duration;
}
var diff = this.endValue - this.startValue;
var i = this.startValue + ((diff / this.duration) * elapsed);
i = Math.round(i);
if(elapsed === this.duration || i === this.endValue) {
this.stop();
if(this.stopFunction) {
this.stopFunction();
}
}
else if(this.updateFunction) {
this.updateFunction(i);
}
}
}
},
restart: function(currentTime, startValue, endValue) {
this.start(currentTime, this.updateFunction, this.stopFunction, startValue, endValue, this.duration);
this.step(currentTime);
},
stop: function() {
this.inProgress = false;
}
});
return Transition;
});