mirror of
https://github.com/john-okeefe/foliate-js.git
synced 2026-09-09 11:29:14 -04:00
Fix blocked animate when running in background (#80)
This commit is contained in:
@@ -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