- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 appearance of the 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>