tween.js icon indicating copy to clipboard operation
tween.js copied to clipboard

Fix yoyo wrong by calculate elapsed portion

Open trusktr opened this issue 1 year ago • 3 comments

Re-opening of

  • https://github.com/tweenjs/tween.js/pull/691

to get the tests to run and update it with latest from main.

trusktr avatar Jan 11 '25 02:01 trusktr

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!

trusktr avatar Jan 11 '25 02:01 trusktr

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

alphabetabc avatar Jan 20 '25 10:01 alphabetabc

@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;
};

image

alphabetabc avatar Mar 18 '25 11:03 alphabetabc