How to make a polygon object react to the resizing event using FabricJS?


We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. We use the object:modified event to make a polygon object react to being resized.

Syntax

object:modified

Example 1: Default Appearance of the Polygon Object

Let’s see a code example of how the polygon object appears when the object:modified event is not used. In this case, the polygon object will be added to the canvas.

<!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 polygon object</h2>
   <p>You can see that the polygon object has been added to the canvas</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 polygon instance
      var polygon = new fabric.Polygon(
         [
            { x: 0, y: 0 },
            { x: 0, y: 50 },
            { x: 50, y: 50 },
            { x: 50, y: 0 },
         ],
         {
            left: 100,
            top: 30,
            fill: "red",
            stroke: "blue",
            strokeWidth: 2,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
   </script>
</body>
</html>

Example 2: Displaying how the Object Reacts to being Resized

Let’s see a code example to see the logged output when a polygon object is resized. We have used the object:modified event which is fired at the end of any object transform or any change concerning the object. In this case, everytime we change the scale of the object, the scaled height and width will be console logged.

<!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>Displaying how the object reacts to being resized</h2>
   <p>
      You can scale object using corner and open console from dev tools to see that the scaled width and height value of the polygon object is being logged
   </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 polygon instance
      var polygon = new fabric.Polygon(
         [
            { x: 0, y: 0 },
            { x: 0, y: 50 },
            { x: 50, y: 50 },
            { x: 50, y: 0 },
         ],
         {
            left: 100,
            top: 30,
            fill: "red",
            stroke: "blue",
            strokeWidth: 2,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using object:modified event
      canvas.on("object:modified", (e) => {
         canvas.getActiveObjects().forEach((o) => { 
            console.log(
               "Scaled Height of the polygon is: ",
               o.getScaledHeight(),
               "Scaled Width of the polygon is:",
               o.getScaledWidth()
            );
         });
      });
   </script>
</body>
</html>

Conclusion

In this tutorial, we used two simple examples to demonstrate how you can make a polygon object react to the resizing event using FabricJS.

Updated on: 02-Jan-2023

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements