-
Notifications
You must be signed in to change notification settings - Fork 0
/
tick.js
113 lines (96 loc) · 2.75 KB
/
tick.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
/* Tick.js
* Source: https://github.com/makesites/tick
* Copyright © Makesites.org
*/
/*
* Initiated by Makis Tracend (@tracend)
* Distributed through [Makesites.org](http://makesites.org)
* Released under the [MIT license](http://makesites.org/licenses/MIT)
*/
window.Tick = window.Tick || (function( window ){
var Tick = function( options ){
// merge options
options = options || {};
if(options.rate) this.options.rate = options.rate;
// setup animation rate
this.rate();
// start loop
this.loop();
};
Tick.prototype = {
options: {
rate: 1000 / 60 // standard 60fps
},
queue: [],
rate: function( rate ){
rate = rate || this.options.rate;
// RequestAnimationFrame shim - Source: http://paulirish.com/2011/requestanimationframe-for-smart-animating/
window.requestAnimFrame = window.requestAnimFrame || ( function( callback ) {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function ( callback ) {
window.setTimeout( function () {
callback(+new Date());
}, rate );
};
})();
},
loop: function( timestamp ){
// feature-detect if rAF and now() are of the same scale (epoch or high-res),
// if not, we have to do a timestamp fix on each frame
//Source: https://github.com/ded/morpheus/blob/master/src/morpheus.js#L91
//if (fixTs) timestamp = now();
this.process( timestamp );
// reset loop
window.requestAnimFrame(this.loop.bind(this)); // bind only works in > ES5
},
process: function( timestamp ){
// loop through queue
for( var i in this.queue ){
var item = this.queue[i];
// prerequisite
if( typeof item.fn !== "function") continue;
// restrict execution if not time yet
var step = (timestamp % item.interval);
//if( step === 0 || item.run + item.interval > timestamp) continue;
var asc = (step > this.queue[i].step);
this.queue[i].step = step; // store step
if( asc ) continue; // still ascending...
// run
item.fn(); // context?
// condition in case the item was released in the meantime...
// record last run
if( this.queue[i] ) this.queue[i].run = timestamp;
}
},
// interface
add: function( fn, interval ){
// prerequisite
if( typeof fn !== "function" ) return;
// fallback
interval = interval || this.options.rate;
var item = {
fn: fn,
interval: interval,
run: 0
};
this.queue.push( item );
},
remove: function( fn ){
var exists = false;
for( var i in this.queue ){
var item = this.queue[i];
if( String(item.fn) === String(fn) ){
exists = true;
delete this.queue[i];
}
}
return exists;
}
};
// return to save in the global namespace
return Tick;
})( this.window );