So I'm making a javascript game. And I have this basic gameloop engine.
var left = 0,
speed = documentHeight/300;
var keyState = {};
window.addEventListener('keydown',function(e){
keyState[e.keyCode || e.which] = true;
},true);
window.addEventListener('keyup',function(e){
keyState[e.keyCode || e.which] = false;
},true);
function gameLoop() {
if (keyState[39]) {
left -= speed;
move();
}
if (keyState[37]){
left += speed;
move();
}
function move() {
element.style.left = left + 'px';
}
setTimeout(gameLoop, 20);
}
gameLoop();
I have it all at 100% height so I have speed set relative to the document height. It works just fine on my pc, and it is consistent even when resizing. But when I test it on my iMac, the elements move much slower. I've also tried window.requestAnimationFrame(gameLoop) but same thing happens.
Does this have to do with some screens processing more fps? What would be the best way to achieve a consistent speed on all computers/screens?