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

[bug] yoyo has wrong value

Open alphabetabc opened this issue 1 year ago • 6 comments

demo

const rootEl = document.getElementById("root");
Object.assign(rootEl.style, {
    width: "800px",
    height: "600px",
});

rootEl.innerHTML = "loading";

const dataX = [];
const dataY = [];

let i = 0;
createAnimationQueue({
    from: { value: 1 },
    to: { value: 1.5 },
    yoyo: true,
    repeat: 3,
    onUpdate: (o) => {
        dataX.push(i++);
        dataY.push(o.value);
    },
    onComplete: () => {
        const chart = ec.init(rootEl);
        chart.setOption({
            xAxis: {
                type: "category",
                data: dataX,
            },
            yAxis: {
                type: "value",
            },
            series: [
                {
                    data: dataY,
                    type: "line",
                },
            ],
        });
    },
});

result

image

alphabetabc avatar Jan 25 '24 09:01 alphabetabc

Hello, can you provide a link to the codepen containing the complete demo?

mk965 avatar Jan 26 '24 10:01 mk965

https://codepen.io/humodz/pen/YzgEpXG

When the red ball reaches the end of the animation, on both directions, it blinks to the opposite side for a split second

humodz avatar Jan 26 '24 11:01 humodz

As a workaround until it's fixed, we can call TWEEN.update twice, like so:

requestAnimationFrame(function animate(time) {
  requestAnimationFrame(animate);
  TWEEN.update(time);
  TWEEN.update(time);
});

This twill trigger callbacks twice which is a bummer, but at least the weird jump goes away

Another workaround would be to generate an yoyo-ified easing function, like as follows:

function yoyo(f) {
  return t => {
    if (t < 0.5) {
      return f(2 * t);
    } else {
      return 1 - f(2 * (t - 0.5));
    }
  }
}

new TWEEN.Tween({ x: 0 })
  .to({ x: 200 }, 2000 * 2) // duration needs to be doubled 
  .repeat(Infinity)
  //.yoyo(true)
  .easing(yoyo(t => TWEEN.Easing.Cubic.InOut(t)))

The only gotcha is that the duration of the tween needs to be doubled, because now a single iteration does both the forward and backward movement.

humodz avatar Feb 12 '24 09:02 humodz

Fixed in

  • https://github.com/tweenjs/tween.js/pull/683 thanks to @humodz !

Released in v23.1.2

trusktr avatar May 05 '24 21:05 trusktr

Re-opening, as the merge for this broke the yoyo demo, which I've reverted. See

  • https://github.com/tweenjs/tween.js/issues/687

We assumed passing unit tests were enough, but we forgot to test the actual yoyo demo! 🥴

trusktr avatar Jul 26 '24 03:07 trusktr