tweenjs / tween.js

JavaScript/TypeScript animation engine

Home Page:https://tweenjs.github.io/tween.js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Repeating value is incorrect over time

OldManMeta opened this issue · comments

Hi,

I'm using the lib to attempt to increment a value from a start to a target, and then back down to the start again.

I am using the following code:

        // Set up the TWEEN animation
        const tween = new TWEEN.Tween({ value: startValue })
          .to({ value: endValue }, duration)
          .repeat(1) // Repeats the animation once (forward and backward)
          .yoyo(true) // Causes the animation to play forward and then backward
          .onUpdate(({ value }) => {

            console.log('Updating tween',value);
            
          })
          .onComplete(({ value }) => {
            console.log('Completed tween=>',value);            
          })
          .start();        
       }

I'm calling an update withing a React Three Fiber useFrame() hook, which is executing at 60 FPS.

The issue occurs given that the method containing this code, could be called several times in a second, and indeed the same target value could be being modified at the same time if the duration of the up and back has not stopped.

It's only a small end value difference - so if the start value is 0 and the end value is 0.8, after a few instances of this, the return to the start value in the onComplete method, shows that the value is now 0.00003 instead of pure 0.

The error is then amplified on each successive call to the code.

This of course could be me just not understanding and making the mistake in my coding?

Many thanks

A working sample of the problem would help. It could be there is an issue, and that on return of the yoyo, it should snap to the original starting value like it currently does for non-yoyo end values.

A solution for this is to probably use two tweens instead of yoyo.

tween1.chain(tween2)
tween2.chain(tween1)

where tween 2 is set up to be the opposite of tween 1.


Generally speaking I'd like to remove timeline-like capabilities from the Tween class (yoyo, chain, repeat, etc) because it complicates what the concept of a tween is, and the add a separate Timeline type of feature. F.e.

const timeline= new Timeline([
  someTween,
  someTween.reverse()
])

// In loop
timeline.update(time)

But for now, I bet having two tweens, one in opposite direction, will solve the issue.