Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c648f9a

Browse files
committed
jj._tick() loop now uses requestAnimationFrame, in supporting browsers, for smoother animation: tick event callback is now passed two arguments: 1) the deltaT time since the previous frame was rendered, 2) timestamp for the most recent rendered frame
1 parent 04e438b commit c648f9a

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

javascript/jungle.js

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
var $ = jQuery.noConflict(true),
55
Events = Broadcast.noConflict(),
66
jungle = $('#jungle'),
7+
jungleEl = jungle[0],
78
creatures = {}, jj = {},
89

910
// File that contains a list of urls, passed to jj.load
1011
//CREATURE_URL_LIST = 'http://jsbin.com/uxukok/latest',
1112
CREATURE_URL_LIST = false,
1213

1314
// 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;
1519

1620
/////
1721

1822
// Create the global jungle object.
19-
window.jj = jj = $.extend({}, Events, {
20-
fps: FRAMERATE,
21-
23+
window.jj = jj = $.extend({}, Events, {
2224
// jQuery object
2325
jQuery: $,
2426

@@ -112,18 +114,59 @@
112114

113115
isRunning: false,
114116

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/
116121
_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;
121127

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+
*/
127170

128171
start: function(){
129172
this.isRunning = true;

0 commit comments

Comments
 (0)