//http://www.gamedev.net/topic/579738-web-javascript-game-loop/
// -- CREDIT http://www.gamedev.net/user/107433-bunzaga/ bunzaga
// -- requestAnimationFrame http://paulirish.com/2011/requestanimationframe-for-smart-animating/
//http://dev.opera.com/articles/view/framerate-control-system-for-javascript/
//http://jsfiddle.net/digitalicarus/4kshN/3/
var Wee = (function() {
var FPS= 60,
rate= 60,
dt= 1/60,
accrued= 0.0,
last= Date.now()/1000,
fpsDisplay= $('<div id="fpsDisplay"/>').appendTo('body')[0],
frameCounter= 0,
droppedFrames= 0,
interval= null,
paused= false,
intervalRate= Math.floor(1000/FPS) - 1,
ciCallback= function(){},
upCallback= function(){},
rdCallback= function(){},
pauseCallback= function(){};
// Use this callback to latch your inputs if you want
// Kinda not useful cause you could latch em when you get the events
//var CheckInput= function() {
// ciCallback();
//};
// Updates can happen more frequently than render. This call back
// will fire very rapidly. Don't waste much time in here at all!
// You can use this for collision detection etc.
// Just check if you have already updated this render cycle and bail ASAP.
var Update= function() {
frameCounter++;
droppedFrames++;
upCallback();
};
// This is the one you really want. Draw the result of your state in here.
var Render= function() {
rate = (droppedFrames > 0) ?
((rate+(FPS/(droppedFrames*2)))/2):
((rate + FPS)/2);
// just render every N frames to avoid a jarring display
fpsDisplay.innerHTML = parseInt(rate,10)+" FPS";
droppedFrames = -1;
rdCallback();
};
var GameLoop= function() {
var now =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.