Skip to content

Commit

Permalink
clean up a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
humodz committed Apr 19, 2024
1 parent 933982f commit 78120c5
Show file tree
Hide file tree
Showing 7 changed files with 375 additions and 381 deletions.
138 changes: 63 additions & 75 deletions dist/tween.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,13 +678,11 @@ 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 endTime = this._startTime + this._duration;
if (!this._goToEnd && !this._isPlaying) {
if (time > endTime)
Expand All @@ -711,84 +709,31 @@ define(['exports'], (function (exports) { 'use strict';
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 repeated = false;
var completed = false;
var checkStillPlayingAndReverse = function () {
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 -= completeCount;
}
// Reassign starting values, restart by making startTime = now
for (property in _this._valuesStartRepeat) {
if (!_this._yoyo && typeof _this._valuesEnd[property] === 'string') {
_this._valuesStartRepeat[property] =
// eslint-disable-next-line
// @ts-ignore FIXME?
_this._valuesStartRepeat[property] + parseFloat(_this._valuesEnd[property]);
}
if (_this._yoyo) {
_this._swapEndStartRepeatValues(property);
}
_this._valuesStart[property] = _this._valuesStartRepeat[property];
}
if (_this._yoyo) {
_this._reversed = !_this._reversed;
}
_this._startTime += durationAndDelay * completeCount;
repeated = true;
return true;
}
else {
completed = true;
return false;
}
}
return true;
};
var doUpdates = function () {
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);
}
};
var stillPlaying;
if (elapsedTime <= this._duration) {
doUpdates();
stillPlaying = checkStillPlayingAndReverse();
var elapsed = this._calculateElapsedPortion(elapsedTime, durationAndDelay, totalTime);
var value = this._easingFunction(elapsed);
var status = this._calculateCompletionStatus(elapsedTime, durationAndDelay);
if (status === 'repeat') {
// the current update is happening after the instant the tween repeated
this._processRepetition(elapsedTime, durationAndDelay);
}
else {
stillPlaying = checkStillPlayingAndReverse();
doUpdates();
this._updateProperties(this._object, this._valuesStart, this._valuesEnd, value);
if (status === 'about-to-repeat') {
// the current update is happening at the exact instant the tween is going to repeat
// the values should match the end of the tween, not the beginning,
// that's why _processRepetition happens after _updateProperties
this._processRepetition(elapsedTime, durationAndDelay);
}
if (this._onUpdateCallback) {
this._onUpdateCallback(this._object, elapsed);
}
if (repeated) {
if (status === 'repeat' || status === 'about-to-repeat') {
if (this._onRepeatCallback) {
this._onRepeatCallback(this._object);
}
this._onEveryStartCallbackFired = false;
}
if (completed) {
completed = true;
else if (status === 'completed') {
this._isPlaying = false;
if (this._onCompleteCallback) {
this._onCompleteCallback(this._object);
}
Expand All @@ -797,9 +742,52 @@ define(['exports'], (function (exports) { 'use strict';
// even if the `update()` method was called way past the duration of the tween
this._chainedTweens[i].start(this._startTime + this._duration, false);
}
this._isPlaying = false;
}
return stillPlaying;
return status !== 'completed';
};
Tween.prototype._calculateElapsedPortion = function (elapsedTime, durationAndDelay, totalTime) {
if (this._duration === 0 || elapsedTime > totalTime) {
return 1;
}
var timeIntoCurrentRepeat = elapsedTime % durationAndDelay;
var portion = Math.min(timeIntoCurrentRepeat / this._duration, 1);
if (portion === 0 && elapsedTime !== 0 && elapsedTime % this._duration === 0) {
return 1;
}
return portion;
};
Tween.prototype._calculateCompletionStatus = function (elapsedTime, durationAndDelay) {
if (this._duration !== 0 && elapsedTime < this._duration) {
return 'playing';
}
if (this._repeat <= 0) {
return 'completed';
}
if (elapsedTime === this._duration) {
return 'about-to-repeat';
}
return 'repeat';
};
Tween.prototype._processRepetition = function (elapsedTime, durationAndDelay) {
var completeCount = Math.min(Math.trunc((elapsedTime - this._duration) / durationAndDelay) + 1, this._repeat);
if (isFinite(this._repeat)) {
this._repeat -= completeCount;
}
// Reassign starting values, restart by making startTime = now
for (var property in this._valuesStartRepeat) {
var valueEnd = this._valuesEnd[property];
if (!this._yoyo && typeof valueEnd === 'string') {
this._valuesStartRepeat[property] = this._valuesStartRepeat[property] + parseFloat(valueEnd);
}
if (this._yoyo) {
this._swapEndStartRepeatValues(property);
}
this._valuesStart[property] = this._valuesStartRepeat[property];
}
if (this._yoyo) {
this._reversed = !this._reversed;
}
this._startTime += durationAndDelay * completeCount;
};
Tween.prototype._updateProperties = function (_object, _valuesStart, _valuesEnd, value) {
for (var property in _valuesEnd) {
Expand Down
138 changes: 63 additions & 75 deletions dist/tween.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -680,13 +680,11 @@ 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 endTime = this._startTime + this._duration;
if (!this._goToEnd && !this._isPlaying) {
if (time > endTime)
Expand All @@ -713,84 +711,31 @@ var Tween = /** @class */ (function () {
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 repeated = false;
var completed = false;
var checkStillPlayingAndReverse = function () {
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 -= completeCount;
}
// Reassign starting values, restart by making startTime = now
for (property in _this._valuesStartRepeat) {
if (!_this._yoyo && typeof _this._valuesEnd[property] === 'string') {
_this._valuesStartRepeat[property] =
// eslint-disable-next-line
// @ts-ignore FIXME?
_this._valuesStartRepeat[property] + parseFloat(_this._valuesEnd[property]);
}
if (_this._yoyo) {
_this._swapEndStartRepeatValues(property);
}
_this._valuesStart[property] = _this._valuesStartRepeat[property];
}
if (_this._yoyo) {
_this._reversed = !_this._reversed;
}
_this._startTime += durationAndDelay * completeCount;
repeated = true;
return true;
}
else {
completed = true;
return false;
}
}
return true;
};
var doUpdates = function () {
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);
}
};
var stillPlaying;
if (elapsedTime <= this._duration) {
doUpdates();
stillPlaying = checkStillPlayingAndReverse();
var elapsed = this._calculateElapsedPortion(elapsedTime, durationAndDelay, totalTime);
var value = this._easingFunction(elapsed);
var status = this._calculateCompletionStatus(elapsedTime, durationAndDelay);
if (status === 'repeat') {
// the current update is happening after the instant the tween repeated
this._processRepetition(elapsedTime, durationAndDelay);
}
else {
stillPlaying = checkStillPlayingAndReverse();
doUpdates();
this._updateProperties(this._object, this._valuesStart, this._valuesEnd, value);
if (status === 'about-to-repeat') {
// the current update is happening at the exact instant the tween is going to repeat
// the values should match the end of the tween, not the beginning,
// that's why _processRepetition happens after _updateProperties
this._processRepetition(elapsedTime, durationAndDelay);
}
if (this._onUpdateCallback) {
this._onUpdateCallback(this._object, elapsed);
}
if (repeated) {
if (status === 'repeat' || status === 'about-to-repeat') {
if (this._onRepeatCallback) {
this._onRepeatCallback(this._object);
}
this._onEveryStartCallbackFired = false;
}
if (completed) {
completed = true;
else if (status === 'completed') {
this._isPlaying = false;
if (this._onCompleteCallback) {
this._onCompleteCallback(this._object);
}
Expand All @@ -799,9 +744,52 @@ var Tween = /** @class */ (function () {
// even if the `update()` method was called way past the duration of the tween
this._chainedTweens[i].start(this._startTime + this._duration, false);
}
this._isPlaying = false;
}
return stillPlaying;
return status !== 'completed';
};
Tween.prototype._calculateElapsedPortion = function (elapsedTime, durationAndDelay, totalTime) {
if (this._duration === 0 || elapsedTime > totalTime) {
return 1;
}
var timeIntoCurrentRepeat = elapsedTime % durationAndDelay;
var portion = Math.min(timeIntoCurrentRepeat / this._duration, 1);
if (portion === 0 && elapsedTime !== 0 && elapsedTime % this._duration === 0) {
return 1;
}
return portion;
};
Tween.prototype._calculateCompletionStatus = function (elapsedTime, durationAndDelay) {
if (this._duration !== 0 && elapsedTime < this._duration) {
return 'playing';
}
if (this._repeat <= 0) {
return 'completed';
}
if (elapsedTime === this._duration) {
return 'about-to-repeat';
}
return 'repeat';
};
Tween.prototype._processRepetition = function (elapsedTime, durationAndDelay) {
var completeCount = Math.min(Math.trunc((elapsedTime - this._duration) / durationAndDelay) + 1, this._repeat);
if (isFinite(this._repeat)) {
this._repeat -= completeCount;
}
// Reassign starting values, restart by making startTime = now
for (var property in this._valuesStartRepeat) {
var valueEnd = this._valuesEnd[property];
if (!this._yoyo && typeof valueEnd === 'string') {
this._valuesStartRepeat[property] = this._valuesStartRepeat[property] + parseFloat(valueEnd);
}
if (this._yoyo) {
this._swapEndStartRepeatValues(property);
}
this._valuesStart[property] = this._valuesStartRepeat[property];
}
if (this._yoyo) {
this._reversed = !this._reversed;
}
this._startTime += durationAndDelay * completeCount;
};
Tween.prototype._updateProperties = function (_object, _valuesStart, _valuesEnd, value) {
for (var property in _valuesEnd) {
Expand Down
3 changes: 3 additions & 0 deletions dist/tween.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ declare class Tween<T extends UnknownProps> {
* it is still playing, just paused).
*/
update(time?: number, autoStart?: boolean): boolean;
private _calculateElapsedPortion;
private _calculateCompletionStatus;
private _processRepetition;
private _updateProperties;
private _handleRelativeValue;
private _swapEndStartRepeatValues;
Expand Down
Loading

0 comments on commit 78120c5

Please sign in to comment.