How to identify the type of a selected object?

前端 未结 4 1314
故里飘歌
故里飘歌 2021-02-12 11:13

I am placing Text, Image and Shapes on canvas using Fabric.js. I have made Three different Edit-Panels for all three. When user select text I want to show text panel. like wise

4条回答
  •  一生所求
    2021-02-12 12:01

    In fabricjs 3.4+, to get the object type, just use:

    var objType = canvas.getActiveObject().type;
    

    On IText object, the above will return: i-text

    On image, it will return: image

    Usage:

    Call the following function which adds an additional check to make sure type is a property of the active element. Note: not all elements have the type property.

     canvas.on('object:selected', onObjectSelected);
    
     function onObjectSelected() { 
         // check if type is a property of active element
         var objType = (canvas.getActiveObject().type ? canvas.getActiveObject().type : "");
    
         // your code to process the object
    }
    
       

提交回复
热议问题