我有一種情況,我想在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