forked from brunosimon/burno.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ticker.class.js
304 lines (254 loc) · 7.59 KB
/
ticker.class.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
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
300
301
302
303
304
/**
* @class Resizer
* @author Bruno SIMON / http://bruno-simon.com
*/
B.Tools.Ticker = B.Core.Event_Emitter.extend(
{
static : 'ticker',
options :
{
auto_run : true
},
/**
* Initialise and merge options
* @constructor
* @param {object} options Properties to merge with defaults
*/
construct : function( options )
{
this._super( options );
this.reseted = false;
this.running = false;
this.time = {};
this.time.start = 0;
this.time.elapsed = 0;
this.time.delta = 0;
this.time.current = 0;
this.waits = {};
this.waits.before = [];
this.waits.after = [];
this.intervals = {};
if( this.options.auto_run )
this.run();
},
/**
* Reset the ticker by setting time infos to 0
* @param {boolean} run Start the ticker
* @return {object} Context
*/
reset : function( run )
{
this.reseted = true;
this.time.start = + ( new Date() );
this.time.current = this.time.start;
this.time.elapsed = 0;
this.time.delta = 0;
if( run )
this.run();
return this;
},
/**
* Run the ticker
* @return {object} Context
*/
run : function()
{
var that = this;
// Already running
if( this.running )
return;
this.running = true;
var loop = function()
{
if(that.running)
window.requestAnimationFrame( loop );
that.tick();
};
loop();
return this;
},
/**
* Stop ticking
* @return {object} Context
*/
stop : function()
{
this.running = false;
return this;
},
/**
* Tick (or is it tack ?)
* @return {object} Context
*/
tick : function()
{
// Reset if needed
if( !this.reseted )
this.reset();
// Set time infos
this.time.current = + ( new Date() );
this.time.delta = this.time.current - this.time.start - this.time.elapsed;
this.time.elapsed = this.time.current - this.time.start;
var i = 0,
len = this.waits.before.length,
wait = null;
// Do next (before trigger)
for( ; i < len; i++ )
{
// Set up
wait = this.waits.before[ i ];
// Frame count down to 0
if( --wait.frames_count === 0 )
{
// Apply action
wait.action.apply( this, [ this.time ] );
// Remove from actions
this.waits.before.splice( i, 1 );
// Update loop indexes
i--;
len--;
}
}
// Trigger
this.trigger( 'tick', [ this.time ] );
// Trigger intervals
this.trigger_intervals();
// Do next (after trigger)
i = 0;
len = this.waits.after.length;
for( ; i < len; i++ )
{
// Set up
wait = this.waits.after[ i ];
// Frame count down to 0
if( --wait.frames_count === 0 )
{
// Apply action
wait.action.apply( this, [ this.time ] );
// Remove from actions
this.waits.after.splice( i, 1 );
// Update loop indexes
i--;
len--;
}
}
return this;
},
/**
* Apply function on X frames
* @param {number} frames_count How many frames before applying the function
* @param {function} action Function to apply
* @param {boolean} after Should apply the function after the 'tick' event is triggered
* @return {object} Context
*/
wait : function( frames_count, action, after )
{
// Errors
if( typeof action !== 'function' )
return false;
if( typeof frames_count !== 'number' )
return false;
this.waits[ after ? 'after' : 'before' ].push( {
frames_count : frames_count,
action : action
} );
return this;
},
/**
* Create interval
* @param {integer} interval Milliseconds between each tick
* @return {object} Context
*/
create_interval : function( interval )
{
this.intervals[ interval ] = {
interval : interval,
next_trigger : interval,
start : this.time.elapsed,
last_trigger : this.time.elapsed,
};
return this;
},
/**
* Destroy interval
* @param {integer} interval Milliseconds between each tick
* @return {object} Context
*/
destroy_interval : function( interval )
{
delete this.intervals[ interval ];
return this;
},
/**
* Trigger intervals
* @return {object} Context
*/
trigger_intervals : function()
{
// Each interval
for( var _key in this.intervals )
{
var interval = this.intervals[ _key ];
// Test if interval should trigger
if( this.time.elapsed - interval.last_trigger > interval.next_trigger )
{
// Update next trigger to stay as close as possible to the interval
interval.next_trigger = interval.interval - ( this.time.elapsed - interval.start ) % interval.interval;
interval.last_trigger = this.time.elapsed;
this.trigger( 'tick-' + interval.interval, [ this.time, interval ] );
}
}
return this;
},
/**
* Start listening specified events
* @param {string} names Events names (can contain namespace)
* @param {function} callback Function to apply if events are triggered
* @return {object} Context
*/
on : function( names, callback )
{
// Set up
var that = this,
resolved_names = this.resolve_names( names );
// Each resolved name
resolved_names.forEach( function( name )
{
// Has interval interval
if( name.match( /^tick([0-9]+)$/) )
{
// Extract interval interval
var interval = parseInt( name.replace( /^tick([0-9]+)$/, '$1' ) );
// Create interval
if( interval )
that.create_interval( interval );
}
} );
return this._super( names, callback );
},
/**
* Stop listening specified events
* @param {string} names Events names (can contain namespace or be the namespace only)
* @return {object} Context
*/
off : function( names )
{
// Set up
var that = this,
resolved_names = this.resolve_names( names );
// Each resolved name
resolved_names.forEach( function( name )
{
// Has interval interval
if( name.match( /^tick([0-9]+)$/) )
{
// Extract interval interval
var interval = parseInt( name.replace( /^tick([0-9]+)$/, '$1' ) );
// Create interval
if( interval )
that.destroy_interval( interval );
}
} );
return this._super( names );
},
} );