What are property keyframes in anime.js?
Anime.js is a Javascript library that lets us easily create animations. To make complicated animations, we can break down the animation into multiple keyframes or steps where each frame is responsible for some visual effect.
Animation keyframes execute these frames one after the other. On the other hand, property keyframes allow frames to overlap to produce more exciting effects.
Syntax
Similar to animation keyframes, we provide keyframes as a list of objects. However, each property in the animation is provided with its list of keyframes.
The code below describes the syntax:
anime({
...
property: [
{value: some_value, property parameter: some_value},
{value: some_value, property parameter: some_value},
],
property: [
{value: some_value, property parameter: some_value},
{value: some_value, property parameter: some_value},
],
...
});
Here, the property is any valid anime.js property, and we provide it with a list of objects. Each object is a keyframe.
The value key in each keyframe is the value for the property. For example, for the translateX property, some_value represents the pixel position to move horizontally. For the rotate property, some_value represents the degree of rotation.
Any property parameter can optionally accompany the value key.
Note: The
duration,delay, andendDelayproperty parameters are essential for creating animations using property keyframes.
Rules of overlap
To create animations using property keyframes, we must first understand some rules. We explain each rule using an example.
The keyframes for a single property are executed one after the other.
anime({
...
translateX: [
{value: 500}, //keyframe 1
{value: 0}, //keyframe 2
{value: 500}, //keyframe 3
],
...
});
We provide the translateX property with three frames. First, it moves horizontally to
If no
delayparameter is specified, the first keyframe for all properties starts simultaneously.
anime({
...
translateX: [
{value: 500}, //keyframe 1 of translateX property
],
translateY: [
{value: 150}, //keyframe 1 of translateY property
],
...
});
Here we have two properties: translateX and translateY, each with a single keyframe. Since neither frame has the delay property, the frames of both properties start simultaneously when the animation begins. Hence, we see a diagonal motion.
If no
durationparameter is specified, the total duration is divided equally amongst the keyframes of each property.
anime({
...
translateX: [
{value: 500}, //duration = 3000/3 = 1000
{value: 0}, //duration = 3000/3 = 1000
{value: 500}, //duration = 3000/3 = 1000
],
translateY: [
{value: 150}, //duration = 3000/2 = 1500
{value: 0}, //duration = 3000/2 = 1500
],
duration: 3000,
...
});
Here, we provide the translateX property with three keyframes and the translateY property with two keyframes. We set the animation's duration to translateX has three frames, each frame gets a duration of translateY gets a duration of
If some frames have a duration while others don't, the division still occurs in the same manner. However, frames will give precedence to the duration parameter specified for them.
We can use the timing-related parameters
duration,delay, andendDelayto control how and when the different keyframes overlap.
anime({
...
translateX: [
{value: 500, duration: 1000},
],
translateY: [
{value: 150, duration: 500, delay: 1000},
],
...
});
In the above animation, the horizontal movement caused by the translateX property does not overlap with the vertical movement caused by translateY.
This is because the translateX keyframe has a duration of translateY keyframe has a delay of
Example
We can now use the above rules to create an animation with multiple overlapping keyframes.
In the example below, we animate a box to do the following:
Move right
Stop, spin and change color
Move right again
Stop, spin and change color
Explanation
In the HTML section, we create a box using a <div> tag and style it using the box class.
In the JavaScript section, we animate the box using the anime() function.
Line 2: We target the box using its class.
Lines 3–6: We create a list of keyframes for the
translateXproperty.Line 4: We define the first keyframe with a
valueofpx, a durationofms, and a delayofms. Since this is the first frame, the delayis relative to the start of the animation.Line 5: We define the second keyframe with a
valueofpx, a durationofms, and a delayofms. Since this isn't the first frame, the delayis relative to the end of the previous frame in the list.
Lines 7–10: We create a list of keyframes for the
rotateproperty.Line 8: The first keyframe is given a
valueofdegrees, a durationofms, and a delayofms. It is the first frame, the delayis relative to the start of the animation.Line 9: The second keyframe is created with a negative
valueto make it spin in the opposite direction. Thedelayis set toms. Since this isn't the first frame in the list, the delayis relative to the end of the previous frame.
Lines 11–14: Similarly, we create a list of keyframes for the
backgroundColorproperty. Since we want this property to overlap precisely with therotateproperty, their frames have the samedurationanddelayas therotateframes.
The illustration below summarizes each frame's order of execution.
Free Resources