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 all 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
37 changes: 26 additions & 11 deletions dist/tween.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,12 +675,13 @@ define(['exports'], (function (exports) { 'use strict';
* it is still playing, just paused).
*/
Tween.prototype.update = function (time, autoStart) {
var _this = this;
var _a;
if (time === void 0) { time = now(); }
if (autoStart === void 0) { autoStart = true; }
if (this._isPaused)
return true;
var property;
var elapsed;
var endTime = this._startTime + this._duration;
if (!this._goToEnd && !this._isPlaying) {
if (time > endTime)
Expand All @@ -704,18 +705,37 @@ define(['exports'], (function (exports) { 'use strict';
}
this._onEveryStartCallbackFired = true;
}
elapsed = (time - this._startTime) / this._duration;
elapsed = this._duration === 0 || elapsed > 1 ? 1 : elapsed;
var elapsedTime = time - this._startTime;
var durationAndDelay = this._duration + ((_a = this._repeatDelayTime) !== null && _a !== void 0 ? _a : this._delayTime);
var totalTime = this._duration + this._repeat * durationAndDelay;
var calculateElapsedPortion = function () {
if (_this._duration === 0)
return 1;
if (elapsedTime > totalTime) {
return 1;
}
var timesRepeated = Math.trunc(elapsedTime / durationAndDelay);
var timeIntoCurrentRepeat = elapsedTime - timesRepeated * durationAndDelay;
// TODO use %?
// const timeIntoCurrentRepeat = elapsedTime % durationAndDelay
var portion = Math.min(timeIntoCurrentRepeat / _this._duration, 1);
if (portion === 0 && elapsedTime === _this._duration) {
return 1;
}
return portion;
};
var elapsed = calculateElapsedPortion();
var value = this._easingFunction(elapsed);
// properties transformations
this._updateProperties(this._object, this._valuesStart, this._valuesEnd, value);
if (this._onUpdateCallback) {
this._onUpdateCallback(this._object, elapsed);
}
if (elapsed === 1) {
if (this._duration === 0 || elapsedTime >= this._duration) {
if (this._repeat > 0) {
var completeCount = Math.min(Math.trunc((elapsedTime - this._duration) / durationAndDelay) + 1, this._repeat);
if (isFinite(this._repeat)) {
this._repeat--;
this._repeat -= completeCount;
}
// Reassign starting values, restart by making startTime = now
for (property in this._valuesStartRepeat) {
Expand All @@ -733,12 +753,7 @@ define(['exports'], (function (exports) { 'use strict';
if (this._yoyo) {
this._reversed = !this._reversed;
}
if (this._repeatDelayTime !== undefined) {
this._startTime = time + this._repeatDelayTime;
}
else {
this._startTime = time + this._delayTime;
}
this._startTime += durationAndDelay * completeCount;
if (this._onRepeatCallback) {
this._onRepeatCallback(this._object);
}
Expand Down
37 changes: 26 additions & 11 deletions dist/tween.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -677,12 +677,13 @@ var Tween = /** @class */ (function () {
* it is still playing, just paused).
*/
Tween.prototype.update = function (time, autoStart) {
var _this = this;
var _a;
if (time === void 0) { time = now(); }
if (autoStart === void 0) { autoStart = true; }
if (this._isPaused)
return true;
var property;
var elapsed;
var endTime = this._startTime + this._duration;
if (!this._goToEnd && !this._isPlaying) {
if (time > endTime)
Expand All @@ -706,18 +707,37 @@ var Tween = /** @class */ (function () {
}
this._onEveryStartCallbackFired = true;
}
elapsed = (time - this._startTime) / this._duration;
elapsed = this._duration === 0 || elapsed > 1 ? 1 : elapsed;
var elapsedTime = time - this._startTime;
var durationAndDelay = this._duration + ((_a = this._repeatDelayTime) !== null && _a !== void 0 ? _a : this._delayTime);
var totalTime = this._duration + this._repeat * durationAndDelay;
var calculateElapsedPortion = function () {
if (_this._duration === 0)
return 1;
if (elapsedTime > totalTime) {
return 1;
}
var timesRepeated = Math.trunc(elapsedTime / durationAndDelay);
var timeIntoCurrentRepeat = elapsedTime - timesRepeated * durationAndDelay;
// TODO use %?
// const timeIntoCurrentRepeat = elapsedTime % durationAndDelay
var portion = Math.min(timeIntoCurrentRepeat / _this._duration, 1);
if (portion === 0 && elapsedTime === _this._duration) {
return 1;
}
return portion;
};
var elapsed = calculateElapsedPortion();
var value = this._easingFunction(elapsed);
// properties transformations
this._updateProperties(this._object, this._valuesStart, this._valuesEnd, value);
if (this._onUpdateCallback) {
this._onUpdateCallback(this._object, elapsed);
}
if (elapsed === 1) {
if (this._duration === 0 || elapsedTime >= this._duration) {
if (this._repeat > 0) {
var completeCount = Math.min(Math.trunc((elapsedTime - this._duration) / durationAndDelay) + 1, this._repeat);
if (isFinite(this._repeat)) {
this._repeat--;
this._repeat -= completeCount;
}
// Reassign starting values, restart by making startTime = now
for (property in this._valuesStartRepeat) {
Expand All @@ -735,12 +755,7 @@ var Tween = /** @class */ (function () {
if (this._yoyo) {
this._reversed = !this._reversed;
}
if (this._repeatDelayTime !== undefined) {
this._startTime = time + this._repeatDelayTime;
}
else {
this._startTime = time + this._delayTime;
}
this._startTime += durationAndDelay * completeCount;
if (this._onRepeatCallback) {
this._onRepeatCallback(this._object);
}
Expand Down
37 changes: 26 additions & 11 deletions dist/tween.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,12 +673,13 @@ var Tween = /** @class */ (function () {
* it is still playing, just paused).
*/
Tween.prototype.update = function (time, autoStart) {
var _this = this;
var _a;
if (time === void 0) { time = now(); }
if (autoStart === void 0) { autoStart = true; }
if (this._isPaused)
return true;
var property;
var elapsed;
var endTime = this._startTime + this._duration;
if (!this._goToEnd && !this._isPlaying) {
if (time > endTime)
Expand All @@ -702,18 +703,37 @@ var Tween = /** @class */ (function () {
}
this._onEveryStartCallbackFired = true;
}
elapsed = (time - this._startTime) / this._duration;
elapsed = this._duration === 0 || elapsed > 1 ? 1 : elapsed;
var elapsedTime = time - this._startTime;
var durationAndDelay = this._duration + ((_a = this._repeatDelayTime) !== null && _a !== void 0 ? _a : this._delayTime);
var totalTime = this._duration + this._repeat * durationAndDelay;
var calculateElapsedPortion = function () {
if (_this._duration === 0)
return 1;
if (elapsedTime > totalTime) {
return 1;
}
var timesRepeated = Math.trunc(elapsedTime / durationAndDelay);
var timeIntoCurrentRepeat = elapsedTime - timesRepeated * durationAndDelay;
// TODO use %?
// const timeIntoCurrentRepeat = elapsedTime % durationAndDelay
var portion = Math.min(timeIntoCurrentRepeat / _this._duration, 1);
if (portion === 0 && elapsedTime === _this._duration) {
return 1;
}
return portion;
};
var elapsed = calculateElapsedPortion();
var value = this._easingFunction(elapsed);
// properties transformations
this._updateProperties(this._object, this._valuesStart, this._valuesEnd, value);
if (this._onUpdateCallback) {
this._onUpdateCallback(this._object, elapsed);
}
if (elapsed === 1) {
if (this._duration === 0 || elapsedTime >= this._duration) {
if (this._repeat > 0) {
var completeCount = Math.min(Math.trunc((elapsedTime - this._duration) / durationAndDelay) + 1, this._repeat);
if (isFinite(this._repeat)) {
this._repeat--;
this._repeat -= completeCount;
}
// Reassign starting values, restart by making startTime = now
for (property in this._valuesStartRepeat) {
Expand All @@ -731,12 +751,7 @@ var Tween = /** @class */ (function () {
if (this._yoyo) {
this._reversed = !this._reversed;
}
if (this._repeatDelayTime !== undefined) {
this._startTime = time + this._repeatDelayTime;
}
else {
this._startTime = time + this._delayTime;
}
this._startTime += durationAndDelay * completeCount;
if (this._onRepeatCallback) {
this._onRepeatCallback(this._object);
}
Expand Down
37 changes: 26 additions & 11 deletions dist/tween.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,12 +679,13 @@
* it is still playing, just paused).
*/
Tween.prototype.update = function (time, autoStart) {
var _this = this;
var _a;
if (time === void 0) { time = now(); }
if (autoStart === void 0) { autoStart = true; }
if (this._isPaused)
return true;
var property;
var elapsed;
var endTime = this._startTime + this._duration;
if (!this._goToEnd && !this._isPlaying) {
if (time > endTime)
Expand All @@ -708,18 +709,37 @@
}
this._onEveryStartCallbackFired = true;
}
elapsed = (time - this._startTime) / this._duration;
elapsed = this._duration === 0 || elapsed > 1 ? 1 : elapsed;
var elapsedTime = time - this._startTime;
var durationAndDelay = this._duration + ((_a = this._repeatDelayTime) !== null && _a !== void 0 ? _a : this._delayTime);
var totalTime = this._duration + this._repeat * durationAndDelay;
var calculateElapsedPortion = function () {
if (_this._duration === 0)
return 1;
if (elapsedTime > totalTime) {
return 1;
}
var timesRepeated = Math.trunc(elapsedTime / durationAndDelay);
var timeIntoCurrentRepeat = elapsedTime - timesRepeated * durationAndDelay;
// TODO use %?
// const timeIntoCurrentRepeat = elapsedTime % durationAndDelay
var portion = Math.min(timeIntoCurrentRepeat / _this._duration, 1);
if (portion === 0 && elapsedTime === _this._duration) {
return 1;
}
return portion;
};
var elapsed = calculateElapsedPortion();
var value = this._easingFunction(elapsed);
// properties transformations
this._updateProperties(this._object, this._valuesStart, this._valuesEnd, value);
if (this._onUpdateCallback) {
this._onUpdateCallback(this._object, elapsed);
}
if (elapsed === 1) {
if (this._duration === 0 || elapsedTime >= this._duration) {
if (this._repeat > 0) {
var completeCount = Math.min(Math.trunc((elapsedTime - this._duration) / durationAndDelay) + 1, this._repeat);
if (isFinite(this._repeat)) {
this._repeat--;
this._repeat -= completeCount;
}
// Reassign starting values, restart by making startTime = now
for (property in this._valuesStartRepeat) {
Expand All @@ -737,12 +757,7 @@
if (this._yoyo) {
this._reversed = !this._reversed;
}
if (this._repeatDelayTime !== undefined) {
this._startTime = time + this._repeatDelayTime;
}
else {
this._startTime = time + this._delayTime;
}
this._startTime += durationAndDelay * completeCount;
if (this._onRepeatCallback) {
this._onRepeatCallback(this._object);
}
Expand Down
35 changes: 25 additions & 10 deletions src/Tween.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ export class Tween<T extends UnknownProps> {
if (this._isPaused) return true

let property
let elapsed

const endTime = this._startTime + this._duration

Expand Down Expand Up @@ -428,9 +427,28 @@ export class Tween<T extends UnknownProps> {
this._onEveryStartCallbackFired = 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 @@ -440,10 +458,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 @@ -466,11 +485,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
Loading
Loading