Fix blocked animate when running in background (#80)

This commit is contained in:
Huang Xin
2025-08-01 23:43:27 +00:00
committed by GitHub
parent 55c0027d11
commit 9fd2209b4d
+8
View File
@@ -19,12 +19,20 @@ const easeOutQuad = x => 1 - (1 - x) * (1 - x)
const animate = (a, b, duration, ease, render) => new Promise(resolve => { const animate = (a, b, duration, ease, render) => new Promise(resolve => {
let start let start
const step = now => { const step = now => {
if (document.hidden) {
render(lerp(a, b, 1))
return resolve()
}
start ??= now start ??= now
const fraction = Math.min(1, (now - start) / duration) const fraction = Math.min(1, (now - start) / duration)
render(lerp(a, b, ease(fraction))) render(lerp(a, b, ease(fraction)))
if (fraction < 1) requestAnimationFrame(step) if (fraction < 1) requestAnimationFrame(step)
else resolve() else resolve()
} }
if (document.hidden) {
render(lerp(a, b, 1))
return resolve()
}
requestAnimationFrame(step) requestAnimationFrame(step)
}) })