MozBeforePaint 编辑

Gecko 2.0 adds a new method for performing JavaScript controlled animations that synchronize not only with one another, but also with CSS transitions and SMIL animations being performed within the same window.

Usage outline

In order to time your animation, the first thing you need to know is the time base; that is, the time at which your animation sequence started. You can determine this value by looking at window.mozAnimationStartTime. This new property indicates the time, in milliseconds since epoch, at which all animations started in the specified window during the current refresh interval should be considered to have started running.

With that value in hand, you can then schedule all your subsequent animation frames. Whenever you're ready to refresh your animation, you call the window.requestAnimationFrame() method. Once you've called this, the MozBeforePaint event will be fired one time, when it's time for animations to be updated for the window's next animation frame.  The timeStamp property of this event will be set to the time when the animation frame is sampled; this is relevant when trying to synchronize mozRequestAnimationFrame animations with SMIL animations or CSS transitions.

This allows multiple animations to remain in sync with one another within the context of a given window.

Frame rate control

MozBeforePaint won't fire more than a fixed number of times per second, e.g. 50 or 60. This is intentional, because modern operating systems and hardware won't let the browser display more frames than that anyway. Limiting the frame rate avoids wasting work, thereby saving CPU usage and power and improving overall system performance.

Example

This example performs a simple animation, moving a box a short distance after a short period of time.

<!DOCTYPE HTML>
<html>
<body>
<div id="d" style="width:100px; height:100px; background:lime; position:relative;"></div>
<script>
var d = document.getElementById("d");
var start = window.mozAnimationStartTime;
function step(event) {
  var progress = event.timeStamp - start;
  d.style.left = Math.min(progress/10, 200) + "px";
  if (progress < 2000) {
    window.mozRequestAnimationFrame();
  } else {
    window.removeEventListener("MozBeforePaint", step, false);
  }
}
window.addEventListener("MozBeforePaint", step, false);
window.mozRequestAnimationFrame();
</script>
</body>
</html>

As you can see, each time the MozBeforePaint event fires, our step() method is called. This computes the current position for the animating box and updates the box's position on screen, and, if the animation sequence is not yet complete, calls window.requestAnimationFrame() to schedule the next animation frame to be drawn. When the animation sequence completes, removes the event listener.

You may take a look at this example live if you like.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:121 次

字数:5551

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文