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 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 cancel running animations, we use the dispose method.

Syntax

dispose()

Without using dispose method

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.

<!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

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. The dispose method is called immediately after starting the animation, preventing it from executing.

<!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 - cancels all running animations
      line.dispose();
   </script>
</body>
</html>

How It Works

The dispose method in FabricJS serves multiple purposes:

  • Cancels all running animations on the object
  • Removes event listeners
  • Cleans up internal references to prevent memory leaks

When called on a Line object with active animations, dispose immediately stops all ongoing animations and performs cleanup operations.

Conclusion

The dispose method is essential for cancelling running animations in FabricJS Line objects. It provides a clean way to stop animations and free up resources, preventing unwanted visual effects and potential memory leaks.

Updated on: 2026-03-15T23:19:00+05:30

539 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements