Fabric.js: how to deselect one or multiple objects on canvas?

canvas, fabricjs, javascript

Solution

I may be late answering this, but in `fabricjs (1.4.3)` you can use the following to deselect all selected objects on the canvas.

canvas.deactivateAll().renderAll();

I am using this before creating a image of that canvas. Hope it helps someone.

for fabricjs 4.0 you can use:

canvas.discardActiveObject().renderAll();

Problem

For example, I have multiple objects on a canvas, A,*B* and C. Three of these objects are selected as activated. By using Fabric.js, is it possible to discard/deactivate only one of the objects? For example, Three objects as selected, then when a button outside the canvas is clicked, object B is discarded/deactivated. I looked into the doc files on FabricJS official website, and I only found canvas.deactivateAll(), canvas.discardActiveObject(). These function are only able to deactivate all active objects while not a specific active object. Could someone please give me a guide on this? Thanks!

Original source