- 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 on a Line using FabricJS?
In this tutorial, we are going to learn how to set the duration of animation on a Line using FabricJS. A Line element is one of the basic elements provided in FabricJS. It is used for creating straight lines. Because line elements are geometrically one-dimensional and do not contain an interior, they are never filled. We can create a line object by creating an instance of fabric.Line, specifying the x and y coordinates of the line and adding it to the canvas. In order to change the duration of animated text we use the duration property.
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.
Options Keys
duration − This property accepts a Number. It can be used to change the duration of an animation. The default value is 500ms.
Using default value of duration property
Example
Let’s see a code example to see how our line object looks when the animate method is used in conjunction with the default value of duration property. In this case, the left 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 left animation happens 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 Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, }); // Using the animate method line.animate("left", "+=100", { onChange: canvas.renderAll.bind(canvas), duration: 500, }); line.animate("angle", "90", { onChange: canvas.renderAll.bind(canvas), }); // Add it to the canvas canvas.add(line); </script> </body> </html>
Using a custom value for duration property
Example
In this example, we will see how by using the animate method along with the duration property, we can control the time for which an animation occurs. Here, we have specified the duration as 1000ms which means, our left animation will occur for 1 second.
<!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 a custom value for duration property</h2> <p>You can see that the left animation happens for 1000ms</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 Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, }); // Using the animate method line.animate("left", "+=100", { onChange: canvas.renderAll.bind(canvas), duration: 1000, }); line.animate("angle", "90", { onChange: canvas.renderAll.bind(canvas), }); // Add it to the canvas canvas.add(line); </script> </body> </html>
- Related Articles
- How to set the duration of animation in Text using FabricJS?
- How to add animation in Line using FabricJS?
- How to set the angle of rotation of a Line using FabricJS?
- How to set the angle of a Line in FabricJS?
- How to set the border colour of a Line in FabricJS?
- How to set the color of a selection area on a canvas using FabricJS?
- How to change the duration of cursor fadein in IText using FabricJS?
- FabricJS – How to set the size of the controlling corners of a Line?
- FabricJS – How to set the colour of the controlling corners of a Line?
- How to set the background colour of Line in FabricJS?
- How to set the border color of a selection area on a canvas using FabricJS?
- How to set the width of a selection area border on a canvas using FabricJS?
- How to add animation in Text using FabricJS?
- How to set the angle of skew on Y-axis of a Rectangle using FabricJS?
- How to set the angle of skew on the X-axis of a Circle using FabricJS?
