Jump to content
Search Community

updating variable via loop

ericshew 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

Hi there,

 

I'm trying to create a timeline that switches out a headline through an animation. In the case of the codepens below, I just want it to run through hello, how are you, so long.

 

The problem is that I'm running into issues setting variables. 

 

Here is my first attempt to do this. It involved calling a function after the loop is completed.

 

 

 

See the Pen Jrjrgg by ericshew (@ericshew) on CodePen

 

 

Here is my second, which involves doing a series of actions within the loop itself.

 

See the Pen JrjOKo by ericshew (@ericshew) on CodePen

 

Would welcome any help on this one. Thank you in advance.

See the Pen Jrjrgg by ericshew (@ericshew) on CodePen

Link to comment
Share on other sites

Thanks for the demos. The trouble with your approach is that the timeline is only created once and the value it gets for the text when it first executes is recorded and re-used each time it plays back. The timeline has no idea that you are changing the value of index and it isn't ever trying to re-evaluate what textArray[index] is. That expression only gets evaluated when the timeline is created. 

 

From what I understand of the end result you are going for I would suggest looping through the array and for each word in the array add a set() and to() tween to the timeline so that each word can have its own animations in the timeline. You'll have great advantages in that the animation can be paused, reversed, sped up or slowed down.

 

See if this works for you

var 
  text = jQuery("h1"),
  textArray = ['hello', 'how are you', 'so long']
  tl = new TimelineMax({repeat: -1});
  
textArray.forEach(function(element, index) {
  console.log(index, element)
   tl.set(text, {text:textArray[index]})
      .to(text, 1, {opacity:1, repeat:1, yoyo:true, repeatDelay:0.5}) 
});

 

See the Pen GMROVd?editors=0010 by GreenSock (@GreenSock) on CodePen

 

  • Like 3
Link to comment
Share on other sites

  • 3 years later...

Hi Oracles,

 

What I am trying to do is a translation effect, once you click on the botton the animation will reverse back and will come up again with the new text. But I have two problems, the variables are not being updated on both of them. 

 

Problem A;   width:textWidth is not getting updated

 

let introTl = gsap.timeline();
introTl.to('.box', {duration: 1width: textWidthdelay: 0.5ease: 'power4.inOut'})
.from('.hi', {duration: 1y:'7vw'ease:'power3.out'onComplete: ()=>{masterTl.play(); cursor.play()}}).from('p', {opacity:0})

 

Problem B;   text: word is not getting updated

let masterTl = gsap.timeline({repeat:-1}).pause();
words.forEach(word => {
    let wordTl = gsap.timeline({yoyo:truerepeat:1repeatDelay:1});
    wordTl.to('.text', {duration:1, text: word})
    masterTl.add(wordTl)
})

 

See the Pen poeNjva by mayconcabral (@mayconcabral) on CodePen

 

 

 

 

 

 

Link to comment
Share on other sites

Hi Oracles,

 

The last post I said “update the variables”,  sorry for that, actually is not variables but properties! 


At the problem B

I tried to kill masterTl with the method .kill( ), then I made wordTl globally to be able to kill it as well. RESULT: I could update the values!!! But the old values still coming up, now every time when I click on those buttons it is coming with a new and old values, it is accumulating. 

 

At the problem A

Using the method .kill( ) it didn’t  work. I tried to apply the same tactical plan as I did with problem B. RESULT: Everything disappeared… 

 

Down below is the new code with .kill( ) method.

 

See the Pen wvJojYO by mayconcabral (@mayconcabral) on CodePen

 

The solution I am look for: When the button is clicked, the animation will reverse and it will come up again with the new text.

 

Link to comment
Share on other sites

Hi @Mikemcfly

 

Can you create a new topic instead of replying to an old one? Thanks!

 

But I'll just quickly go over some of your issues.

 

11 hours ago, Mikemcfly said:

The last post I said “update the variables”,  sorry for that, actually is not variables but properties! 

 

Properties aren't dynamic like that. It's impossible for gsap to detect when you change the textWidth here.

introTl.to('.box', {duration: 1, width: textWidth, delay: 0.5, ease: 'power4.inOut'})

 

If the textWidth changes, you should recreate that animation. Using functions to create all your animations can greatly simplify this.

 

11 hours ago, Mikemcfly said:

I tried to kill masterTl with the method .kill( ), then I made wordTl globally to be able to kill it as well

 

Kill doesn't reset the timeline. It's usually best to do something like this if you want to revert everything.

tl.progress(0).kill();

tl = gsap.timeline();

// or clear if you want to reuse it
tl.progress(0).clear();
tl.to(...);

 

And you have some setTimeouts that could probably led to some problems. 

 

Here's how I would approach this. I would not have a some master timeline. I would create all related animations inside functions. That function returns a function to cleanup the current animation when it's time for a new one to play.

 

See the Pen 855770d485d460ec9280da133ec86d9a by osublake (@osublake) on CodePen

 

 

 

  • Like 3
  • Thanks 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...