Jump to content
Search Community

Looping through children of a SVG <g>

athuillier 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

Hello,

 

I've created a set of 9 animations triggered by pressing keyboard keys [QWE][ASD][ZXC].

My question is about the animation triggered when pressing "C" key (i.e. the 14 small colorful rectangles arriving on the canvas).

 

These elements are SVG <rect> grouped under a <g id="confettis">.

I defined a function playConfettis() where I randomly move each <rect>. Here is a simplified version of this function :

 

function playConfettis() {
  tl.set(theConfettis, { rotation:0, x:0, y:0 })
  .to(theConfetti1, .3, { rotation:rand(-360,360), x:rand(-500,500), y:rand(0,-500) })

  .to(theConfetti2, .3, { rotation:rand(-360,360), x:rand(-500,500), y:rand(0,-500) })

  ...AND SO ON FOR EACH 

}

 

I guess my code is not optimized, but I couldn't figure out how to loop through all those <rect>.

 

Thank in advance for your suggestions :)

 

 

See the Pen JpLvbM by athuillier (@athuillier) on CodePen

  • Like 1
Link to comment
Share on other sites

You were using really old version of TweenMax which wouldn't work even though you used correct code.

 

You can use a staggerTo tween to add delay to subsequent tweens and to use random values on each tween you can pass a function for each property. So GSAP will loop through and get random values for each tween. BTW cool demo.

 

See the Pen YejdpV?editors=0010 by Sahil89 (@Sahil89) on CodePen

 

  • Like 5
  • Thanks 1
Link to comment
Share on other sites

Hi @athuillier :)

 

Welcome to the forum.

 

In addition to @Sahil's excellent advice, I'd add that if you do want to loop through a group, it's easy to do so. Here's a basic example:

 

See the Pen NyBEZY by PointC (@PointC) on CodePen

 

Using that same technique, you could add all those individual animations to a master timeline for complete control.

 

Happy tweening.

:)

 

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