-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handling of repeats with browser tab sleep #564
Changes from 5 commits
4fbced8
21cc033
f6c8ac1
bc5aad5
2a4195a
bb608dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -321,7 +321,6 @@ export class Tween<T extends UnknownProps> { | |
|
||
update(time?: number): boolean { | ||
let property | ||
let elapsed | ||
|
||
time = time !== undefined ? time : TWEEN.now() | ||
|
||
|
@@ -348,9 +347,28 @@ export class Tween<T extends UnknownProps> { | |
this._onStartCallbackFired = true | ||
} | ||
|
||
elapsed = (time - this._startTime) / this._duration | ||
elapsed = this._duration === 0 || elapsed > 1 ? 1 : elapsed | ||
const elapsedTime = time - this._startTime | ||
const durationAndDelay = this._duration + (this._repeatDelayTime ?? this._delayTime) | ||
const totalTime = this._duration + this._repeat * durationAndDelay | ||
|
||
const calculateElapsedPortion = () => { | ||
if (this._duration === 0) return 1 | ||
if (elapsedTime > totalTime) { | ||
return 1 | ||
} | ||
|
||
const timesRepeated = Math.trunc(elapsedTime / durationAndDelay) | ||
const timeIntoCurrentRepeat = elapsedTime - timesRepeated * durationAndDelay | ||
// TODO use %? | ||
// const timeIntoCurrentRepeat = elapsedTime % durationAndDelay | ||
|
||
const portion = Math.min(timeIntoCurrentRepeat / this._duration, 1) | ||
if (portion === 0 && elapsedTime === this._duration) { | ||
return 1 | ||
} | ||
return portion | ||
} | ||
const elapsed = calculateElapsedPortion() | ||
const value = this._easingFunction(elapsed) | ||
|
||
// properties transformations | ||
|
@@ -360,10 +378,11 @@ export class Tween<T extends UnknownProps> { | |
this._onUpdateCallback(this._object, elapsed) | ||
} | ||
|
||
if (elapsed === 1) { | ||
if (this._duration === 0 || elapsedTime >= this._duration) { | ||
if (this._repeat > 0) { | ||
const completeCount = Math.min(Math.trunc((elapsedTime - this._duration) / durationAndDelay) + 1, this._repeat) | ||
if (isFinite(this._repeat)) { | ||
this._repeat-- | ||
this._repeat -= completeCount | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Down below, regarding the chained tweens, there is this: this._chainedTweens[i].start(this._startTime + this._duration) Does this need to be updated too? Should it now be only this._chainedTweens[i].start(this._startTime) because we already advanced _startTime to where the next repeat would have been? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This chained tween logic happens on the next update right? I wonder if it needs to happen immediately in the updated where _repeat > 0, f.e. when we run Looks like we should add a unit test for repeat + chain used together. EDIT: Aha! This issue #397 shows the problem of chained tweens starting on the next update. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is getting complicated. I think that later we can remove some complexity from here, and instead adding something like a Timeline feature would make everything a lot easier to understand: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another issue with repeat(N) is that we cannot make the time go backwards, because we lose the initial _startTime every time we increment it. Timeline would make it easy to go forwards/backward anywhere on the timeline. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is complicated to use "repeat" and "chain" at the same time. I agree with the need for additional unit tests. |
||
} | ||
|
||
// Reassign starting values, restart by making startTime = now | ||
|
@@ -386,11 +405,7 @@ export class Tween<T extends UnknownProps> { | |
this._reversed = !this._reversed | ||
} | ||
|
||
if (this._repeatDelayTime !== undefined) { | ||
this._startTime = time + this._repeatDelayTime | ||
} else { | ||
this._startTime = time + this._delayTime | ||
} | ||
this._startTime += durationAndDelay * completeCount | ||
trusktr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (this._onRepeatCallback) { | ||
this._onRepeatCallback(this._object) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MasatoMakino Hello! I renamed some vars for easier reading. F.e. I renamed
elapsedTime
totimeIntoCurrentRepeat
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with what @trusktr said.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like the naming isn't quite right yet. 🤔 EDIT: On second thought, I think naming is fine now.