FabricJS – How to check if a Polygon Object Intersects with Another Object?


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.

In order to check if a Polygon object intersects with another object, we use the intersectsWithObject method. This method checks whether the object that is passed to it, intersects with the polygon object.

Syntax

intersectsWithObject(other: Object, absolute: Boolean, calculate: Boolean ): Boolean

Parameters

  • other βˆ’ This parameter accepts an Object which specifies the object we want to test.

  • absolute (optional) βˆ’ This parameter accepts a String value which specifies whether to use coordinates without viewportTransform or not. This parameter is optional.

  • Calculate (optional) βˆ’ This parameter accepts a Boolean value which specifies whether to use coordinates of current position. This parameter is optional.

Example 1: Using intersectsWithObject Method

Let’s see a code example to see the logged output when the intersectsWithObject method is used. The intersectsWithObject method returns true or false on checking if the polygon object intersects with another object. Here, we have initialized two rectangle objects namely rectangleRed and rectangleBlue. Since our polygon object intersects with rectangleRed, a true value is returned.

<!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 intersectsWithObject method</h2>
   <p>You can open console from dev tools and see the logged output</p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating a polygon object
      var polygon = new fabric.Polygon(
         [
            { x: -20, y: -35 },
            { x: 20, y: -35 },
            { x: 40, y: 0 },
            { x: 20, y: 35 },
            { x: -20, y: 35 },
            { x: -40, y: 0 },
         ],
         {
            stroke: "red",
            left: 100,
            top: 10,
            fill: "black",
            strokeWidth: 2,
            strokeLineJoin: "bevil",
         }
      );
      
      // Initiate a rectangle object
      var rectangleRed = new fabric.Rect({
         width: 60, 
         height: 20,
         top: 40,
         left: 80,
         fill: "red",
         strokeWidth: 6,
      });
      
      // Initiate another rectangle object
      var rectangleBlue = new fabric.Rect({
         width: 20,
         height: 40,
         top: 70,
         left: 200,
         fill: "blue",
      });
      
      // Add them to the canvas
      canvas.add(polygon);
      canvas.add(rectangleRed);
      canvas.add(rectangleBlue);
      
      // Using intersectsWithObject method
      console.log(
         "Does the polygon object intersect with rectangleRed?: ",
         polygon.intersectsWithObject(rectangleRed)
      );
      console.log(
         "Does the polygon object intersect with rectangleBlue?: ",
         polygon.intersectsWithObject(rectangleBlue)
      );
   </script>
</body>
</html>

Example 2: Using intersectsWithObject Method with Different Objects

In this example, we have used the intersectsWithObject method along with different objects to prove that this method can work with any object.

<!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 intersectsWithObject method with different objects</h2>
   <p>You can open console from dev tools and see the logged output</p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating a polygon object
      var polygon = new fabric.Polygon(
         [
            { x: -20, y: -35 },
            { x: 20, y: -35 },
            { x: 40, y: 0 },
            { x: 20, y: 35 },
            { x: -20, y: 35 },
            { x: -40, y: 0 },
         ],
         {
            stroke: "red",
            left: 100,
            top: 10,
            fill: "black",
            strokeWidth: 2,
            strokeLineJoin: "bevil",
         }
      );
      
      // Initiate a triangle object 
      var triangle = new fabric.Triangle({
         width: 90,
         height: 70,
         top: 40,
         left: 80,
         fill: "red",
         strokeWidth: 6,
      });
      
      // Initiate a circle object
      var circle = new fabric.Circle({
         radius: 40,
         top: 70,
         left: 200,
         fill: "blue",
      });
      
      // Add them to the canvas
      canvas.add(polygon);
      canvas.add(triangle);
      canvas.add(circle);
      
      // Using intersectsWithObject method
      console.log(
         "Does the polygon object intersect with triangle?: ",
         polygon.intersectsWithObject(triangle)
      );
      console.log(
         "Does the polygon object intersect with circle?: ",
         polygon.intersectsWithObject(circle)
      );
   </script>
</body>
</html> 

Conclusion

In this tutorial, we used two examples to demonstrate how you can check if a Polygon object intersects with another object using FabricJS.

Updated on: 29-Dec-2022

510 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements