Skip to content
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

Merged
merged 6 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/Tween.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ export class Tween<T extends UnknownProps> {

update(time?: number): boolean {
let property
let elapsed

time = time !== undefined ? time : TWEEN.now()

Expand All @@ -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
Copy link
Member

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 to timeIntoCurrentRepeat.

Copy link
Contributor Author

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.

Copy link
Member

@trusktr trusktr Apr 23, 2023

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.

// 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
Expand All @@ -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
Copy link
Member

@trusktr trusktr Apr 23, 2023

Choose a reason for hiding this comment

The 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?

Copy link
Member

@trusktr trusktr Apr 23, 2023

Choose a reason for hiding this comment

The 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 this._repeat -= completeCount and _repeat becomes 0.

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.

Copy link
Member

@trusktr trusktr Apr 23, 2023

Choose a reason for hiding this comment

The 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:

#647

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@MasatoMakino MasatoMakino Apr 24, 2023

Choose a reason for hiding this comment

The 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
Expand All @@ -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)
Expand Down
103 changes: 103 additions & 0 deletions test/unit/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,109 @@
test.done()
},

'Test TWEEN.update() should reduce the repeat count': function (test) {
TWEEN.removeAll()

const obj = {x: 0},
t = new TWEEN.Tween(obj).to({x: 100}, 100).repeat(5).delay(100)

t.start(0)

TWEEN.update(100)
test.equal(t._repeat, 5)

TWEEN.update(150)
test.equal(t._repeat, 5)

TWEEN.update(200)
test.equal(t._repeat, 4)

TWEEN.update(250)
test.equal(t._repeat, 4)

TWEEN.update(300)
test.equal(t._repeat, 4)

TWEEN.update(350)
test.equal(t._repeat, 4)

TWEEN.update(400)
test.equal(t._repeat, 3)

test.done()
},

'Test TWEEN.update() should reduce the repeat count multiple times': function (test) {
TWEEN.removeAll()

const obj = {x: 0},
t = new TWEEN.Tween(obj).to({x: 100}, 100).repeat(5).delay(100)

t.start(0)

TWEEN.update(400)
test.equal(t._repeat, 3)

test.done()
},

'Test browser tab sleep with delay': function (test) {
TWEEN.removeAll()

var obj = {x: 0},
t = new TWEEN.Tween(obj).to({x: 100}, 100).repeat(Infinity).delay(100)

t.start(0)

TWEEN.update(350)
test.equal(obj.x, 50)

TWEEN.update(750)
test.equal(obj.x, 50)

test.done()
},

'Test browser tab sleep with repeatDelay': function (test) {
TWEEN.removeAll()

var obj = {x: 0},
t = new TWEEN.Tween(obj).to({x: 100}, 100).repeat(1).repeatDelay(200)

t.start(0)

TWEEN.update(350)
test.equal(obj.x, 50)

TWEEN.update(600)
test.equal(obj.x, 100)

TWEEN.update(Infinity)
test.equal(obj.x, 100)

test.done()
},

'Test browser tab sleep with repeatDelay and delay': function (test) {
TWEEN.removeAll()

var obj = {x: 0},
t = new TWEEN.Tween(obj).to({x: 100}, 100).delay(100).repeat(1).repeatDelay(200)

t.start(0)

TWEEN.update(450)
test.equal(obj.x, 50)

TWEEN.update(500)
test.equal(obj.x, 100)

TWEEN.update(Infinity)
test.equal(obj.x, 100)

test.done()
},

'Tween.js compatible with Object.defineProperty getter / setters': function (test) {
var obj = {_x: 0}

Expand Down