There are many JavaScript animation libraries out there, but Anime.js is one of the best. It’s easy to use, has a small and simple API, and offers everything you could want from a modern animation engine. The library has a small file size and supports all modern browsers, including IE/Edge 11+.
Anime.js is a very lightweight JavaScript animation engine, 14kb minified, and only 6kb gzipped. It supports all modern browsers and can practically animate anything from CSS properties to arbitrary JavaScript values. In this article, we’ll learn the basics of Anime.js and explore how to create the following animation:
This is the animation that we will be creating by the end of the article.
Anime provides a very simple API for animating elements. You start by creating an Anime object, and passing it a plain JavaScript object describing your animation:
const animeObject = anime({ /* describe animation */ });
The following is the anatomy of an animation object:
{ /* Animation Targets: div, .box, #sky, etc... *//* Animatable Properties: height, opacity, color, translateX, etc ... *//* Property Parameters: duration, delay, easing, etc... *//* Animation Properties: loop, direction, autoplay, etc... */}
For example, the following piece of code is all you need to animate a div, with the id of box, 200 pixels across the screen:
anime({ targets: '#box', translateX: 200, });
The snippet above results in the following animation:
Let’s use the snippet above to talk more about the anatomy of an animation object.
translateX: (elm, index, t) => index * 2
The most common animation properties are duration and easing functions:
Let’s look at another example and use more attributes to create an animation with multiple elements:
const boxesAnimation = anime({ targets: '.js-box', translateX: (elm, index, t) => index * 50, scale: 2, easing: 'easeInOutSine', delay: (elm, index, t) => index * 20, duration: 1200, loop: true, direction: 'alternate', });
In the snippet above:
Now let’s put what we have learned together and create the following animation:
First, we need to set the html markup for our page. The following snippet loads Anime.js, the essential CSS, and our JavaScript animation:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/squares.css"> <script src="//cdnjs.cloudflare.com/ajax/libs/animejs/2.2.0/anime.js"></script> <title>Anime Animation</title> </head> <body> <div class="boxes"> <div class="boxes__inner"> <div class="box js-box"></div> <div class="box js-box"></div> <div class="box js-box"></div> <div class="box js-box"></div> <div class="box js-box"></div> <div class="box js-box"></div> <div class="box js-box"></div> <div class="box js-box"></div> <div class="box js-box"></div> <div class="box js-box"></div> <div class="box js-box"></div> </div> </div> <script src="/squares.js"></script> </body> </html>
Then, we need to define a bunch of CSS to prepare the set for our animation. The most important part of this CSS is the .boxes__inner selector. In this selector we are setting the height of the div to a fixed value, and also making the overflow hidden. Essentially this will make the boxes "appear" from the bottom of the outer div, resulting in a nice, smooth effect. The rest of the CSS is pretty self explanatory. We are basically setting a nice background color for the page and centering everything on the page.
CSS:
html, body { padding: 0; margin: 0; background-color: #25275a; width: 100%; height: 100%; } .box { width: 50px; height: 150px; } .boxes { display: flex; align-items: center; justify-content: center; height: 100%; } .boxes__inner { display: flex; overflow: hidden; height: 165px; }
JS
const boxesAnimation = window.anime({ targets: '.js-box', translateY: [150, 50], backgroundColor: (el, i, t) => { const r = 58 + (i * 12); const g = 35 + (i * 12); const b = 220; const color = `rgb(${r}, ${g}, ${b})`; return color; }, duration: 900, easing: 'easeOutElastic', elasticity: 500, delay: (el, i, t) => i * 20, loop: true, direction: 'alternate', });
Now, let’s look at the JavaScript in more detail. In the JavaScript snippet above:
Fine-tunning the Background Color Transition
While the animation above is pretty acceptable, it would be nice to control the duration and transition of the background color transition. In Anime, you can have fine grained control over each property transition by using an object:
anime({ property: { value: ..., transition: ..., duration: ..., /* etc */ } });
For example:
anime({ scale: { value: 2, transition: 'linear', duration: 1000, }, opacity: 1, height: [0, 100], transition: 'easeInOutSine', duration: 500, });
In the snippet above all the properties transition using the easeInOutSine function over 500ms, except the scale property. The scale property will animate linearly over 1000ms. We can apply the same thing to our background transition, and make it a bit more smooth:
const boxesAnimation = window.anime({ targets: '.js-box', translateY: [150, 50], backgroundColor: { value: (el, i, t) => { const r = 58 + (i * 12); const g = 35 + (i * 12); const b = 220; const color = `rgb(${r}, ${g}, ${b})`; return color; }, easing: 'linear', duration: 200, }, duration: 900, easing: 'easeOutElastic', elasticity: 500, delay: (el, i, t) => i * 20, loop: true, direction: 'alternate', });
As you can see, we assigned an object to backgroundColor and moved the function to value instead. And we have also defined a linear easing and a duration of 200ms for each box background color transition. You can see the result below:
Final animation after fine-tuning the background color transition
We saw how easy it is to create smooth animations with couple of lines of JavaScript using Anime.js. There are so many other features packed into Anime.js such as timelines, keyframes and SVG animations. Be sure to check out Anime’s documentation for more details and examples.
Thanks for reading ❤
If you liked this post, please do share/like it with all of your programming buddies!
Follow us on Facebook | Twitter
☞ The Complete JavaScript Course 2019: Build Real Projects!
☞ Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
☞ JavaScript Bootcamp - Build Real World Applications
☞ JavaScript Programming Tutorial - Full JavaScript Course for Beginners
☞ New ES2019 Features Every JavaScript Developer Should Know
☞ Best JavaScript Frameworks, Libraries and Tools to Use in 2019
☞ React vs Angular vs Vue.js by Example
☞ Microfrontends — Connecting JavaScript frameworks together (React, Angular, Vue etc)
☞ Creating Web Animations with Anime.js
☞ Ember.js vs Vue.js - Which is JavaScript Framework Works Better for You
☞ Do we still need JavaScript frameworks?
#javascript #web-development