Delve into the specifics of JavaScript, GSAP's stagger property, and animating SVGs for web development and design. Navigate through the nitty-gritty of setting code properties, handling display issues, and managing animation transformation and repetition to create visually appealing websites.
Key Insights
- The tutorial covers animating SVG (Scalable Vector Graphics), how to use GSAP’s stagger property, and creating animations with minimal code.
- Students work with the GSAP-SVG-Stagger folder, editing the svg-stagger.html file to animate individual elements within the Noble Desktop logo.
- There is a focus on handling display issues, for example when SVG elements are cropped if they exceed the SVG element's boundaries. This is fixed with CSS.
- The tutorial then goes into details of creating a timeline and fixing the display issue by adding specific code to instantiate a new timeline.
- Transforming the origin of animations is explored, allowing for scaling up from the vertical center of an object.
- The tutorial discusses the use of the "stagger" property in animating multiple elements from a single tween, thus creating a wave of animated letters.
- Finally, students learn how to repeat their animations indefinitely, animate other elements of the logo, and fine-tune timing with a position parameter.
Discover how to animate SVGs and use GSAP's stagger property to create effective animations with minimal code through this comprehensive guide to JavaScript.
This exercise is excerpted from Noble Desktop’s JavaScript for Front-End training materials and is compatible with JavaScript updates through 2023. To learn current skills in JavaScript with hands-on training, check out our Front-End Web Development Certificate, Full-Stack Web Development Certificate, and coding classes in-person and live online.
Topics Covered in This JavaScript Tutorial:
Animating SVG, Transform Origin, Stagger: Animating Multiple Elements from a Single Tween
Exercise Preview
Exercise Overview
In this exercise, you’ll learn how to animate SVG (Scalable Vector Graphics) and how to use GSAP’s amazing stagger property to create awesome animations with very little code.
Getting Started
- For this exercise we’ll be working with the GSAP-SVG-Stagger folder located in Desktop > Class Files > JavaScript Class. Open that folder in your code editor if it allows you to (like Visual Studio Code does).
- In your code editor, open svg-stagger.html from the GSAP-SVG-Stagger folder.
-
Preview svg-stagger.html in a browser.
This is the same Noble Desktop logo you’ve see in the previous exercises, but the HTML is set up differently so we can animate the individual elements within the logo, rather than only the logo as a whole.
-
Back in your code editor, let’s get acquainted with the HTML. Notice the following:
-
Instead of an img tag like we used in the previous exercises, we copied and pasted all the SVG code from the.svg file into our HTML. This will allow us to refer to specific parts of it!
FYI, we did not write this SVG code. It was generated by our design app (Adobe Illustrator, Figma, Adobe XD, Sketch, etc.)
- In the SVG you’ll see
<g>
tags, which stands for group. They were created and named in our design app which turned them into IDs when exporting the SVG. -
Notice the various IDs and nested structure of the groups inside the SVG logo:
—Noble Desktop noble (the letters of the word “noble”) desktop (the letters of the word “desktop”)
—n-background (the rounded black rectangle behind the colored n)—n (the colored n)
-
Creating a Timeline & Fixing a Display Issue
-
We’ve already linked this file to the GSAP.js file, so in the script tag at the bottom of the file, add the following bold code to instantiate a new timeline:
<script> let tl = gsap.timeline(); </script>
-
Before we animate, we must see a problem where the SVG elements will be cropped if they go outside the SVG element. To see this, let’s move something with GSAP. To instantly set a property (without animating it), we can use .set (instead of .from and .to that we used before). This does not have a duration as it happens instantly.
Add the following bold code to move the entire word group a bit to the right:
<script> let tl = gsap.timeline(); tl.set('#Noble Desktop', {X:50}) </script>
-
Save and reload the page in the browser.
Notice the right side of desktop is being cut off because it’s protruding beyond the boundaries of the SVG container. Luckily we can fix this easily with CSS.
- Back in your code editor, from the css folder open main.css
-
In the #logo rule, add the following bold code:
#logo { overflow: visible; width: 100%; max-width: 700px; height: auto; }
-
Save and reload the page in the browser.
Now you should be able to see the entire logo, even the part that sticks out of the SVG container.
-
Switch to svg-stagger.html in your code editor and replace the .set line of code with the tween shown below in bold:
<script> let tl = gsap.timeline(); tl.from('#Noble Desktop', {duration:.4, scale:0, X:-5}) </script>
-
Save and reload the page in the browser.
The words are being scaled up (and moved over slightly from left to right).
We don’t like how the scaling is being done from the top (of the left side). It would look better if they were scaled from the vertical center (of the left side).
Transform Origin
-
In your code editor, add the following bold property and don’t miss the comma!
.from('#Noble Desktop', {duration:.4, scale:0, transformOrigin:'left center', X:-5})
NOTE: transformOrigin also accepts percent and pixel values, so these both work:
transformOrigin:'0% 50%'
transformOrigin:'left center'
-
Save and reload the page in the browser.
That looks better with it scaling up from the vertical center (of the left side).
Stagger: Animating Multiple Elements from a Single Tween
The g tag in SVG stands for group. Inside the Noble Desktop group we have 2 groups: noble and desktop (each group contains together the letters for that word). Let’s animate those 2 groups instead of the entire Noble Desktop.
We’ll use the CSS selector for direct descendant >
to target only the groups directly inside Noble Desktop (so it doesn’t target groups within those groups).
-
Back in your code editor, add
> g
as shown below in bold:.from('#Noble Desktop > g', {duration:.4, scale:0, transformOrigin:'left center', X:-5})
-
Save and reload the page in the browser.
Notice how both of the words animate at the same time. While that’s interesting, we want the words to animated one after the other.
Instead of making two different animations in our timeline as we did in the previous exercise, because these will always be the same animations applied to multiple elements sequentially, we can use a stagger.
-
In your code editor, add the following bold code for the stagger property (again don’t miss the comma):
.from('#Noble Desktop > g', {duration:.4, scale:0, stagger:1, transformOrigin:'left center', X:-5})
-
Save and reload the page in the browser.
- The first word animates, there’s a 1 second delay (from the stagger), and then the second work animates.
- That’s cool, but it will be even more impressive when we animation each letter one at a time!
-
In your code editor, reduce the stagger amount to a very short 0.03 seconds:
.from('#Noble Desktop > g', {duration:.4, scale:0, stagger:0.03, transformOrigin:'left center', X:-5})
-
Change
> g
to*
so GSAP targets any element inside the Noble Desktop group (*
is a wildcard that means anything):.from('#Noble Desktop *', {duration:.4, scale:0, stagger:0.03, transformOrigin:'left center', X:-5})
-
Save and reload the page in the browser.
Each letter animates the same way, but with a slight delay between the animations creating a wave of animated letters. Now that’s sweeet!
-
To finish this tween, in your code editor add a back ease (don’t miss the comma!):
.from('#Noble Desktop *', {duration:.4, scale:0, stagger:0.03, transformOrigin:'left center', X:-5, ease:'back.out'})
NOTE: The stagger property can be used on any element, not just SVG like we’re working with in this example.
-
Save and reload the page in the browser.
Easing is a simple way to change the feeling of an animation. This really adds some bubbliness to it.
Repeating the Animation
Let’s make the animation repeat forever, with a slight delay between repeats so you can appreciate the logo for a little while after the animation finishes.
-
Inside the gsap.timeline() method, add the following bold code:
let tl = gsap.timeline( {repeat:-1, repeatDelay:1} );
-
Save and reload the page in the browser.
The animation should now repeat indefinitely.
Animating the Rest of the Logo
The n icon of the logo needs to be animated.
-
Let’s start by scaling up the black rectangle background. Below the first tween, add a second shown below in bold:
.from('#Noble Desktop *', {duration:.4, scale:0, stagger:0.03, transformOrigin:'left center', X:-5, ease:'back.out'}) .from('#n-background', {duration:.3, scale:0, transformOrigin:'right center', ease:'back.out'})
-
Save and reload the page in the browser.
- Ignore the colored part of the n for now, we’ll animate that next.
- Notice the rounded black rectangle (behind the n) now animates after Noble Desktop.
-
All that’s left is the colored parts of the n. We’ll use a stagger so they appear one after the other. Below the last tween, add another as shown below in bold:
.from('#Noble Desktop *', {duration:.4, scale:0, stagger:0.03, transformOrigin:'left center', X:-5, ease:'back.out'}).from('#n-background', {duration:.3, scale:0, transformOrigin:'right center', ease:'back.out'}) .from('#n *', {duration:.2, scale:0, transformOrigin: 'center', stagger:0.03, ease:'back.out'})
-
Save and reload the page in the browser.
The logo animation is now done.
The colored parts of the n should appear sequentially in a quick animation. Isn’t it amazing you can do that part with just one line of GSAP code? And the entire logo animation is only a few lines of code. GSAP is indeed very powerful.
Optional Bonus: Finessing Timing with a Position Parameter
Even though the black rectangle background animates immediately after the Noble Desktop letters are done, it still feels slow. Let’s start the black rectangle animation slightly earlier.
-
Add a position parameter that makes the black rectangle (n-background) start a little earlier using a relative value as shown below in bold. Again do not miss that comma!
.from('#n-background', {duration:.3, scale:0, transformOrigin:'right center', ease:'back.out'}, '-=0.3')
-
Save and reload the page in the browser.
It’s subtle, but nicer with the faster timing. Having the animations slightly overlap can be a nice way to speed up your animations and make them more fluid.