简体   繁体   中英

Deselection fabric.js object event

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

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.
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM