简体   繁体   中英

Deselect a selected object in Fabric.js

I have multiple type objects on my canvas. I can do scaling, rotation, etc. everything on a particular object.

But when I select a particular object, I want to change its stroke color.

canvas.observe('mouse:down', function(e) {

    activeInstance = canvas.getActiveObject();
    activeGroupInstance = canvas.getActiveGroup();
    if (activeInstance!=null){        
        activeInstance.set("stroke","#FF0000");
    }
}

The code works well. But the issue is that the stroke color or any other valid property doesn't reflect directly which means I have to deselect a currently selected object to see the change in its properties. So my question is what's going wrong? If this is the usual behavior that we have to deselect/click the selected object, then, how to deselect the selected object using code ie without using mouse clicks simply using a code-snippet.

We can use below mentioned solutions.

To discard all active groups you can use below function.Discards currently active group and fire events If the function is called by fabric as a consequence of a mouse event, the event is passed as a parmater and sent to the fire function for the custom events. When used as a method the e param does not have any application.

canvas.discardActiveGroup();

To discard sigle object you can use like this. Discards currently active object and fire events. If the function is called by fabric as a consequence of a mouse event, the event is passed as a parmater and sent to the fire function for the custom events. When used as a method the e param does not have any application.

canvas.discardActiveObject();

And i the last finally renders both the top canvas and the secondary container canvas like this.

canvas.renderAll();

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