FabricJS – How to identify if the given object is of a Polygon instance?

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 identify if the given object is of a Polygon 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

Parameters

type ? This parameter accepts a String which specifies the type we want to check.

Return Value

Returns true if the object is of the specified type, otherwise returns false.

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

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

Common Use Cases

The isType method is particularly useful when working with multiple FabricJS objects and you need to perform type-specific operations. Common use cases include:

  • Filtering objects by type in a canvas collection
  • Applying specific transformations based on object type
  • Implementing conditional logic for different shape types

Conclusion

The isType method provides a reliable way to identify if a FabricJS object is of a Polygon instance. Use it to implement type-specific logic in your canvas applications.

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

224 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements