Jump to content
Search Community

Adjusting animation speed on button click

PG1 test
Moderator Tag

Go to solution Solved by akapowl,

Recommended Posts

Hello @PG1

 

Was it maybe something like the latter example on this page?

 

 

 

Here is an example which is a bit more simplified. On click you could either just set the timeScale of the tween to something different to adjust the speed, or even tween on the timeScale of the tween to make the adjustment between speeds smoother. I hope this will help. Happy tweening!

 

See the Pen dyZvKvv by akapowl (@akapowl) on CodePen

  • Like 2
Link to comment
Share on other sites

Thanks, yes like that but I should have mentioned I'm using React. 

 

I am setting the overall tl speed like so: 
W

tl.current.timeScale(speed);

 

then i'd like to be able to click a button and have that change. I'm trying to set speed to a different value on click, then have a use effect watch that and update the tl timescale if it changes:
 

useEffect(() => {
if (tl) {
tl.current.timeScale(speed);
}
}, [speed]);



The function is being called, and passing the if condition, but it causes the timeline to end, rather than changing it's speed.

Link to comment
Share on other sites

OK I've fixed the issue with it breaking. The buttons now work to trigger the function, and if I log out 'tl.current' I can see the '_ts' property (second from bottom in the screenshot) on the timeline updates correctly. It's just that the animation speed does not change:

 

Screenshot 2022-02-09 at 13.39.12.png

Looks like maybe a parent timeline thing... 

 

Link to comment
Share on other sites

I have a function that maps over an array, selects an animation for each item, adds them to a parent timeline within the function, then the function returns that parent timeline and I add that THAT timeline to the master timeline.

Master timeline is at the component level, which is where I have all the code to set the speed.

Appreciate a demo would be handy - but gonna take some time to strip out, i'll work on it :D

Link to comment
Share on other sites

On 2/9/2022 at 11:49 AM, akapowl said:

Hello @PG1

 

Was it maybe something like the latter example on this page?

 

 

 

Here is an example which is a bit more simplified. On click you could either just set the timeScale of the tween to something different to adjust the speed, or even tween on the timeScale of the tween to make the adjustment between speeds smoother. I hope this will help. Happy tweening!

 

 

 

Thanks for this - i've converted it to a react version that reflects the way my project works here: 

See the Pen mdqmMWy by petegarvin1 (@petegarvin1) on CodePen



I set my component up with references to the parent timeline and the box.
I set up a useEffect so that when the speed variable changes (on button Click) it calls the function you suggested to use the new speed. (line 14).

I have movementLeft and movementRight functions that specify a tween and return their own timelines.
I have a generate function that combines these two timelines into another and returns it.

Then on line 45 I add this generate function to the parent timeline. 

At first, clicking any of the speed buttons kicks off the animation fine, at the required speed. But try and chance the speed mid animation by clicking another button and weird things happen.

 

Link to comment
Share on other sites

  • Solution

Taking this example from the 'First Steps & Handy Techniques' guide for GSAP + React...

 

See the Pen eYWGeGe by GreenSock (@GreenSock) on CodePen

 

 

... I was able to change it to something that at least does what you intended.

 

See the Pen jOaBpgm by akapowl (@akapowl) on CodePen

 

See the Pen GROWXop by akapowl (@akapowl) on CodePen

 

 

Now I'm not sure if it follows a similar logic like what you need in the end, but maybe it can serve as some sort of pointer at least.

 

Beyond that I do not have enough experience with React to be of any help with that I'm afraid.

  • Like 2
Link to comment
Share on other sites

Bingo - you legend, thank you so so much. 

You were correct - just by walking through that example I found my parent timeline setup was outside a useEffect - putting it in one fixed everything!! Project working.

You guys are great!

  • Like 2
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...