How to identify if the given object is of a Polyline instance 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 identify if the given object is of a Polyline instance, we use the isType method. This method checks if the object is of the specified type and returns a true or false value depending on that.

Syntax

isType(type: String): Boolean

Here, type is the parameter that accepts a String which specifies the type we want to check.

Example 1: Using isType Method

Let’s see a code example to see the logged output when the isType method is used. The isType method returns a true or false value depending on whether the type of the instance matches the type that we want to check or not. In this case, a true value is returned since the type matches.

<!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 isType 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: 50,
            fill: "black",
            strokeWidth: 2,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polyline);
      
      // Using isType method
      console.log(
         "Is the specified type identical to a polyline instance? : ",
         polyline.isType("polyline")
      );
   </script>
</body>
</html>

Example 2: Using isType Method with a Different Value

In this example, we have used the isType to check if the specified circle type is identical to a polyline instance. Here, a false value is returned since they are not identical.

<!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 isType method with a different value</h2>
   <p> You can open console from dev tools and see that the logged output contains a false value </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: 50,
            fill: "black",
            strokeWidth: 2,
         } 
      );
      
      // Adding it to the canvas
      canvas.add(polyline);
      
      // Using isType method
      console.log(
         "Is the specified type identical to a polyline instance? : ",
         polyline.isType("circle")
      );
   </script>
</body>
</html>

Updated on: 16-Feb-2023

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements