Jump to content
Search Community

Is it possible to control the rate of onUpdate

Fakebook test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

I was curious if can set the rate that a specific tween fires onUpdate. Basically, I want to simulate a lower frame rate for a tween that is using onUpdate. I've seen that the TweenLite.ticker.fps(10) could work, but this seems to control the rate at all the tweens, and not just one specific tween.

 

TweenMax.to("#lowFps", 3, {
  onUpdate: () => animate()
})

 

In my Codepen example, I was hoping to have the top Tween smooth, and bottom one look like it's low fps. However, changing the ticker of course effects both.

See the Pen ydOvQm by anon (@anon) on CodePen

Link to comment
Share on other sites

2 hours ago, Visual-Q said:

Note sure about whether you can throttle a callback in that way,  but you could probably make your tween behave the way you want with stepped Ease.

 

https://greensock.com/docs/Easing/SteppedEase

 

 

 

 

 

 

 

@Visual-Q This is definitely a great idea, but it appears that using stepped easing doesn't effect the onUpdate rate. I'm guessing it might be best to throttle inside my function as @elegantseagulls mentioned.

Link to comment
Share on other sites

4 minutes ago, PointC said:

I'd go with a steppedEase. You can even use a stagger and cycle if you like.

 

 

Wow, never occured to me to pass an ease to cycle. Could do a lot of really cool stuff with that.

 

11 minutes ago, Fakebook said:

but it appears that using stepped easing doesn't effect the onUpdate rate.

 

As @PointC demonstrated the onUpdate isn't needed unless of course you intend to do more with the callback function. 

  • Like 1
Link to comment
Share on other sites

I like the SteppedEase approach, but if you really need to limit the entire update of a tween to a certain FPS, you could do something like this: 

 

See the Pen 2b4554531b20a9876dac381beea741e2 by GreenSock (@GreenSock) on CodePen

 

Here's the utility function I whipped together, so you can just pass a tween/timeline instance in, along with the desired FPS, and it'll do it all for you: 

function throttleAnimation(animation, fps) {
  var ticker = TweenLite.ticker,
      time = ticker.time,
      frameLength = 1 / fps,
      frame = 0,
      update = function() {
        var newTime = ticker.time - time,
            newFrame = ~~(newTime / frameLength);
        if (frame !== newFrame) {
          frame = newFrame;
          animation.time(newTime);
          if (animation.progress() === 1) {
            ticker.removeEventListener("tick", update);
          }
        }
      };
  animation.pause();
  ticker.addEventListener("tick", update);
}

 

Does that help? 

  • Like 4
Link to comment
Share on other sites

  • 9 months later...

Thanks Jack! This is how the function looks like in GSAP 3:

  const throttleAnimation = (animation, fps) => {
	let frame = 0

	const ticker = gsap.ticker,
	  time = ticker.time,
	  frameLength = 1 / fps,
	  update = function () {
		const newTime = ticker.time - time,
		  newFrame = ~~(newTime / frameLength)
		if (frame !== newFrame) {
		  frame = newFrame
		  animation.time(newTime)
		  if (animation.progress() === 1) {
			ticker.remove(update)
		  }
		}
	  }

	animation.pause()
	ticker.add(update)
  }
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...