Easings are mathematical functions that define the rate of change in animation progress. Anime.js provides many in-built easings, but we can also define custom easings.
Custom easings are made as a function-based value. We return another function that takes time
as a parameter within the function-based value.
Note: An understanding of function-based values is necessary to understand custom easings. Function-based values and their parameters:
targets
,index
, andlength
are explained here.
The syntax is as follows:
anime({
...,
easing: function (target, index, length) {
return function (time) {
return ...;
};
}
});
The time
parameter represents the current time of the animation. This parameter ranges from
The value returned by the nested function is the progress of the animation ranging from
Generally, we define an easing function
In other words, at time
The example below shows how to create a custom easing function. We make a 'dribbling' effect by using the trigonometric function given below:
This equation generates the following progress vs. time graph:
The code below describes how to use this formula as an easing:
In the HTML section, we create a box using a div
element and a box
class. In the JavaScript section, we create the animation using the anime()
function.
box
class.tranlsateX
property moves the box right to 500px, the duration
is set to 1000 milliseconds, and the delay
before the start of the animation is set to 500 milliseconds.easing
parameter to a function-based value. Inside this function-based value, we return a function that takes time
as a parameter. This nested function returns the trigonometric function that we have defined above.