-
Notifications
You must be signed in to change notification settings - Fork 0
/
animate.js
256 lines (218 loc) · 7.07 KB
/
animate.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory(root);
} else if (typeof define === 'function' && define.amd) {
define('animate', [], function () {
return factory(root);
});
} else {
root.Animate = factory(root);
}
}(this, function(window, undefined) {
'use strict';
// TODO: use ids instead of element in buffers; stop,... styles only
var Animate = function(options) {
this.options = {
ease: function(n) {return -(--n*n*n*n-1)}, // --n*n*n+1
speed: 0.25,
animationEndCallback: function(element){},
};
_init(this, options || {});
},
_init = function(_this, options) {
for (var option in options) { // extend options
_this.options[option] = options[option];
}
},
_animate = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame || function(cb) {
window.setTimeout(cb, 16);
},
_buffer = [],
_pauseBuffer = [];
Animate.prototype = {
queue: function(element, styles, speed, cbs, ease) {
return this.animate(element, styles, speed, cbs, ease, true);
},
animate: function(element, styles, speed, cbs, ease, queue) { // id?
var previous = this.stopAnimation(element); // TODO: check
if (previous) {
previous.endCallback.call(this, previous.element);
}
_buffer.push({
element: element,
elementStyle: element.style,
timeIsUp: false,
data: {
startTime: new Date().getTime(),
styles : sizzleCSS(styles.replace(/;\s*$/, '').split(/\s*;\s*/g)),
updateCallbacks: cbs && cbs.updateCallbacks,
speed: (speed !== undefined ? speed : this.options.speed) * 1000,
ease: ease || this.options.ease
},
endCallback: cbs && cbs.endCallback || this.options.animationEndCallback,
});
return queue ? this : triggerRendering(this);
},
getElementState: function(element) {
return (findBufferItem(element, _buffer) || {}).item;
},
pauseAnimation: function(element) { // TODO: use style parameter
var bufferItem = removeItem(element, _buffer);
if (bufferItem) {
_pauseBuffer.push(bufferItem);
}
return triggerRendering(this);
},
resetAnimation: function(element) { // TODO: use style parameter
var bufferItem = removeItem(element, _pauseBuffer);
if (bufferItem) {
bufferItem.data.startTime = // tweak time to continue
new Date().getTime() - bufferItem.elapsedTime;
_buffer.push(bufferItem);
}
return triggerRendering(this);
},
stopAnimation: function(element, _this, endCallback) { // TODO: parameter
var bufferItem = removeItem(element, _buffer);
removeItem(element, _pauseBuffer);
triggerRendering(this);
return bufferItem;
},
destroy: function() {
// TODO
}
};
function removeItem(element, buffer) {
var bufferItem = findBufferItem(element, buffer);
if (bufferItem) {
return buffer.splice(bufferItem.index, 1)[0];
}
}
function triggerRendering(_this) {
if (!_this.isAnimating && _buffer.length) {
_this.isAnimating = true;
render(_this, _buffer);
} else if (!_buffer.length) {
_this.isAnimating = false;
}
return _this;
}
function findBufferItem(element, buffer) {
for (var n = buffer.length; n--; ) {
if (buffer[n].element === element) {
return {
item: buffer[n],
index: n
}
}
}
}
function render(_this, buffer) {
var time = new Date().getTime(),
elapsedTime = 0,
ease = 0;
for (var n = buffer.length; n--; ) { // pre-render
if (buffer[n].timeIsUp) {
buffer.splice(n, 1); // preserve for backwards animation??
_this.isAnimating = !!buffer.length;
continue;
}
elapsedTime = time - buffer[n].data.startTime; // inside preRender?
buffer[n].timeIsUp = elapsedTime >= buffer[n].data.speed;
ease = buffer[n].timeIsUp ? 1 : // TODO: maybe sync with others
buffer[n].data.ease(elapsedTime / buffer[n].data.speed);
buffer[n].preRender = preRender(buffer[n].data, ease);
buffer[n].elapsedTime = elapsedTime;
}
// fastest possible rendering as there is no further calculations
// in between Element.style declarations
for (var n = 0, l = buffer.length; n < l; n++) { // render
for (var style in buffer[n].preRender) _render(buffer[n], style);
}
buffer.length && _animate(function() { // call again
render(_this, buffer);
});
for (var n = 0, l = buffer.length; n < l; n++) { // endCallback
buffer[n].timeIsUp &&
buffer[n].endCallback.call(_this, buffer[n].element);
}
}
function _render(_buffer, style) { // v8 opt.
_buffer.elementStyle[style] = _buffer.preRender[style];
}
function preRender(data, ease) {
var values = [],
css = {}; // String is fater than Array.push().join()
for (var style in data.styles) _preRender(style, data, ease, values, css);
return css;
}
function _preRender(style, data, ease, values, css) { // v8 opt.
values = data.styles[style];
css[style] = '';
for (var n = 0, len = values.length; n < len; n++) {
if (typeof values[n] === 'string') {
css[style] += values[n];
// delete data.styles[style]; // only once; faster
} else {
if(values[n].hasCB) {
values[n].delta =
data.updateCallbacks[values[n].hasCB]() - values[n].start;
}
css[style] += round(values[n].start + values[n].delta * ease,
values[n].doRound) + values[n].unit;
}
}
}
function sizzleCSS(styles) {
var style = '',
output = {},
fragments = [],
css = [];
for (var n = styles.length; n--; ) {
fragments = [];
style = styles[n]
.replace(/\s*(\)|\(|,|:|->|=>)\s*/g, '$1')
.split(':');
css = style[1].replace(/([.0-9]+[^,\s)]*(?:-|=)>.*?)([\s,)])/g,
function($1, $2, $3) {
fragments.push(extractValues($2));
return '||' + $3;
}).split('||');
if (css.length === 1) {
fragments.push(extractValues(css[0]));
} else {
for (var x = 0, xl = css.length; x < xl; x++) {
fragments.splice(x * 2, 0, css[x])
}
}
output[style[0].replace(/-(.)/g, function($1, $2) {
return $2.toUpperCase();
})] = fragments;
}
return output;
}
function extractValues(value) {
var doRound = value.indexOf('=>') !== -1,
val = value.split(/(?:-|=)>/),
unit = val[0].replace(/[0-9.-]/g, ''),
hasCB = false;
if (!val[1]) {
return value;
}
hasCB = val[1].split('fn|')[1];
val[0] = +val[0] || parseInt(val[0]);
val[1] = +val[1] || parseInt(val[1]);
return {
start: val[0],
delta: val[1] >= 0 || val[1] < 0 ? val[1] - val[0] : 0,
unit: unit,
doRound: doRound,
hasCB: hasCB,
};
}
function round(value, doRound) {
return doRound ? Math.round(value) : value;
}
return Animate;
}));