How to check if Polyline object intersects with another object using FabricJS?


We can create a Polyline object by creating an instance of fabric.Polyline. A polyline object can be characterised by 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 Polyline object intersects with another object, we use the intersectsWithObject method. This method checks whether the object that is passed to it, intersects with the polyline 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 polyline object intersects with another object.

Here, we have initialized two rectangle objects namely rectangleRed and rectangleBlue. Since our polyline 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 polyline object
      var polyline = new fabric.Polyline(
         [
            { 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: "white",
            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(polyline);
      canvas.add(rectangleRed);
      canvas.add(rectangleBlue);
      
      // Using intersectsWithObject method
      console.log(
         "Does the polyline object intersect with rectangleRed?: ",
         polyline.intersectsWithObject(rectangleRed)
      );
      console.log(
         "Does the polyline object intersect with rectangleBlue?: ",
         polyline.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 polyline object
      var polyline = new fabric.Polyline(
         [
            { 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: "white",
            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(polyline);
      canvas.add(triangle);
      canvas.add(circle);
      
      // Using intersectsWithObject method
      console.log(
         "Does the polyline object intersect with triangle?: ",
         polyline.intersectsWithObject(triangle)
      );
      console.log(
         "Does the polyline object intersect with circle?: ",
         polyline.intersectsWithObject(circle)
      );
   </script>
</body>
</html> 

Updated on: 16-Feb-2023

116 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements