Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to add animation in Text using FabricJS?
In this tutorial, we are going to learn how to add animation in Text using FabricJS. We can display text on canvas by adding an instance of fabric.Text. Not only does it allow us to move, scale and change the dimensions of the text but it also provides additional functionality like text alignment, text decoration, line height which can be obtained by the properties textAlign, underline and lineHeight respectively. Similarly, we can also animate text by using the animate method.
Syntax
animate(property: String | Object, value: Number | Object)
Parameters
property ? This property accepts a String or Object value which determines which properties we want to animate.
value ? This property accepts a Number or Object value which determines the values to animate properties
Example 1: Default Text Object
Let's see a code example to see how our text object looks when the animate method is not used. In this case, no animation is displayed.
<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
<h2>Default appearance of the Text object</h2>
<p>You can see that the text has no animation</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiate a text object
var text = new fabric.Text("Sparse is better than dense!", {
width: 300,
left: 50,
top: 70,
fill: "green",
});
// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>
Example 2: Using the animate Method
In this example, we will see how by using the animate property we can easily create our own animation. The first argument is the property we want to animate. Here, for example, we have used the stroke property as an argument in order to change its colour. This property also allows us to use relative values just as we have specified the left value as +=100 which makes the text move.
<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
<h2>Using the animate method</h2>
<p>You can see the animation now</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiate a text object
var text = new fabric.Text("Sparse is better than dense!", {
width: 700,
left: 50,
top: 70,
fill: "#ccccff",
stroke: "black",
strokeWidth: 2,
fontSize: 25
});
// Using the animate method
text.animate('stroke', '87ceeb', {
onChange: canvas.renderAll.bind(canvas)
})
text.animate('left', '+=100', { onChange: canvas.renderAll.bind(canvas) });
// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>
Key Points
- The
animatemethod accepts property name and target value as arguments - Use
onChange: canvas.renderAll.bind(canvas)to ensure the canvas updates during animation - Relative values like
'+=100'can be used for smooth transitions - Multiple properties can be animated simultaneously by calling
animatemultiple times
Conclusion
FabricJS provides a powerful animate method to create smooth text animations. By combining different properties like position, color, and stroke, you can create engaging visual effects for your text objects.
