tween.js
tween.js copied to clipboard
When the browser tab sleeps, the delay of repeated tweens is not keep
Description
When the browser tab goes into the background, the delay of repeated tween is not keep.
Expected Behavior
Tabs are restored from the background, the delay of tween is unchanged.
Actual Behavior
Tabs are restored from the background, the delay of tween changes.
Steps to Reproduce
- open https://codepen.io/masatomakino/pen/QWyQpYY
- open new tab.
- change the tab of codepen to the background and wait 5 seconds. ( =
tween._repeatDelayTime
) - change the codepen tab to foreground.
- delay of tween is not kept.
Possible Fix
When the browser tabs are in the background, requestAnimationFrame
will go to sleep. requestAnimationFrame will return, and the pass large delta time.
However, tween.js
loop does not take large delta time.
elapsed = this._duration === 0 || elapsed > 1 ? 1 : elapsed
In this process, time is rounded to elapsed
. And the delta time disappears.
const overflowCounter = Math.trunc((time - this._startTime) / this._duration);
const overflowTime = (time - this._startTime) % this._duration;
For example, we get an overflow for this process. You can use this value to modify this._repeat
and this._startTime
.
Environment
macOS 10.15.2 Chrome 83.0.4103.116 (Official Build) tween.js 18.6.0
Thank you.
Hello! Yeah, this is a known issue. The Tween code merely gets the current time, it does not account for time that the window is unfocused and requestAnimationFrame
can not run.
The current workaround is to manually update tweens, passing in our own time values to tween.update()
, and then we can manage the time any way we want.
It would be great to fix this. If interested in making a PR, happy to review.