我有一种情况,我想在1秒内将600px宽的div的宽度设置为0px。我可以使用requestAnimationFrame()
。但如果动画需要1秒钟,我不会100%确定。如何调用requestAnimationFrame()独立于refreshrates
这将是这个样子:
let width = 600;
let animation;
function animateWidth(){
if(width <= 0){
window.cancelAnimationFrame();
}else{
width -= 10; // (600px/60fps)
}
document.getElementById('id1').setAttribute('width', width);
animation = window.requestAnimationFrame(animateWidth);
}
animation = window.requestAnimationFrame(animateWidth);
的事情是,当一个设备有不同的FPS会影响动画的持续时间(在30fps的有可能需要2秒和60帧它将采取一个)。我想确保这个动画的持续时间总是一秒钟。如果fps率不同,我想根据持续时间更改宽度的新值(所以在30 fps下动画我们会将宽度每改变20(600px/30fps))。
- 有没有什么办法可以在使用时达到这个目的?如果我能得到帧之间的平均间隔或我认为可行的fps。
- 我是否担心这件事不是一个大问题?
- 在不同的设备(手机,电脑,平板电脑等)上我可以期待什么样的fps?
文档:https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame
你可以通过使用['performance.now()'](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now)来做到这一点。 – Xufox