Is wanting to control the FPS of my animation a good reason to continue using setTimeout in stead of requestAnimationFrame?

前端 未结 1 1718
天命终不由人
天命终不由人 2021-01-05 10:23

I\'m wondering if I should switch my game over to requestAnimationFrame. If there even is still a reason to do so anymore, as I\'ve read that setTimeout() now also pauses wh

相关标签:
1条回答
  • 2021-01-05 10:41

    requestAnimationFrame is the correct way to make animations.

    Why?

    Because the browser can optimize the render for make a smoother animation. Answering the question, another way is:

    window.requestAnimFrame = (function(){
        return  window.requestAnimationFrame       || 
              window.webkitRequestAnimationFrame || 
              window.mozRequestAnimationFrame    || 
              window.oRequestAnimationFrame      || 
              window.msRequestAnimationFrame     || 
              function(callback, element){
                window.setTimeout(callback, 1000 / 60);
              };
    })();
    

    And there is a other way in link below.

    When you call requestAnimation, you make the browser understand when is the moment to redraw the html. If you make animation/transitions in CSS and move stuff or change background (as sprites), using requestAnimation will make the render works when you call requestAnimation. Without it, the browser will render anything in which time, what can make more redraws then needed.

    Even, the way the browsers are going on, it will do more optimizations than we don't know yet. Make the browser understand what we are doing and then they help us.

    I found this link, can add some more ideas

    0 讨论(0)
提交回复
热议问题