mirror of
https://github.com/john-okeefe/foliate-js.git
synced 2026-10-24 22:59:51 -04:00
Fix blocked animate when running in background (#80)
This commit is contained in:
1 parent
55c0027d11
commit
9fd2209b4d
1 file changed
+8
@@ -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)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user