|
4 | 4 | var $ = jQuery.noConflict(true),
|
5 | 5 | Events = Broadcast.noConflict(),
|
6 | 6 | jungle = $('#jungle'),
|
| 7 | + jungleEl = jungle[0], |
7 | 8 | creatures = {}, jj = {},
|
8 | 9 |
|
9 | 10 | // File that contains a list of urls, passed to jj.load
|
10 | 11 | //CREATURE_URL_LIST = 'http://jsbin.com/uxukok/latest',
|
11 | 12 | CREATURE_URL_LIST = false,
|
12 | 13 |
|
13 | 14 | // The number of frames/second that the "tick" event will fire.
|
14 |
| - FRAMERATE = 25; |
| 15 | + FRAMERATE = 25, |
| 16 | + |
| 17 | + // If a frame is rendered more than this number of milliseconds after the previous one, then ditch the frame |
| 18 | + FRAME_CUTOFF = 100; |
15 | 19 |
|
16 | 20 | /////
|
17 | 21 |
|
18 | 22 | // Create the global jungle object.
|
19 |
| - window.jj = jj = $.extend({}, Events, { |
20 |
| - fps: FRAMERATE, |
21 |
| - |
| 23 | + window.jj = jj = $.extend({}, Events, { |
22 | 24 | // jQuery object
|
23 | 25 | jQuery: $,
|
24 | 26 |
|
|
112 | 114 |
|
113 | 115 | isRunning: false,
|
114 | 116 |
|
115 |
| - _tickRef: null, |
| 117 | + _tickRef: null, |
| 118 | + |
| 119 | + // Derived from https://gist.github.com/1114293#file_anim_loop_x.js |
| 120 | + // See http://hacks.mozilla.org/2011/08/animating-with-javascript-from-setinterval-to-requestanimationframe/ |
116 | 121 | _tick: (function(){
|
117 |
| - var frame = 0; |
118 |
| - |
119 |
| - return function(){ |
120 |
| - jj.trigger("tick", frame); |
| 122 | + // feature testing |
| 123 | + var raf = window.mozRequestAnimationFrame || |
| 124 | + window.webkitRequestAnimationFrame || |
| 125 | + window.msRequestAnimationFrame || |
| 126 | + window.oRequestAnimationFrame; |
121 | 127 |
|
122 |
| - frame = (frame !== jj.fps) ? frame + 1 : 0; |
123 |
| - // Using setTimeout instead of setInterval to allow in-runtime fps-bending |
124 |
| - jj._tickRef = window.setTimeout(jj._tick, 1000 / jj.fps); |
125 |
| - }; |
126 |
| - }()), |
| 128 | + function animLoop(){ |
| 129 | + var lastFrame = +new Date; |
| 130 | + |
| 131 | + function loop(now) { |
| 132 | + var deltaT; |
| 133 | + |
| 134 | + if (jj.isRunning){ |
| 135 | + raf ? |
| 136 | + raf(loop, jungleEl) : |
| 137 | + // fallback to setTimeout |
| 138 | + jj._tickRef = window.setTimeout(loop, 1000 / FRAMERATE); |
| 139 | + |
| 140 | + // Make sure to use a valid time, since: |
| 141 | + // - Chrome 10 doesn't return it at all |
| 142 | + // - setTimeout returns the actual timeout |
| 143 | + now = now && now > 1E4 ? now : +new Date; |
| 144 | + deltaT = now - lastFrame; |
| 145 | + |
| 146 | + lastFrame = now; |
| 147 | + //jj.fps = 1000 / deltaT; |
| 148 | + |
| 149 | + // do not render frame when deltaT is too high |
| 150 | + if (deltaT < FRAME_CUTOFF) { |
| 151 | + jj.trigger("tick", deltaT, now); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + loop(); |
| 157 | + } |
| 158 | + |
| 159 | + return animLoop; |
| 160 | + }()), |
| 161 | + |
| 162 | + // Usage |
| 163 | + /* animLoop(function( deltaT, now ) { |
| 164 | + // rendering code goes here |
| 165 | + // return false; will stop the loop |
| 166 | + ... |
| 167 | + // optional 2nd arg: elem containing the animation |
| 168 | + }, animWrapper ); |
| 169 | + */ |
127 | 170 |
|
128 | 171 | start: function(){
|
129 | 172 | this.isRunning = true;
|
|
0 commit comments