简体   繁体   English

使用RequestAnimationFrame准确计时

[英]Accurate Timing with RequestAnimationFrame

I've not found a lot of documentation on this subject so far. 到目前为止,我还没有找到关于这个主题的大量文档。 The general feel I get is when implementing animations for the browser it is better to use RequestAnimationFrame over bog standard JavaScript timers. 我得到的一般感觉是,在为浏览器实现动画时,最好使用RequestAnimationFrame而不是bog标准JavaScript计时器。 My understanding is that timers are unreliable and their resolution can vary between different browsers. 我的理解是定时器不可靠,并且它们的分辨率可能因不同的浏览器而异。

I've been looking at this gist: https://gist.github.com/1114293#file_anim_loop_x.js 我一直在关注这个要点: https//gist.github.com/1114293#file_anim_loop_x.js

But it's not clear to me how you can ensure that an animation transpires over a fixed amount of time. 但是我不清楚如何确保动画在固定的时间内发生。 For example I might want to animate something from left to right within 2 seconds. 例如,我可能想在2秒内从左到右动画一些东西。 Is this do-able using RequestAnimationFrame or does it defeat the purpose? 这是否可以使用RequestAnimationFrame或者它是否能够达到目的?

It just so happens, I had a similar problem not long ago and came up with an approach which combines rAF with performance.now() which works very effectively. 碰巧的是,我不久前遇到了类似的问题,并提出了一种将rAF与performance.now()相结合的方法,该方法非常有效。

Im now able to make timers accurate to approx 12 decimal places: 我现在能够将定时器精确到小数点后12位:

    window.performance = window.performance || {};
    window.performance.now = (function() {
        return performance.now       ||
            window.performance.mozNow    ||
            window.performance.msNow     ||
            window.performance.oNow      ||
            window.performance.webkitNow ||
                function() {
                    return new Date().getTime(); 
                };
        })();

http://jsfiddle.net/CGWGreen/9pg9L/ http://jsfiddle.net/CGWGreen/9pg9L/

I would implement a time based animation. 我会实现一个基于时间的动画。 In each rAF iteration you can measure the time lapse respect to the previous iteration. 在每次rAF迭代中,您可以测量相对于前一次迭代的时间流逝。 Knowing this delta time and the "distance" in pixels you can calculate the necessary speed to get the 2 secs. 知道这个增量时间和像素的“距离”,你可以计算出获得2秒的必要速度。

In this article from Mozilla Hacks are treated various considerations when animating at constant speed (pixel/sec). 在本文中, Mozilla Hacks在以恒定速度(像素/秒)制作动画时会受到各种考虑。 Here is a snippet that explains the basic concept: 这是一个解释基本概念的片段:

animLoop(function( deltaT ) {
  elem.style.left = ( left += 10 * deltaT / 16 ) + "px";
  if ( left > 400 ) {
    return false;
  }
});

RequestAnimationFrame is better in next thing: you can draw when it sould be most effective. RequestAnimationFrame在接下来的事情上会更好:你可以在它最有效的时候绘制。 Ie RequestAnimationFrame could give better fps than just timers, but straight exact timing may be even less reliable. RequestAnimationFrame可以提供比定时器更好的fps,但直接精确定时可能更不可靠。 You should get current time, compare it with value from previous frame and calculate animations in accordance with amount of time passed since last frame. 您应该获取当前时间,将其与前一帧的值进行比较,并根据自上一帧以来经过的时间量计算动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM