Skip to content

Commit ff4f0ad

Browse files
authored
Update tween.js (#27670)
1 parent ffa128c commit ff4f0ad

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

examples/jsm/libs/tween.module.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -413,18 +413,21 @@ var Tween = /** @class */ (function () {
413413
Tween.prototype.isPaused = function () {
414414
return this._isPaused;
415415
};
416+
Tween.prototype.getDuration = function () {
417+
return this._duration;
418+
};
416419
Tween.prototype.to = function (target, duration) {
417420
if (duration === void 0) { duration = 1000; }
418421
if (this._isPlaying)
419422
throw new Error('Can not call Tween.to() while Tween is already started or paused. Stop the Tween first.');
420423
this._valuesEnd = target;
421424
this._propertiesAreSetUp = false;
422-
this._duration = duration;
425+
this._duration = duration < 0 ? 0 : duration;
423426
return this;
424427
};
425428
Tween.prototype.duration = function (duration) {
426429
if (duration === void 0) { duration = 1000; }
427-
this._duration = duration;
430+
this._duration = duration < 0 ? 0 : duration;
428431
return this;
429432
};
430433
Tween.prototype.dynamic = function (dynamic) {
@@ -673,12 +676,13 @@ var Tween = /** @class */ (function () {
673676
* it is still playing, just paused).
674677
*/
675678
Tween.prototype.update = function (time, autoStart) {
679+
var _this = this;
680+
var _a;
676681
if (time === void 0) { time = now(); }
677682
if (autoStart === void 0) { autoStart = true; }
678683
if (this._isPaused)
679684
return true;
680685
var property;
681-
var elapsed;
682686
var endTime = this._startTime + this._duration;
683687
if (!this._goToEnd && !this._isPlaying) {
684688
if (time > endTime)
@@ -702,18 +706,37 @@ var Tween = /** @class */ (function () {
702706
}
703707
this._onEveryStartCallbackFired = true;
704708
}
705-
elapsed = (time - this._startTime) / this._duration;
706-
elapsed = this._duration === 0 || elapsed > 1 ? 1 : elapsed;
709+
var elapsedTime = time - this._startTime;
710+
var durationAndDelay = this._duration + ((_a = this._repeatDelayTime) !== null && _a !== void 0 ? _a : this._delayTime);
711+
var totalTime = this._duration + this._repeat * durationAndDelay;
712+
var calculateElapsedPortion = function () {
713+
if (_this._duration === 0)
714+
return 1;
715+
if (elapsedTime > totalTime) {
716+
return 1;
717+
}
718+
var timesRepeated = Math.trunc(elapsedTime / durationAndDelay);
719+
var timeIntoCurrentRepeat = elapsedTime - timesRepeated * durationAndDelay;
720+
// TODO use %?
721+
// const timeIntoCurrentRepeat = elapsedTime % durationAndDelay
722+
var portion = Math.min(timeIntoCurrentRepeat / _this._duration, 1);
723+
if (portion === 0 && elapsedTime === _this._duration) {
724+
return 1;
725+
}
726+
return portion;
727+
};
728+
var elapsed = calculateElapsedPortion();
707729
var value = this._easingFunction(elapsed);
708730
// properties transformations
709731
this._updateProperties(this._object, this._valuesStart, this._valuesEnd, value);
710732
if (this._onUpdateCallback) {
711733
this._onUpdateCallback(this._object, elapsed);
712734
}
713-
if (elapsed === 1) {
735+
if (this._duration === 0 || elapsedTime >= this._duration) {
714736
if (this._repeat > 0) {
737+
var completeCount = Math.min(Math.trunc((elapsedTime - this._duration) / durationAndDelay) + 1, this._repeat);
715738
if (isFinite(this._repeat)) {
716-
this._repeat--;
739+
this._repeat -= completeCount;
717740
}
718741
// Reassign starting values, restart by making startTime = now
719742
for (property in this._valuesStartRepeat) {
@@ -731,12 +754,7 @@ var Tween = /** @class */ (function () {
731754
if (this._yoyo) {
732755
this._reversed = !this._reversed;
733756
}
734-
if (this._repeatDelayTime !== undefined) {
735-
this._startTime = time + this._repeatDelayTime;
736-
}
737-
else {
738-
this._startTime = time + this._delayTime;
739-
}
757+
this._startTime += durationAndDelay * completeCount;
740758
if (this._onRepeatCallback) {
741759
this._onRepeatCallback(this._object);
742760
}
@@ -812,7 +830,7 @@ var Tween = /** @class */ (function () {
812830
return Tween;
813831
}());
814832

815-
var VERSION = '21.0.0';
833+
var VERSION = '23.1.1';
816834

817835
/**
818836
* Tween.js - Licensed under the MIT license

0 commit comments

Comments
 (0)