- 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 cancel running animations in Line using FabricJS?
In this tutorial, we are going to learn about how to cancel running animations in 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 onedimensional 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 cancel running animations, we use the dispose method.
Syntax
dispose()
Without using dispose method
Example
Let’s see a code example to see how animation works for a Line object when the dispose method is not used. Here we have created a simple animation where the line object completes a full rotation and expands in width.
<!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>Without using dispose method</h2> <p>You can see the animation is happening properly</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([250, 100, 310, 100], { stroke: "blue", strokeWidth: 10, }); // Using the animate method line.animate("angle", "+=360", { onChange: canvas.renderAll.bind(canvas), duration: 1700, }); // Add it to the canvas canvas.add(line); </script> </body> </html>
Using the dispose method
Example
In this example, we have used the dispose method to cancel the running animation. All the running animations of the line instance will thus be cancelled.
<!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 dispose method</h2> <p>You can see that the animation no longer happens</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([250, 100, 310, 100], { stroke: "blue", strokeWidth: 10, }); // Using the animate method line.animate("angle", "+=360", { onChange: canvas.renderAll.bind(canvas), duration: 1700, }); // Add it to the canvas canvas.add(line); // Using dispose method line.dispose() </script> </body> </html>