
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Identify if Object is Polygon Instance in Fabric.js
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.
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>
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>
Conclusion
In this tutorial, we used two simple examples to demonstrate how you can identify if a given object is of a Polygon instance using FabricJS.