Custom framerate for Threejs

By default, javascript's requestAnimationFrame will run as fast as it possibly can, and if you have much logic in your animation loop, you'll quickly hear your computer fans start to whir.

To free up CPU power for other tasks and keep  your webpage snappy, request your animation frames by timestamp instead of "asap", like this:

``` var then = 0 var now = 0 var delta = 0 var FPS = 30 // frames per second function animate(){ requestAnimationFrame( animate ) now = Date.now() delta = now - then if ( delta > FPS ) { then = now - ( delta % FPS ) // your animation code here } } animate() ```

Leave a Reply

Your email address will not be published. Required fields are marked *

oko
friend