How to solve different FPS in requestAnimationFrame
on different browsers?
I am making a 3D game using THREE.js
that uses requestAnimationFrame
and it is fast on Google Chrome 15.
However, it is really slow on Firefox 6 and really really slow (slower than Firefox) on IE9.
This is really a big problem and I am wondering if there is a solution to that.
Thanks.
Advertisement
Answer
As far as I know there’s no way to really fix this, other than making your code less resource intensive.
Chrome seems to be the fastest browser, but usually FF is not far behind, but IE is still slow. Depending on the rendering methods, canvas, svg or webGL, it’s also very dependent on your local hardware as it uses the clientside for most things, and complicated webGL renderings need a powerful GPU to achieve good framerates.
There are ways to measure the framerate on the fly, and change your animations accordingly.
Here’s a very simple example that measures framerate.
function step(timestamp) { var time2 = new Date; var fps = 1000 / (time2 - time); time = time2; document.getElementById('test').innerHTML = fps; window.requestAnimationFrame(step); } var time = new Date(), i = 0; window.requestAnimationFrame(step);
<div id="test"></div>
This is just an instant measure that’s not that accurate, you’d probably want something that measures over some time to get a more correct framerate for the browser being used.
Also note the timestamp
argument, which in requestAnimationFrame
is high-res timestamp with a minimal precision of 1 milliseconds, that can also be used to deterine the speed of the animation, and any browser lag.