Loader Animations Using Anime.js
Anime.js is a lightweight JavaScript library for smooth, customizable animations on CSS, SVG, and DOM elements. It offers flexibility and ease of use.
Join the DZone community and get the full member experience.
Join For FreeAnime.js is a lightweight JavaScript animation library that allows developers to create smooth and powerful animations with ease.
Why Use Anime.js?
- Simple and flexible syntax
- Supports multiple animation types (CSS properties, SVG, DOM attributes, JavaScript objects)
- High performance and lightweight (~17KB gzipped)
- Works well with other libraries and frameworks
Getting Started
Anime.js is a Javascript library. You can download it from the public GitHub or use the CDN URL.
The anime.js method can be used to apply anime js to selected HTML elements. In the code snippet below, select the elements having .css-selector-demo .el
and translate the x-axis to 250px.
<script>
anime({
targets: '.css-selector-demo .el',
translateX: 250
});
</script>
In the official documentation itself, there are a lot of examples available. So, in this article, let's try out some animations that will be useful for website development use cases.
Concepts
- Targets – Elements targeted for CSS animation
- CSS properties – CSS properties causing animation changes
- Keyframes – Defined as an array of animation effects to be done
- Timeline – Synchronizes multiple animations together by giving them the same timelines
- Staggering – Allows you to animate multiple elements with follow-through and overlapping action
- Controls – Plays a paused animation or starts the animation if the autoplay parameter is set to false
- Callback – Callback triggered on every frame as soon as the animation starts playing
- SVG animation – Animates an element relative to the x, y, and angle values of an SVG path element
- Easing – Easing makes the animation smooth
- Helpers – Removes targets from a running animation or timeline. The targets parameter accepts the same values as the targets property.
AJAX Loader With Rotating Circle
While doing an API service call, generally in the UI, an AJAX rotating GIF is shown. This can be easily built using anime.js, like a rotating circle loader.
In the example below, the loader is shown for a few seconds until the back-end API fetches the data. Then, the loader is set to display none, and a list of users is shown.
.loader-container {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.loader-circle {
width: 50px;
height: 50px;
border: 5px solid #3498db;
border-top: 5px solid transparent;
border-radius: 50%;
}
function showLoader() {
document.getElementById("loader").style.display = "block";
anime({
targets: '.loader-circle',
rotate: 360,
duration: 1000,
easing: 'linear',
loop: true
});
}
function hideLoader() {
document.getElementById("loader").style.display = "none";
}
document.getElementById("fetch-data").addEventListener("click", function() {
showLoader();
fetch("api to fetch users data")
.then(response => response.json())
.then(data => {
hideLoader();
})
.catch(error => {
console.error("Error:", error);
hideLoader();
});
});
Output
Circular Animation GIF Effect
In a website, the whole page needs to be loaded from the back-end data; then the loader rotates spirally to span across the pages. In the below snippet, triggerCircularAnimation
uses anime.js to move the circles in a trigonometric path with scaling to have a zoom effect.
#circle-container {
position: relative;
width: 200px;
height: 200px;
margin: 50px auto;
}
.circle {
position: absolute;
width: 20px;
height: 20px;
background: hsl(200, 80%, 60%);
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
opacity: 0;
}
function createCircles() {
const container = document.getElementById("circle-container");
container.innerHTML = '';
for (let i = 0; i < 8; i++) {
const div = document.createElement("div");
div.classList.add("circle");
div.style.width = `${Math.random() * 20 + 10}px`;
div.style.height = div.style.width;
div.style.borderRadius = "50%";
div.style.background = `hsl(${Math.random() * 360}, 80%, 60%)`;
container.appendChild(div);
}
}
function triggerCircularAnimation() {
createCircles();
anime({
targets: '.circle',
rotate: '1turn',
translateX: (el, i) => 80 * Math.cos((i * Math.PI) / 4),
translateY: (el, i) => 80 * Math.sin((i * Math.PI) / 4),
scale: [ 0.8, 1.2, 0.8 ],
opacity: [ 1, 0.5, 1 ],
easing: 'easeInOutSine',
duration: 2000,
delay: anime.stagger(100),
loop: true
});
}
The output will be something like this:
Burst Effect With Different Shapes
To show a surprise gift or score on a website, there will generally be an explosion and a lot of shapes. Using anime.js, different shapes can be produced in different sizes randomly and bring that burst gift effect.
.shapes-container {
position: relative;
width: 200px;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
.shape {
width: 20px;
height: 20px;
position: absolute;
opacity: 0;
}
function createShapes() {
const container = document.getElementById("shapes-container");
container.innerHTML = '';
for (let i = 0; i < 15; i++) {
const div = document.createElement("div");
div.classList.add("shape");
div.style.width = `${Math.random() * 20 + 10}px`;
div.style.height = div.style.width;
div.style.borderRadius = Math.random() > 0.5 ? "50%" : "0";
div.style.background = `hsl(${Math.random() * 360}, 80%, 60%)`;
container.appendChild(div);
}
}
function burstAnimation() {
createShapes();
anime({
targets: '.shape',
opacity: [ 0, 1, 0 ],
translateX: () => anime.random(-100, 100),
translateY: () => anime.random(-100, 100),
scale: [ 0.5, 1.5 ],
easing: 'easeOutQuad',
duration: 1000,
delay: anime.stagger(100)
});
}
setInterval(burstAnimation, 2000);
Output
Grid Effect Animation
Screen transformation, like a kid learning tutorial, is a kind of site use case that requires grid effect animation.
#grid-container {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-template-rows: repeat(5, 1fr);
gap: 10px;
width: 150px;
height: 150px;
}
.grid-item {
width: 20px;
height: 20px;
background: #fff;
opacity: 0;
}
function createGrid() {
const container = document.getElementById("grid-container");
container.innerHTML = '';
for (let i = 0; i < 12; i++) {
const div = document.createElement("div");
div.classList.add("grid-item");
container.appendChild(div);
}
}
function triggerGridAnimation() {
createGrid();
anime({
targets: '.grid-item',
opacity: [ 0, 1 ],
scale: [ 0.5, 1 ],
delay: anime.stagger(100),
duration: 800,
easing: 'easeOutQuad'
});
}
Output
Conclusion
Anime.js is easy to learn and use for JQuery and Javascript developers. It helps us use many animations with an adequate set of customizations. The above article explored the abilities with source code and output preview, which can be a reference for your animation development.
The complete source code is available on GitHub.
Opinions expressed by DZone contributors are their own.
Comments