I want a constant requestAnimationFrame
frame rate. So we have this:
var stop = false;
var frameCount = 0;
var fps, fpsInterval, startTime, now, then, elapsed;
startAnimating(30);
function startAnimating(fps) {
fpsInterval = 1000 / fps;
then = window.performance.now();
startTime = then;
console.log(startTime);
animate();
}
function animate(newtime) {
// stop
if (stop) {
return;
}
// request another frame
requestAnimationFrame(animate);
// calc elapsed time since last loop
now = newtime;
elapsed = now - then;
// if enough time has elapsed, draw the next frame
if (elapsed > fpsInterval) {
// Get ready for next frame by setting then=now, but...
// Also, adjust for fpsInterval not being multiple of 16.67
then = now - (elapsed % fpsInterval);
// draw stuff here
// TESTING...Report #seconds since start and achieved fps.
var sinceStart = now - startTime;
var currentFps = Math.round(1000 / (sinceStart / ++frameCount) * 100) / 100;
console.log(currentFps + " fps.");
}
}
But the issue is if you loss your focus on the page and minimize your browser wait seconds and come back to the window again the frame rate drops suddenly. But we want a constant frame rate right?
How do you fix this?