- 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 set the duration of animation in Text using FabricJS?
In this tutorial, we are going to learn how to set the duration of an animated 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 change the duration of animated text by using the duration property.
Syntax
animate(property: String | Object, value: Number | Object, { duration: Number })
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.
Options Keys
duration − This property accepts a Number. It can be used to change the duration of an animation. The default value is 500ms.
Example 1
Using default value of duration property
Let’s see a code example to see how our text object looks when the animate method is used in conjunction with the default value of duration property. In this case, animation will have a duration of 500ms.
<!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 default value of duration property</h2> <p>You can see that the skewY animation occurs for 500ms</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("ADD SAMPLE
text here.", { width: 300, left: 60, top: 70, fill: "#ccccff", stroke: "black", strokeWidth: 2, fontSize: 50, }); // Using the animate method text.animate("skewY", "-=10", { onChange: canvas.renderAll.bind(canvas), duration: 500, }); // Add it to the canvas canvas.add(text); </script> </body> </html>
Example 2
Using the animate method and passing the duration option
In this example, we will see how by using the animate property along with the duration option, we can control the duration of the animation. In this case, we have mentioned the duration as 2000ms which is why the skewY animation occurs for 2000ms.
<!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 and passing the duration option</h2> <p>You can see that the skewY animation occurs for 2000ms</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("ADD SAMPLE
text here.", { width: 300, left: 60, top: 70, fill: "#ccccff", stroke: "black", strokeWidth: 2, fontSize: 50 }); // Using the animate method text.animate('skewY', '-=10', { onChange: canvas.renderAll.bind(canvas), duration: 2000 }); // Add it to the canvas canvas.add(text); </script> </body> </html>