Controlling Fps with Requestanimationframe

Controlling fps with requestAnimationFrame?

How to throttle requestAnimationFrame to a specific frame rate

Demo throttling at 5 FPS: http://jsfiddle.net/m1erickson/CtsY3/

This method works by testing the elapsed time since executing the last frame loop.

Your drawing code executes only when your specified FPS interval has elapsed.

The first part of the code sets some variables used to calculate elapsed time.

var stop = false;
var frameCount = 0;
var $results = $("#results");
var fps, fpsInterval, startTime, now, then, elapsed;

// initialize the timer variables and start the animation

function startAnimating(fps) {
fpsInterval = 1000 / fps;
then = Date.now();
startTime = then;
animate();
}

And this code is the actual requestAnimationFrame loop which draws at your specified FPS.

// the animation loop calculates time elapsed since the last loop
// and only draws if your specified fps interval is achieved

function animate() {

// request another frame

requestAnimationFrame(animate);

// calc elapsed time since last loop

now = Date.now();
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 your
// specified fpsInterval not being a multiple of RAF's interval (16.7ms)
then = now - (elapsed % fpsInterval);

// Put your drawing code here

}
}

How to lock FPS with requestAnimationFrame?

Something like this should work. If the time delta between two frames is shorter than your FPS limit, the update function returns and waits for the next frame. But this will only limit the updates from happening too quickly; like emackey said, there's always the possibility the update loop will run more slowly.

var updateId,
previousDelta = 0,
fpsLimit = 30;

function update(currentDelta) {
updateId = requestAnimationFrame(update);

var delta = currentDelta - previousDelta;

if (fpsLimit && delta < 1000 / fpsLimit) {
return;
}

/* your code here */

previousDelta = currentDelta;
}

How to control fps with requestAnimationFrame?

You want to control the duration of your AnimationAction loop. To do so, you can use this pattern:

action.setDuration( 10 ).play();

three.js r.87

requestAnimationFrame with higher rate than 60 fps

Exactly true.

Here is a simple example to measure:

let i = 0;
const start = Date.now();
const stop = start + 5000;

function raf() {
requestAnimationFrame(() => {
const now = Date.now();
if (now < stop){
i++;
raf();
}else{
const elapsedSeconds = (now - start) / 1000;
console.log('Frame rate is: %f fps', i / elapsedSeconds);
}
});
}

console.log('Testing frame rate...')
raf();

Shall the requestAnimationFrame calls always be throttled down to 60 FPS?

Per MDN it will not necessarily always be 60 FPS.

Relevant quote:

This will request that your animation function be called before the browser performs the next repaint. The number of callbacks is usually 60 times per second, but will generally match the display refresh rate in most web browsers as per W3C recommendation. The callback rate may be reduced to a lower rate when running in background tabs.

As for can the human eye distinguish 60 vs 120 FPS, well, that's an open, and opinionated question. Some claim to see it, other's claim its impossible. Allowing the end user to choose is (or simply using their hardware to its fullest) probably best.

As markE's comment pointed out. The requestAnimationFrame callback receives a DOMHighResTimeStamp which is a high precision timer accurate to the "thousandth of a millisecond." By using this time-stamp, and calculating the delta between frames, you can tune your refresh rate to whatever desired value.

References:

  • requestAnimationFrame
  • W3C Timing control for script-based animations
  • DOMHighResTimeStamp

Note: please remember to leave a comment addressing any downvotes, otherwise they are not useful feedback.

requestAnimationFrame at a limited framerate

Yoshi's answer is probably the best code solution to this problem. But still I'll mark this answer as correct, because after some research I basically found that my question was invalid. requestAnimationFrame is really meant to keep frame rates as high as possible, and it optimizes for scenarios where animation is meant to be kept consistent and smooth.

Worth noting though is that you don't need requestAnimationFrame to get optimization (even though rAF was touted as a great performance booster) since browsers still optimize regular drawing of a <canvas>. For example, when a tab isn't focused, Chrome for one stops drawing its canvases.

So my conclusion was that this question was invalid. Hope this helps anyone who was wondering something similar to me.



Related Topics



Leave a reply



Submit