-
Notifications
You must be signed in to change notification settings - Fork 0
/
transitionable.js
140 lines (124 loc) · 4.29 KB
/
transitionable.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
136
137
138
139
140
(function () {
'use strict';
var transitionables = [];
var Transitionable = function (startValue, easing) {
this.startValue = startValue;
this.active = false;
this.easing = easing || 'linear';
transitionables.push(this);
}
Transitionable.prototype.to = function (options) {
if (typeof options.startValue !== 'undefined') {
this.startValue = options.startValue;
}
this.endValue = options.endValue;
this.period = options.period;
this.startTime = (new Date()).getTime();
this.currentValue = this.startValue;
this.active = true;
this.stepCallback = options.stepCallback || function () {
};
this.endCallback = options.endCallback || function () {
};
_calculateIncrement.call(this);
return this;
};
function _calculateIncrement () {
var diff = this.endValue - this.startValue;
this.step = diff / EasingFunctions[this.easing](this.period);
}
function _easingWrapper (t) {
return this.step * EasingFunctions[this.easing](t);
}
function _calculateDiffTime () {
this.diffTime = (new Date()).getTime() - this.startTime;
}
Transitionable.prototype.increment = function () {
var result;
result = this.startValue + _easingWrapper.call(this, this.diffTime);
return result;
};
Transitionable.prototype.get = function () {
return this.currentValue;
};
Transitionable.prototype.render = function () {
var oldValue = this.currentValue;
_calculateDiffTime.call(this);
if (this.diffTime < this.period) {
this.currentValue = this.increment();
this.stepCallback(this.currentValue, oldValue);
} else {
this.currentValue = this.endValue;
this.active = false;
this.startValue = this.endValue;
this.endCallback(this.currentValue, oldValue);
}
};
Transitionable.update = function () {
var i;
for (i = 0; i < transitionables.length; i++) {
if (transitionables[i].active === true) {
transitionables[i].render();
}
}
};
var EasingFunctions = {
// no easing, no acceleration
linear: function (t) {
return t;
},
// accelerating from zero velocity
easeInQuad: function (t) {
return t * t;
},
// decelerating to zero velocity
easeOutQuad: function (t) {
return t * (2 - t);
},
// acceleration until halfway, then deceleration
easeInOutQuad: function (t) {
return t < .5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
},
// accelerating from zero velocity
easeInCubic: function (t) {
return t * t * t;
},
// decelerating to zero velocity
easeOutCubic: function (t) {
return (--t) * t * t + 1;
},
// acceleration until halfway, then deceleration
easeInOutCubic: function (t) {
return t < .5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
},
// accelerating from zero velocity
easeInQuart: function (t) {
return t * t * t * t;
},
// decelerating to zero velocity
easeOutQuart: function (t) {
return 1 - (--t) * t * t * t;
},
// acceleration until halfway, then deceleration
easeInOutQuart: function (t) {
return t < .5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t;
},
// accelerating from zero velocity
easeInQuint: function (t) {
return t * t * t * t * t;
},
// decelerating to zero velocity
easeOutQuint: function (t) {
return 1 + (--t) * t * t * t * t;
},
// acceleration until halfway, then deceleration
easeInOutQuint: function (t) {
return t < .5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t;
}
};
(function animloop () {
requestAnimationFrame(animloop);
Transitionable.update();
})();
window.Transitionable = Transitionable;
})();