admin管理员组文章数量:1420078
I'm doing an animation using javascript:
function animate(){
window.setTimeout(function(){
//Do a new frame and recall this function till the animation is finished
}, 1000/FPS);//FPS Default 15 approximately 60FPS
}
animate();
I'm wondering how I can increase the FPS without losing the quality? Using the already built-in css3 is not an option because I have custom-built an animation.
I'm doing an animation using javascript:
function animate(){
window.setTimeout(function(){
//Do a new frame and recall this function till the animation is finished
}, 1000/FPS);//FPS Default 15 approximately 60FPS
}
animate();
I'm wondering how I can increase the FPS without losing the quality? Using the already built-in css3 is not an option because I have custom-built an animation.
Share Improve this question asked Apr 9, 2011 at 11:12 einsteineinstein 13.9k29 gold badges86 silver badges110 bronze badges 1-
Have you tried
window.requestAnimationFrame
? – Richik SC Commented Jul 16, 2014 at 17:35
2 Answers
Reset to default 6A couple of suggestions:
Do not rely on
setTimeout
,setInterval
, or any other built-in utility for scheduling a function invocation to provide a high degree of accuracy or precision, particularly when you are executing custom rendering code.Decouple the ideas of
frame
andtime
in your mind. Yes an animation is internally a series of frames, but the end result is something that's supposed to appear to move fluidly through time. So instead of drawing frame 'n', scheduling a timeout for1000/FPS
and then assuming that "it's time to draw frame 'n + 1' now", try drawing frame 'n', scheduling a timeout for some very short interval (like 10 ms), and then (when the timeout fires) measuring the actual amount of time elapsed between when the animation started and the current point in time. Then use your elapsed time to decide what frame should be showing at this exact moment in time (which may still be 'n', or 'n + 1', or maybe even 'n + 3' if your rendering code takes very long to execute), and render that frame. Trust me, you'll get smoother, more consistent results that way.
In terms of how to improve the framerate and/or rendering quality once you have a reasonably set-up rendering loop, that is all about optimizing your //Do a new frame
code as much as you possibly can.
It's worth knowing a couple of things: FPS above 60 is usually meaningless, as browsers struggle to repaint the screen any quicker than that. And setTimeout is inconsistent. Just because you tell a script to run every 50ms (for example), doesn't mean it will. There's typically a variance of up to 15ms in either direction.
Decent strategies for improving quality would be making use of some sort of easing calculation, syncing the FPS with the browser repaint cycle - some information about this here - and making use of motion blur, etc, to smooth the effect.
Check out http://weblogs.mozillazine/roc/archives/2010/11/measuring_fps.html for some Firefox-specific insights.
本文标签: Increase FPS in animation in javascriptStack Overflow
版权声明:本文标题:Increase FPS in animation in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745324090a2653510.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论