.
, , , ,
,
,
.
, .
,
:
pos = pos + speed * (time step)
:
var x = 0;
var speed = canvas.width / 1000 ;
var lastTime = 0;
requestAnimationFrame(launchAnimate);
function animate() {
requestAnimationFrame(animate);
var now = Date.now();
var dt = now - lastTime ;
lastTime = now ;
draw();
update(dt);
}
function launchAnimate() {
lastTime = Date.now() ;
requestAnimationFrame(animate);
}
: , , 60 16,666 .
RequestAnimationFrame, : " , , ".
, , .
, :
, , , .
2: RequestAnimationFrame :
http://caniuse.com/requestanimationframe
(Chrome Android ).
Rq:
polyFillRAFNow();
function polyFillRAFNow() {
var w = window;
var foundRequestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame || w.oRequestAnimationFrame || function (cb) { setTimeout(cb, 1000 / 60); };
window.requestAnimationFrame = foundRequestAnimationFrame;
}
Rq2: , http://gamealchemist.wordpress.com/2013/03/16/thoughts-on-the-javascript-game-loop/