简体   繁体   English

在不同的选项卡上时,使requestAnimationFrame()动画持久存在

[英]Make requestAnimationFrame() Animation Persist When on Different Tab

When you are using requestAnimationFrame() for animation, the animation persists when you change browsers but not when you change tabs within your browser. 当使用requestAnimationFrame()进行动画处理时,当您更改浏览器时动画将持续存在,但是当您更改浏览器中的选项卡时动画将持续存在。 The animation seems to pause. 动画似乎暂停了。

For example, this animation. 例如,此动画。 http://pixijs.github.io/examples/index.html?s=basics&f=spritesheet.js&title=SpriteSheet%20Animation http://pixijs.github.io/examples/index.html?s=basics&f=spritesheet.js&title=SpriteSheet%20Animation

Is there a way to make the animation persist even when you are in a different tab? 有没有办法使动画即使在其他选项卡中也能持久保存?

Your question isn't actually PIXI specific. 您的问题实际上并非特定于PIXI。 It's about how the browser handles screen updates. 关于浏览器如何处理屏幕更新。

PIXI applications typically update each frame using the common Javascript function Window.requestAnimationFrame() . PIXI应用程序通常使用通用Javascript函数Window.requestAnimationFrame()更新每个帧。

This function tends to pause when you switch tabs. 切换标签时,此功能倾向于暂停。 From the docs: 从文档:

The callback rate may be reduced to a lower rate when running in background tabs or in hidden <iframe>s in order to improve performance and battery life. 在后台选项卡或隐藏的<iframe>中运行时,回调速率可能会降低到较低的速率,以提高性能和电池寿命。

https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame https://developer.mozilla.org/zh-CN/docs/Web/API/window/requestAnimationFrame


So the short answer is no, HOWEVER you can easily simulate it by keeping keep track of the time elapsed between updates, and move your animation forward accordingly. 因此,简短的答案是“不”,但是,您可以通过跟踪更新之间经过的时间来轻松模拟它,并相应地将动画向前移动。

In the PIXI example you provided, change the animate() function to this code below, and re-run it. 在您提供的PIXI示例中,将animate()函数更改为下面的代码,然后重新运行它。 Switch tabs, and when you switch back, you will see that the animation appears to continue as though you never switched tabs. 切换选项卡,然后再切换回去,您会看到动画似乎在继续,就像从未切换过选项卡一样。 Hope that helps! 希望有帮助! :) :)

var lastUpdate = new Date();
function animate() {
    //Determine the amount of time since last frame update
    var now = new Date();
    var elapsed = now - lastUpdate;
    lastUpdate = now;

    //Update the rotation based on time elapsed
    movie.rotation += elapsed/1000;

    // render the stage container
    renderer.render(stage);    
    requestAnimationFrame(animate);
}

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

相关问题 使用鼠标坐标制作动画时的requestAnimationFrame - requestAnimationFrame when using mouse cordinates to make animation 使用 requestAnimationFrame 时,如何使 animation 的部分更新更快 - When using requestAnimationFrame, how to make part of animation update faster requestAnimationFrame 仅在需要时运行动画比一直使用 requestAnimationFrame 快得多 - requestAnimationFrame only when needed runs the animation much faster than having requestAnimationFrame all the time requestAnimationFrame on react - 当选项卡处于非活动状态时不会停止状态更新 - requestAnimationFrame on react - doesn't stop state updates when tab is inactive 当我离开选项卡时,requestAnimationFrame() 不会在 Firefox 停止吗? - Does requestAnimationFrame() not stop in Firefox when I move away from the tab? Javascript:使用requestAnimationFrame没有动画的代码? - Javascript: code with no animation with requestAnimationFrame? 使用requestAnimationFrame重复的3秒动画 - 3 sec animation that repeats with requestAnimationFrame Canvas animation 与 requestAnimationFrame() - Canvas animation with requestAnimationFrame() 使用 requestAnimationFrame 控制 animation 速度 - Controlling animation speed with requestAnimationFrame requestAnimationFrame动画循环 - requestAnimationFrame animation loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM