Web Development

Animations Made Easy with GSAP for Beginners

5 mins | Jul 30, 2024

Animations Made Easy with GSAP for Beginners

Introduction

Websites with beautiful animations are an eye candy, providing an immersive experience that sometimes tells a creative story. The GreenSock Animation Platform (GSAP) is a feature-rich animation library that allows developers to create dynamic effects in web apps, games, and interactive stories. With GSAP, anything can be animated on the browser.

Getting started with GSAP can be an exciting venture into the world of web animations. Renowned for its performance and flexibility, GSAP powers animations on numerous high-profile sites. However, beginners might find the vast features and capabilities of GSAP a bit daunting. Here's a guide to help you start your animation journey with GSAP smoothly, addressing common challenges and offering effective solutions.


Understanding GSAP's Rich Ecosystem

GSAP consists of four main classes and numerous plugins, offering over a hundred methods to create, inspect, and control animations. For a beginner, this can seem overwhelming. Focus initially on the core functionalities:

  • TweenLite: A lightweight version of TweenMax, perfect for simple animations.
  • TweenMax: Includes more features and plugins.
  • TimelineLite: Helps in sequencing multiple animations.
  • TimelineMax: An extended version of TimelineLite with additional features.

Use the official GSAP documentation (https://gsap.com/docs/v3/GSAP/Tween/) to understand what each class and method does.


Start Small and Simple

A common mistake is to dive into complex projects too early. Beginners often envision integrating advanced animations with elements like ScrollMagic, SVGs, or 3D effects right away. Begin with basic animations, try simple tasks like animating a box moving across the screen or fading in an element. These small projects are easier to manage and debug. For instance, animating a single DOM element’s opacity or position can teach you a lot about how GSAP handles timing and easing.


Use Available Learning Resources

GSAP offers a variety of learning materials tailored for beginners, including a Getting Started guide, introductory videos, and the Jump Start tutorial. Make full use of these resources. The Getting Started video is particularly helpful in visualizing how GSAP works. Also, platforms like CodePen are invaluable for exploring existing projects and practicing your skills by modifying those projects.


Starting with GSAP Coding


Installing GSAP

There are many ways to install GSAP. The easiest way is to use a CDN. To load GSAP into your page, use the following script tag:


Creating Tweens

Tweens are the basis for creating animations in GSAP. We set the properties we want to animate, the duration of the animation, the animation's easing, and other properties like delay using tweens. There are three common methods for creating a Tween:

  1. gsap.to()
  2. gsap.from()
  3. gsap.fromTo()


Example


HTML:


CSS:

.box {

 width: 50px;

 height: 50px;

 margin-top: 20px;

 background: tomato;

}

gsap.to()

When using this method, GSAP automatically sets the initial values based on the set CSS properties of the given element.


```javascript

gsap.to(".box", {

 borderRadius: 50,

 x: 100,

 scale: 1.5,

 duration: 1

});

```

gsap.from()

The from() method is best used for reverse animation. When you set the initial CSS properties, GSAP will animate to the default values.


```javascript

gsap.from(".box", {

 y: 80,

 opacity: 0,

 duration: 2

});

```

gsap.fromTo()

With the fromTo() method, developers can define both the start and end properties of a given animation.

```javascript

gsap.fromTo(".box", {

 x: 20,

 borderRadius: 10

}, {

 x: 80,

 borderRadius: 50,

 duration: 1

});

```

Creating Timelines

To achieve a more concise animation, where you define exactly which animation comes first, you can chain the to() method and list the animations (tweens) in the order that you want them. First, create a timeline:


var tl = gsap.timeline({repeat: 30, repeatDelay: 1});

Then, add each of your tweens to the already created timeline:

```javascript

tl.add(gsap.to("#box", {duration: 1, x: 100}));

tl.add(gsap.to("#box2", {duration: 2, x: 100, scale: 1.1}));

tl.add(gsap.to("#box3", {duration: 3, x: 100, scale: 2}));

```

Basic GSAP Example:


Part 1: Basic GSAP Code

In the first example, we use GSAP to animate elements individually with specific properties like duration, delay, translation (x and y), easing, and staggering. Here is the basic code:

https://codesandbox.io/p/devbox/wx658g?file=%2Findex.html


// Basic GSAP Animations

gsap.from('.header', { duration: 1, y: '-100%', ease: 'bounce' });

gsap.from('.link', { duration: 1, opacity: 0, delay: 1, stagger: 0.5 });

gsap.from('.right', { duration: 1, x: '-100vw', delay: 1, ease: 'power2.in' });

gsap.from('.left', { duration: 1, delay: 1.5, x: '-100%' });

gsap.to('.footer', { duration: 1, y: 0, ease: 'elastic', delay: 2.5 });

gsap.fromTo('.button', { opacity: 0, scale: 0, rotation: 720 }, { duration: 1, delay: 3.5, opacity: 1, scale: 1, rotation: 0 });


Explanation

  • Staggering: Allows animating a group of elements with delays between each animation, creating a sequential or overlapping effect.
  • Duration: Defines the time an animation takes to complete.
  • TranslateY (y) and TranslateX (x): Move the element along the Y-axis and X-axis respectively.
  • Ease: Specifies the speed curve of the animation.
  • Delay: Adds a start time for the next animation.

In this example, each animation has a delay to ensure they start one after another. For instance, if we set a duration of 2 seconds for an animation, we must add a 2-second delay to the next animation; otherwise, the second animation will start before the first one is completed. Manually adding delays, however, is not efficient for longer sequences.


Part 2: Using Timelines

Timelines allow you to chain animations together, making it easier to control the sequence and timing without manually adding delays.


// Using GSAP Timelines

const timeline = gsap.timeline({ defaults: { duration: 1 } });

timeline

  .from(".header", { y: "-100%", ease: "bounce" })

  .from(".link", { opacity: 0, stagger: 0.5 })

  .from(".right", { x: "-100vw", ease: "power2.in" }, 1)

  .from(".left", { x: "-100%" }, "<-2")

  .to(".footer", { y: 0, ease: "elastic" })

  .fromTo(

    ".button",

    { opacity: 0, scale: 0, rotation: 720 },

    { opacity: 1, scale: 1, rotation: 0 }

  );

const button = document.querySelector(".button");

button.addEventListener("click", () => {

  timeline.timeScale(3); // 3 times faster

  timeline.reverse();

});


Explanation

  • Timeline Defaults: Sets default properties (like duration) for all animations in the timeline.
  • Chaining Animations: Allows for a seamless sequence of animations without manually setting delays.
  • Ease and Stagger: Remain effective, providing smooth and dynamic animation flows.
  • Control Animations: By adding event listeners, you can control the timeline's playback, such as speeding up or reversing the animations.


Some more examples:

1) Scroll trigger - https://codesandbox.io/p/devbox/scroll-trigger-h4qqyy 

