Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deselection fabric.js object event

Tags:

fabricjs

I am trying to execute some code every time a specific fabric object is "deselected". Is there any deselection event I can handle? I already have a function for when the object is selected, via the selected event, but have not found any documentation about the deselected one. At the canvas level I have the selection:cleared and selection:created events, but nothing for the deselection either.

Cheers, Gonzalo

like image 466
ggmendez Avatar asked Jun 23 '14 13:06

ggmendez


2 Answers

Use the before:selection:cleared event and get the active object or group. After that you can check if it corresponds to your specific fabric object.

canvas.on('before:selection:cleared', function() {
    var clearedObject;
    if(typeof(canvas.getActiveObject()) !== 'undefined') {
        clearedObject = canvas.getActiveObject();
    }
    else {
        clearedObject = canvas.getActiveGroup();
    }
    //do stuff with the deselected element if it is the specific one you want.
});
like image 148
TSE Avatar answered Nov 09 '22 18:11

TSE


Just to let you all know that the newest versions of Fabric.js include a deselected event for the Object class. The only thing you need to do now is:

var aFabricObject = <create your fabric object>

aFabricObject.on('deselected', function (options) {
     // your code here
});
like image 37
ggmendez Avatar answered Nov 09 '22 17:11

ggmendez