Anime.js is a JavaScript library used for creating animations. An element's properties like color, shape, size, and position can be animated, but sometimes we might need to animate numerical data such as the progress in a loading bar.
We can animate numerical data by accessing DOM attributes or (object properties if the target is a JavaScript object) as parameters of the anime()
function.
Note: Click here if you don't know how to target objects in anime.js and here if you're unfamiliar with accessing attributes.
For a general overview of anime.js basics, click here.
Since JavaScript doesn't have an integer data type, it represents all numbers as floating point values. Anime.js provides the round property parameter to control the number of decimal places displayed.
The code below describes how to use the round
parameter. The value
is a power of 10 (1, 10, 100, ... ). This value controls how many decimal places to display.
anime({...round: value,...});
The value indicates the nearest position to round to. For example, if the value given is 1, the number is rounded to the nearest one's position. If the value given is 10, the number is rounded to the nearest tenth's position, and so on.
The example shows how to use the round parameter to animate numerical values to different decimal places.
Note: The following example uses the DOM property
innerHTML
to display the numbers. Click here to learn about theinnerHTML
property.
We consider three boxes, each with an increasing number. Each box has more decimal points from left to right than the last.
We create each box as a div
element and create a function to animate each box in the JavaScript section of the code.
round
value to 1. This value represents the 'ones' decimal place. In simple terms, the code will display no digits after the decimal point.round
value to 10
. This value represents the 10
th decimal place. In other words, one digit will be displayed after the decimal point.100
. So decimal places up to and including the 100
th position will be displayed (2 decimal places).