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

拟墨画扇 提交于 2019-11-30 19:53:23

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!