How to handle the different frame rate on different devices?

Posted by Fenwick on Game Development See other posts from Game Development or by Fenwick
Published on 2014-08-25T04:20:28Z Indexed on 2014/08/25 4:33 UTC
Read the original article Hit count: 208

Filed under:
|
|

I am not quite sure how frame per second works on a web page. I have a Canvas game that involves in moving an image from point A to B, and measuring the time elapsed. The code can be as simple as:

var timeStamp = Date.now();    
function update(){
  obj.y += obj.speed;
  text = "Time: "+ (Date.now() - timeStamp) + "ms";
}

The function update() is called every frame. The problem is that the time elapsed is different from device to device. It is pretty short on my PC, but get longer on my iPad, and is much longer on my cell phone. I thought it is because the FPS is smaller on mobile devices, so instead of calling update() every frame, I call it every 1ms by using a setInterval. But this does not solve the problem.

In my understanding, the function for setInterval is invoked based on the increment in system time, other than frame rate, so it should fix the problem. Am I missing anything here? If the setInterval function is called based on FPS, is there any way to get around with the FPS difference across devices?

On a side note, I have sort of a "water simulator" on the same canvas. It involves in redrawing about 60 objects which can be 600x600 pixels for every frame, so it could be a frame rate killer. I am using Phaser.js but not really using much of its functionalities, if that helps.

© Game Development or respective owner

Related posts about html5

Related posts about frame-rate