38

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!

kangax
  • 38,898
  • 13
  • 99
  • 135
PaulLing
  • 2,068
  • 3
  • 17
  • 21

5 Answers5

72

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();
CrandellWS
  • 2,708
  • 5
  • 49
  • 111
Shantanu Wagh
  • 1,011
  • 7
  • 13
19

suppose you have three objects objectA, ObjectB and ObjectC and objects are selected. Now if you want to deselect any object( for example ObjectB). In this case you can try following code.

var activeGroup = canvas.getActiveGroup();
activeGroup.removeWithUpdate(ObjectB);
canvas.renderAll(); 

If you are having only one node active then you can do.

canvas.discardActiveObject();
canvas.renderAll(); 
14

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 in the last finally renders both the top canvas and the secondary container canvas like this.

canvas.renderAll();
Alex Mac
  • 2,970
  • 1
  • 22
  • 39
  • 3
    → In one line `canvas.discardActiveObject().renderAll();` – Emeric Sep 08 '19 at 09:00
  • 1
    True, But please read my answer again. I have added how to discard GROUP, OBJECT and then render it. The one line you have added here will work only when you want to remove the single object and render it but in case of removing the entire group, you have to change from OBJECT to GROUP. – Alex Mac Sep 11 '19 at 09:13
6

on updates there is just

canvas.discardActiveObject()
GIORGI N.
  • 71
  • 1
  • 2
  • 1
    Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since another user already posted that solution. If a previous answer was helpful to you, you should [vote it up](https://stackoverflow.com/help/privileges/vote-up) once you have enough [reputation](https://stackoverflow.com/help/whats-reputation) – David Buck Nov 26 '19 at 19:31
1

I have just found a way to do so:

When multiple objects are activated, they actually formed a group. Then actually just need to use a method of fabric.group called "removeWithUpdate" then it will do.

Example:

var activeGroup = canvas.getActiveGroup();
activeGroup.removeWithUpdate(theObject);
canvas.renderAll();
PaulLing
  • 2,068
  • 3
  • 17
  • 21