question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Limit framerate and Frame-skipping

See original GitHub issue

If I have an animation that I want to play at a certain FPS on the users screen, how would I accomplish this? I’ve seen some people do this:

function render()
{
    setTimeout(draw(), 1000/60);  // 60 fps?
}

Is that the proper way to do it?

What happens if someone’s computer is too slow to handle the geometry in the scene? Is there someway to do frame skipping in order to keep their framerate at the target rate but skipping certain frames in order to stay there?

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:48 (41 by maintainers)

github_iconTop GitHub Comments

1reaction
benbuckschcommented, Nov 24, 2015

So far, I’ve followed the tutorial that suggested to call requestAnimationFrame() and render in a loop.

function render() {
    requestAnimationFrame( render );
    renderer.render( scene, camera );
}
render();

This bug caused my page to hog 1-2 CPU cores forever, even if nothing at all is happening in the scene, as long as my webpage was open, for days. It drains battery, uses electricity needlessly, and spins up fans. Thus, causing noise pollution and indirectly environmental pollution. Needless to say, that’s a (page) killer. I was so frustrated, I was seriously trying to rewrite everything using another 3D library. (SceneJS doesn’t seem to have the same problem.)

Finally, I realized that my scene only changes on user input. Therefore, I made my call to requestAnimationFrame() conditional on whether there was a user input in the last 2 seconds. If not, I would go into standby mode and not call requestAnimationFrame(). As soon as there’s new action, I call render() again.

Here’s my code:

function render(time) {
  TWEEN.update(time);
  renderer.render(scene, camera);

  if (gLastMove + kStandbyAfter < Date.now()) {
    gRunning = false;
  } else {
    gRunning = true;
    requestAnimationFrame(render);
  }
}
var gLastMove = Date.now();
var gRunning = true;
var kStandbyAfter = 2000; // ms
function requestRender() {
  gLastMove = Date.now();
  if ( !gRunning) {
    requestAnimationFrame(render);
  }
}
window.addEventListener("mousemove", requestRender, false);
window.addEventListener("keydown", requestRender, false);

Super-ugly. But this solved my problem. The CPU usage drops from 2 cores with 100% each to almost nothing, after 2 seconds of no activity on my page.

Your situation may wary, you may other factors other than user input that cause scene changes for you. But maybe it’s a lot less than every 16ms, and more importantly, there may be phases of complete inactivity when you can turn things off entirely until some trigger. (In my case: mouse move)

I hope this helps. I still consider this to be a band-aid and ugly, and I think this should be fixed in ThreeJS. If nothing else, to save all our CPU cores on all the pages that use ThreeJS.

0reactions
rokitcommented, Oct 26, 2017

Well, I just added Stats to my app so that I could see what the frame rate drops to. Turns out it doesn’t drop. It leaps to ~7,500 FPS! Now I’m really confused.

Edit: This solution worked for me: https://stackoverflow.com/a/19772220/996314

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Limit Max FPS for your PC and Games - YouTube
How to set a limit to the maximum frame rate for your PC or programs and games using Nvidia Control Panel in Windows...
Read more >
How To Limit FPS In Games – Best FPS Limiters To Use
The best way to limit frame production is in a game's settings. This is because an external software needs to intercept and adjust...
Read more >
Stuttering/slugish feeling when FPS drops below FPS Cap G ...
The reason you have "butter smoothness" when running a steady 140 FPS, is the because the limit is the framerate cap you set,...
Read more >
Is it possible to limit framerate to a specific number ? #14315
bringing 60fps down to 30 since only half of the frames are being rendered. If a game dipped to 30 while having frame...
Read more >
should i cap my frame rate? : r/buildapc - Reddit
Capping your framerate will decrease frametimes, so you will have the smoothest experience capping it 2 frames below your max refresh rate. This ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found