forked from kaelzhang/neuron.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.js
More file actions
299 lines (222 loc) · 6.11 KB
/
core.js
File metadata and controls
299 lines (222 loc) · 6.11 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* @param {Array.<Fx>} list list of Fx instances
*/
function loop(list){
var now = + new Date,
len = list.length,
instance;
while(len --){
if (instance = list[len]){
instance._step(now);
}
}
};
/**
* add system clock frequency
* @param {Fx} instance
* @param {number} fps
* @param {boolean=} fps
*/
function addOrRemoveClockTicking(instance, fps, toAdd){
var list = _instances[fps] || (_instances[fps] = []),
timer = _timers[fps],
i = 0,
len = list.length;
if(toAdd){
list.push(instance);
if(!timer){
_timers[fps] = setInterval(function(){
loop(list);
}, Math.round(1000 / fps));
}
}else{
for(; i < len; i ++){
if(list[i] === instance){
list.splice(i, 1);
}
}
if(!list.length && timer){
_timers[fps] = clearInterval(timer);
}
}
};
var
// map: fps -> Array.<Fx instance>
_instances = {},
// map: fps -> timer
_timers = {},
META_LINK_WRAPPER = {
cancel: function(fn){
return function(){
this.cancel();
fn.apply(this, arguments);
};
},
chain: function(fn){
var self = this,
queue = self.__queue,
running;
function next(){
var args = queue.shift();
running = false;
if(args){
running = true;
run(args);
}
};
function run(args){
fn.apply(args);
};
function add(args){
running ? queue.push(args) : run(args);
};
self.on('complete', next);
return function(){
add(arguments);
return self;
};
},
ignore: function(fn){
return function(){
!this._isRunning() && fn.apply(this, arguments);
}
}
},
Fx = NR.Class({
Implements: 'events attrs',
initialize: function(options){
var self = this,
start = self.start;
self.subject = self.subject || self;
self.set(options);
self.frameSkip = self.get('frameSkip');
self.start = META_LINK_WRAPPER[self.get('link')].call(this, start);
},
__queue: [],
_step: function(now){
var self = this;
if (self.frameSkip){
var diff = (this.time != null) ? (now - this.time) : 0, frames = diff / this.frameInterval;
self.time = now;
self.frame += frames;
} else {
self.frame ++;
}
if (self.frame < self.frames){
var progress = self._transition(self.frame / self.frames),
now = self._compute(self.from, self.to, progress);
self._set(now);
self.fire('step', [now]);
} else {
self.frame = self.frames;
self._set(self._compute(self.from, self.to, 1));
self.stop();
}
},
_set: function(now){
return now;
},
_compute: function(from, to, progress){
return Fx.compute(from, to, progress);
},
_isRunning: function(){
var list = _instances[this.get('fps')];
return list && list.indexOf(this) !== -1;
},
start: function(from, to){
var self = this;
self.from = from;
self.to = to;
self.frame = self.frameSkip ? 0 : -1;
self.time = null;
self._transition = self.get('transition');
var frames = self.get('frames'),
fps = self.get('fps'),
duration = self.get('duration');
self.duration = duration;
self.frameInterval = 1000 / fps;
self.frames = frames || Math.round( duration / self.frameInterval);
self.fire('start', self.subject);
addOrRemoveClockTicking(self, fps, true);
return self;
},
stop: function(){
var self = this;
if (self._isRunning()){
self.time = null;
self.__queue.length = 0;
addOrRemoveClockTicking(self, self.get('fps'));
if (self.frames === self.frame){
self.fire('complete', self.subject);
} else {
self.fire('stop', self.subject);
}
}
return self;
},
cancel: function(){
var self = this;
if (self._isRunning()){
self.time = null;
addOrRemoveClockTicking(self, self.get('fps'));
self.frame = self.frames;
self.fire('cancel', self.subject);
}
return self;
},
pause: function(){
var self = this;
if (self._isRunning()){
self.time = null;
addOrRemoveClockTicking(self, self.get('fps'));
}
return self;
},
resume: function(){
var self = this;
self.frame < self.frames && !self._isRunning() && addOrRemoveClockTicking(self, self.get('fps'), true);
return self;
}
}, {
fps: {
writeOnce: true,
value: 60
},
unit: {
value: false
},
duration: {
value: 500
},
frames: {
writeOnce: true,
value: null
},
frameSkip: {
value: true
},
link: {
value: 'ignore'
},
property: {
setter: function(v){
this.property = v;
}
},
transition: {
value: function(p){
return - ( Math.cos( Math.PI * p ) - 1) / 2;
}
}
});
Fx.compute = function(from, to, progress){
return (to - from) * progress + from;
};
module.exports = Fx;
/**
2011-10-09 Kael
- migrate to Neuron
TODO:
A. CSS3 transition support
B. chain support
*/