Kai van Megen
Kai van Megen

Reputation: 15

How to get a specific object from Farbic.js and read the type

How do you get a specific object from Fabric.js, and determine which one you got? Like calling canvas.getSelectedObject() and then determining if the returned object is an IText or a Circle or something else.

Thanks.

Upvotes: 0

Views: 82

Answers (2)

melchiar
melchiar

Reputation: 2872

I believe what you're looking for is the object type property. You can use it like this:

var obj = canvas.getActiveObject();
switch(obj.type) {
   case "circle":
   //stuff to do if selection is a circle
   
   break;
   case "itext":
   //stuff to do if selection is a itext
   
   break;
}

Upvotes: 1

Chris Kao
Chris Kao

Reputation: 503

I think instanceof may be what you want.

var c = new fabric.Circle()
console.log(c instanceof fabric.Circle) // true

Upvotes: 0

Related Questions