Performant Animations Using KUTE.js: Part 1, Getting Started

KUTE.js is a JavaScript-based animation engine which focuses on performance and memory efficiency while animating different elements on a webpage. I have already written a series on using Anime.js to create JavaScript-based animations. This time we will learn about KUTE.js and how we can use it to animate CSS properties, SVG, and text elements, among other things.


Before we dive into some examples, let’s install the library first. KUTE.js has a core engine, and then there are plugins for animating the value of different CSS properties, SVG attributes, or text. You can directly link to the library from popular CDNs like cdnjs and jsDelivr.

You can also install KUTE.js using either NPM or Bower with the help of the following commands:

Once you have included the library in your projects, you can start creating your own animation sequences.

Tween Objects

When creating your animation using KUTE.js, you need to define tween objects. These tween objects provide all the animation-related information for a given element or elements. This includes the element itself, the properties that you want to animate, the duration of the animation, and other attributes like the repeat count, delay, or offset.

You can use the .to() method or the .fromTo() method in order to animate a set of CSS properties from one value to another. The .to() method animates the properties from their default value or their computed/current value to a final provided value. In the case of the .fromTo() method, you have to provide both the starting and ending animation values.

The .to() method is useful when you don’t know the current or default value for the property that you want to animate. One major disadvantage of this method is that the library has to compute the current value of all the properties by itself. This results in a delay of a few milliseconds after you call .start() to start the animation.

The .fromTo() method allows you to specify the starting and ending animation values yourself. This can marginally improve the performance of the animations. You can now also specify the units for starting and ending values yourself and avoid any surprises during the course of the animation. One disadvantage of using .fromTo() is that you won’t be able to stack multiple transform properties on chained tweens. In such cases, you will have to use the .to() method.

Remember that both .fromTo() and .to() are meant to be used when you are animating individual elements. If you want to animate multiple elements at once, you will have to use either .allTo() or .allFromTo(). These methods work just like their single element counterparts and inherit all their attributes. They also get an extra offset attribute that determines the delay between the start of the animation for different elements. This offset is defined in milliseconds.

Here is an example that animates the opacity of three different boxes in sequence.

The following JavaScript is used to create the above animation sequence:

All the boxes above have a box class which has been used to select them all using the querySelectorAll() method. The allFromTo() method in KUTE.js is used to animate the opacity of these boxes from 1 to 0.1 with an offset of 700 milliseconds. As you can see, the tween object does not start the animation by itself. You have to call the start() method in order to start the animation.

Controlling the Animation Playback

In the previous section, we used the start() method in order to start our animations. The KUTE.js library also provides a few other methods that can be used to control the animation playback. 

For example, you can stop any animation that is currently in progress with the help of the stop() method. Keep in mind that you can use this method to stop the animation of only those tween objects that have been stored in a variable. The animation for any tween object that was created on the fly cannot be stopped with this method.

You also have the option to just pause an animation with the help of the pause() method. This is helpful when you want to resume the animation again at a later time. You can either use resume() or play() to resume any animation that was paused.

The following example is an updated version of the previous demo with all the four methods added to it.

Here is the JavaScript code needed to add the start, stop, play, and pause functionality.

I have changed the animation duration to 2,000 milliseconds. This gives us enough time to press different buttons and see how they affect the animation playback.

Chaining Tweens Together

You can use the chain() method to chain different tweens together. Once different tweens have been chained, they call the start() method on other tweens after their own animation has finished. 

This way, you get to play different animations in a sequence. You can chain different tweens with each other in order to play them in a loop. The following example should make it clear:

We already had one tween to animate the opacity. We have now added another one that animates the rotation of our boxes. The first two buttons animate the opacity and the rotation one at a time. The third button triggers the chaining of animateOpacity with animateRotation

The chaining itself doesn’t start the animation, so we also use the start() method to start the opacity animation. The last button is used to chain both the tweens with each other. This time, the animations keep playing indefinitely once they have been started. Here is a CodePen demo that shows all the above code in action:

To fully understand how chaining works, you will have to press the buttons in a specific sequence. Click on the Animate Opacity button first and you will see that the opacity animation is played only once and then nothing else happens. Now, press the Animate Rotation button and you will see that the boxes rotate once and then nothing else happens.

After that, press the Chain Animations button and you will see that the opacity animation plays first and, once it completes its iteration, the rotation animation starts playing all by itself. This happened because the rotation animation is now chained to the opacity animation. 

Now, press the Animate Opacity button again and you will see that both opacity and rotation are animated in sequence. This is because they had already been chained after we clicked on Chain Animations.

At this point, pressing the Animate Rotation button will only animate the rotation. The reason for this behavior is that we have only chained the rotation animation to the opacity animation. This means that the boxes will be rotated every time the opacity is animated, but a rotation animation doesn’t mean that the opacity will be animated as well. 

Finally, you can click on the Play in a Loop button. This will chain both the animations with each other, and once that happens, the animations will keep playing in an indefinite loop. This is because the end of one animation triggers the start of the other animation.

Final Thoughts

In this introductory KUTE.js tutorial, you learned about the basics of the library. We started with the installation and then moved on to different methods that can be used to create tween objects. 

You also learned how to control the playback of an animation and how to chain different tweens together. Once you fully understand chaining, you will be able to create some interesting animations using this library.

In the next tutorial of the series, you will learn how to animate different kinds of CSS properties using KUTE.js.

Powered by WPeMatico

Leave a Comment

Scroll to Top