Search code examples
javascriptfabricjs

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.


Solution

  • 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;
    }