JS: GameLoop
fps controlling:
http://codetheory.in/controlling-the-frame-rate-with-requestanimationframe/
fps calculation:
http://stackoverflow.com/a/8280201/1250044
helper:
http://buildnewgames.com/sprite-animation/
JavaScript
var GameLoop = function(fn, fps){
fps = fps || 60;
var now;
var delta;
var interval;
var then = new Date().getTime();
var frames;
var oldtime = 0;
return (function loop(time){
requestAnimationFrame(loop);
interval = 1000/(this.fps||fps);
now = new Date().getTime();
delta = now - then;
if (delta > interval) {
// update time stuffs
then = now - (delta % interval);
// calculate the frames per second
frames = 1000/(time-oldtime)
oldtime = time;
// call the fn
// and pass current fps to it
fn(frames);
}
}(0));
};
var set;
document.onclick = function(){
set = true;
};
GameLoop(function(fps){
if(set) this.fps = 10;
console.log(fps);
}, 5);