Fix yoyo wrong by calculate elapsed portion
Re-opening of
- https://github.com/tweenjs/tween.js/pull/691
to get the tests to run and update it with latest from main.
Hey @alphabetabc, I moved your changes into this branch in the main repo, so that I could get the tests to run. Some of the tests are failing with this change now. Can you fix the tests? Would love to merge this!
Hey @alphabetabc, I moved your changes into this branch in the main repo, so that I could get the tests to run. Some of the tests are failing with this change now. Can you fix the tests? Would love to merge this!
Sorry, I've been quite busy lately. I'll take a look later
@trusktr
I have tried to fix it, please confirm if the following code is usable
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;
}
if (this._yoyo) {
if (timesRepeated % 2 === 1) {
return 1 - portion;
}
}
return portion;
};