- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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>