Introduction to Anime.js
Want to use your web development and JavaScript knowledge to bring out your inner artist? Take at look at Anime.js, a library built to create animations using JavaScript.
Join the DZone community and get the full member experience.
Join For FreeJavaScript animation has been around for a while. There is a wide choice of JavaScript animation libraries for you to try out. Among that group, Anime.js has been gaining some significant popularity. If you’re not familiar with it, I hope this article will get you started with this lightweight and flexible animation library.
This library works with any CSS Properties, individual CSS transforms, SVG, or any DOM attributes, and JavaScript Objects.
Let's create some animations using Anime.js.
Prerequisites
Download the library from GitHub or link it through cdnjs. If you are using npm or bower, you can do as follows:
npm install animejs
OR
bower install animejs
Then in your script file:
import anime from 'animejs'
Let’s add the following code snippet into our HTML <body>:
<div class="purple"></div>
<div class="darkgreen"></div>
<div class="darkred"></div>
Creating Animations
The first step is to initialize the anime object:
anime({
//properties here
});
Within the object above, we will provide certain properties to instruct what and how the elements should animate.
anime({
targets: ['.purple', '.darkgreen', '.darkred'],
translateX: '25rem',
rotate: 360,
borderRadius: '50%',
duration: 3000,
loop: true
});
Here, the 'targets' property defines the elements to animate. The property 'translateX' moves the elements in a horizontal direction. The property 'duration' defines how long the animation lasts.
The 'loop' property runs the animation indefinitely if it is set to true. If it is set to false, it runs just once.
Check out the complete demo on CodePen.
Wrapping Up
Working with Anime.js is easy and fun. It lets you create smooth animations and the API is easy to follow. It offers a lot of cool features. If you wish to explore it further, check out the documentation and examples here. I hope you will enjoy working with Anime.js!
Published at DZone with permission of Swathi Prasad, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
RAML vs. OAS: Which Is the Best API Specification for Your Project?
-
Five Java Books Beginners and Professionals Should Read
-
Database Integration Tests With Spring Boot and Testcontainers
-
Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
Comments