2) Scroll To - https://codesandbox.io/p/devbox/scroll-to-y68hym 


Using GSAP Plugins

To use GSAP plugins like ScrollTrigger, TextPlugin, etc., you must first register those plugins in your JS file or script tag.

gsap.registerPlugin(ScrollTrigger, TextPlugin);


To see basic ScrollTrigger animation you can see the example in below links:

https://www.youtube.com/watch?v=X7IBa7vZjmo&t=614s


Using GSAP in React


To use GSAP in React, first install it using the command:

npm i gsap

Initially, you had to use `useEffect` and `useRef` for GSAP animations, but now GSAP has introduced its own hook called `useGSAP`, which handles animation and its cleanup, so you don’t have to do it manually like before. Use the official GSAP documentation (https://gsap.com/docs/v3/GSAP/Tween/) to understand more about GSAP with react. https://gsap.com/resources/react-basics/

Learn from Mistakes

Copying and pasting code from tutorials or other developers’ projects is a common starting point. However, this approach can limit deeper learning opportunities provided by troubleshooting errors. Type out the code yourself. This practice helps familiarize you with GSAP’s syntax and nuances. When errors occur—missing a comma or a semicolon, or forgetting to close curly braces—fixing them teaches you about the structure and requirements of the code, which is invaluable for mastering GSAP.



Conclusion

Starting with GSAP doesn't have to be intimidating if you approach it step by step. Focus on learning the basics well, leverage the abundant resources available, and engage in practical, hands-on projects. By understanding the common pitfalls and how to navigate them, you’ll find that GSAP is an immensely powerful tool in your web development arsenal, capable of bringing any webpage to life with captivating animations

Author

Sanyam Sharma
Sanyam Sharma
Jr. UI/UX Developer

Share

Share

Related

The Hidden Benefits of Web Applications You Need to Know
Web Development

The Hidden Benefits of Web Applications You Need to Know

7 mins : Sep 16, 2023

Web Development & Design Company in Dubai
Web Development

Web Development & Design Company in Dubai

5 mins : Nov 09, 2023

14 Web Development Trends You Must Watch Out In 2024
Web Development

14 Web Development Trends You Must Watch Out In 2024

5 mins : Jan 08, 2024

Other Articles

Discover the Benefits of ReactJS for Dynamic Web Development
Web Development

Discover the Benefits of ReactJS for Dynamic Web Development

5 mins : Aug 05, 2023

The Art of Crafting a Professional Website for Lawyers and Law Firms.
Web Development

The Art of Crafting a Professional Website for Lawyers and Law Firms.

5 mins : Aug 14, 2023

Top 9 Web Development Companies in Banglore You Must Know!
Web Development

Top 9 Web Development Companies in Banglore You Must Know!

4 mins : Nov 17, 2023

Instagram
Instagram Instagram
Youtube
Youtube Youtube
LinkedIn
LinkedIn LinkedIn
Socials

Let's Work Together

© 2024 All Rights Reserved.
Privacy policyTerms of use

Resources

Articles

A-103, 105, Jai Estate, Mumbai. India - 421203.

Resources / Articles / Services / Portfolio