Websites that have eye-catching animations are definitely worth investing in. They ensure a holistic experience that sometimes provides a creative essence of storytelling. But, how can we really make such websites? Well, the secret sauce is GSAP.
What is GSAP?
GSAP (GreenSock Animation Platform) is a highly scalable animation library that enables us to add dynamic effects in web apps, games, and interactive stories. With the help of GSAP, we can animate almost everything on the browser.
Why should we use GSAP?
GSAP has a lot of benefits. Some of them are -
- a super-fast and high-performant animation library;
- comprises myriad of features to create the most complex and fascinating animations
- compatible with every browser, libraries, and frameworks
- has the ability to animate every aspect of an element
- lightweight, expandable, and doesn’t require external tools
In this article, we will focus on three GSAP concepts -
- Tweens
- ScrollTrigger
- Timelines
Installing GSAP
The best way of installing GSAP is via CDN.
We can also use npm
or simply load GSAP via the script tag. To install GSAP, use
Understanding Tweens
In order to create animations with GSAP, the first step would be to implement Tweens. We set the properties that are to be animated, set the duration of the animation, basic sequencing by using delay in tweens. Check here for the complete documentation of tweens.
There are three common methods for creating a Tween:
-
gsap.to()
-
gsap.from()
-
gsap.fromTo()
Let’s see the animation of circle using different methods.
gsap.to()
gsap.from()
gsap.fromTo()
Understanding ScrollTrigger
GSAP ScrollTrigger is one of the several plugins that GSAP comprises. ScrollTrigger enables us to craft scroll-based animations with less code. Check out the ScrollTrigger documentation here.
ScrollTrigger has the ability to take certain actions on animations such as playing, pausing, resetting, reversing, and so on, while entering/leaving the given area or viewport.
To install ScrollTrigger, we need the following code-
We have to register the plugin in our JavaScript file main.js
,
before using it.
This ensures the seamless functioning of the plugin in GSAP.
We have to link the animation to the element
and drive animations based on the location
of the element in the viewport.
This improves the performance of the animation
and ensures that our beautiful animations are clearly visible.
We can also soften the link between the animation
and the scrollbar using
scrub: 1
so that the animation
takes only 1 second to catch up.
This is how we use ScrollTrigger-
There are several other things
we can do with ScrollTrigger.
We can snap to
certain points in the animation depending on
the velocity using get.velocity()
,
define scroll positions flexibly,
and so on.
Understanding Timelines
Timelines in GSAP is a powerful sequencing tool that makes our job a lot easier, as compared to using Tweens for sequencing. It allows us to control different tween or other timelines in a complete manner, thereby, providing us better control of every tweens timing corresponding to other tweens of the same timeline.
Without Timelines, sequencing would have been a lot
more difficult as we would have had to use delay()
for every animation.
Whether we want to pause, reverse or repeat the sequence, we can do it all at once using Timelines. Look at the code below for better understanding-
Real-world example of using GSAP
Recently, at Saeloun, we revamped our website and used GSAP library to animate different elements to make the page look even more appealing. Look at the video below. The moving petals are the elements that have been animated through GSAP library.
Now, let’s go over the process.
As per the width and height,
random
calculates a number between minimum
to maximum
and assigns that number to the petal(GSAP animation).
Now, we have to transfer the elements from HTML file. Next, we control the animation(petals) in various ways. We set the origins and define the values to spin the elements in the desired pattern.
Final Thoughts
Using GSAP isn’t that tough. All we need is a basic knowledge of HTML, CSS, CSS transitions, and JavaScript. The codes and concepts are easy enough to understand even if one has never used GSAP before.