Exploring Web Animations
Tags: gsap, javascript, beginners, webdev
Published: 2022-05-11 23:17:14

## Is it even worth it
I've always wondered how animations are done on websites. I thought this would be so cool to learn, but I was also doubtful about how much of it I would actually use. I did a brief search on what was possible and found instructions on how to make words go big then small, and how to make a newspaper rectangle spin and get smaller until it's gone. I dismissed this idea thinking there is probably something more practical I can learn.

Another day, I gave animations another shot and came across this [article](https://yalantis.com/blog/web-animation-technologies-and-tools/). It showed examples of really cool things you can do: loading, welcoming, scrolling, image galleries, and visual feedback. It also included different libraries, which helped clear up how mysterious where animations come from is. I looked up more libraries and found even more: 

- [GreenSock (GSAP)](https://greensock.com)
- [ScrollMagic](https://scrollmagic.io)
- [Velocity.js](http://velocityjs.org)
- [mo.js](https://mojs.github.io)
- [anime.js](https://animejs.com)
- [vivus.js](https://maxwellito.github.io/vivus/)
- [Popmotion](https://popmotion.io)
- [three.js](https://threejs.org)
- [scrollReveal.js](https://scrollrevealjs.org)
- [WickedCSS](https://kristofferandreasen.github.io/wickedCSS/)
- [tuesday](https://github.com/shakrmedia/tuesday)
- [CSShake](https://elrumordelaluz.github.io/csshake/)
- [Animate Plus](https://github.com/bendc/animateplus)
- [Bounce.js](https://github.com/tictail/bounce.js/blob/master/README.md)
- [Hover.css](https://ianlunn.github.io/Hover/)

There are so many libraries I am overwhelmed. Some seem like they were more oriented towards doing a specialization while others were larger libraries with more options.

I looked up what library people prefer to use, and I found GSAP mentioned a lot so I decided to roll with it. It has a lot of capabilities, and you can also include plugins that do more specific tasks. It seems like I would be able to do a lot of different things with this library if I learned how to use it. There is a lot of documentation with examples of code, visual examples of how the animation looks, and Youtube tutorial videos. They also show a lot of amazing things people have [created](https://greensock.com/showcase/).


## Using GSAP

> “Animation ultimately boils down to changing property values many times per second, making something appear to move, fade, spin, etc. GSAP snags a starting value, an ending value and then interpolates between them 60 times per second.” -[Getting Started with GSAP](https://greensock.com/get-started/)

To include GSAP, add this code to your HTML file before you include your JavaScript file:

```
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.10.4/gsap.min.js"></script>
<script src="index.js"></script>
```

A **tween** is one of the foundations of GSAP. It is basically a line of code with information about how you want to animate an object. Here is their example of what a tween looks like:
`gsap.to(".box", {rotation: 27, x: 100, duration: 1});`

I copied and pasted tweens from GSAP documentation into [CodePen](https://codepen.io/trending), and played around with how the code worked. Every example I show here is from the GSAP documentation JavaScript examples. Some things I changed to match the HTML I made. Here is the first animation I copied, which is **text fading**:

Html:
`<h1>Hello World</h1>`

JavaScript:
`gsap.to("h1", {duration: 5, opacity: 0.3});`

This is a .to() method. The duration is how long it takes to get _to_ that opacity so the opacity starts at 1.0 and goes to 0.3. There are also the .from() and .fromTo() methods. Here is the same example, but with the .from() method. 

JavaScript:
`gsap.from("h1", {duration: 5, opacity: 0.3});`

The duration is how long it takes to get _from_ that opacity so the opacity starts at 0.3 and goes to 1.0.

Here is an example for basic **movement horizontally and vertically**:

- _move left:_
`gsap.to("h1", {duration: 1, x: -100});`

- _move right:_
`gsap.to("h1", {duration: 1, x: 100});`

- _move up:_
`gsap.to("h1", {duration: 1, y: -100});`

- _move down:_
`gsap.to("h1", {duration: 1, y: 100});`

*The y: -100 to move up and y: 100 to move down are not typos. It is negative to go up.

Here is an example of how to make things move in a **sequence**. Whichever line in the JavaScript comes first will move first in this example:

HTML:

```
<h1>First Movement</h1>
<h2>Second Movement</h2>
<h3>Third Movement</h3>
```

JavaScript:

```
var tl = gsap.timeline({repeat: 30, repeatDelay: 1});
tl.to("h1", {duration: 1, x: 200});
tl.to("h2", {duration: 1, x: 200, scale: 0.2});
tl.to("h3", {duration: 1, x: 200, scale: 2, y: 20});
```

When putting in properties, they have to be in camelCase. For example, for "font-size" you would put "fontSize". This is an example with **many properties**. I played around with each property trying to figure out what they do.

```
gsap.to("h1", 2, 
{
    scale: 0.1, 
    y: 60,
    yoyo: true, 
    repeat: -1, 
    ease: "power1.inOut",
    delay:1,
    stagger: 
    {
      amount: 1.5, 
      grid: "auto",
      from: "center"
    }
 });
```

This makes the text yoyo back and forth from the top left towards the bottom right of the screen

- _gsap.to("h1", 2_ : The 2 is the duration. Apparently, you don't need to specify "duration" here. 
- _scale: 0.1_ : The scale is the size of the text.
- _yoyo: true_ : When I made yoyo false, the text did not move back to where it initially started.
- _repeat: -1_ : This makes the movement repeat infinitely. I made repeat a positive 1, and the movement only repeated one time. 
- _ease: "power1.inOut"_ : The ease is the style of the movement. There are different options like elastic, bounce, and sine. There is a [visual display](https://greensock.com/get-started/) if you scroll down to the "Easing" section of how each ease works with a graph of how fast the movement is for each. 
- _delay:1_ : Delay is how long until the movement starts.
- _stagger_ : The amount of stagger is how long the text stays at that point in between each yoyo. I'm not entirely sure what the "grid" and "from" do at this point.

Here is the code I made for a **color changing** background gif I wanted to use as the header image for this post. I published it, and instead of the colors fading, the colors were shattering so I replaced it with just an image.

CSS:
`background-color: #F8D6FF;`

JavaScript:

```
ar tl = gsap.timeline({repeat: 3});

tl.to("h1", 
{
  duration: 1, 
  backgroundColor: "#F8D6FF"
});

tl.to("h1", 
{
  duration: 2,
  backgroundColor: "#D8C3E8"
});

tl.to("h1", 
{
  duration: 2,
  backgroundColor: "#E9E1FE"
});

tl.to("h1", 
{
  duration: 2,
  backgroundColor: "#C3C3E8"
});

tl.to("h1", 
{
  duration: 2,
  backgroundColor: "#D6E1FF"
});

tl.to("h1", 
{
  duration: 1, 
  backgroundColor: "#F8D6FF"
});
``` 

It creates a sequence of transitions from one color to the next, starting with the original background color from the CSS file.

## Looking Forward
These are the first steps to learning how to make fun visualizations. GSAP also has [information](https://greensock.com/react) on how to incorporate it in React. We will be learning that soon so I will have to take a look at that as well. I'm going to start looking into more of their recommendations on what I should know and utilizing their plugins. Eventually, I'd also like to branch out and see how to use other libraries.