1

I am trying to select multiple and delete. Selecting one object deletes just fine but when selecting multiple it doesn't do anything. I have looked around and seen this answer with a fiddle which works in v1.4

https://stackoverflow.com/a/41286840

When I select multiple I get

canvas.getActiveGroup is not a function

Now as the getActiveObject can go from an array. I tried to check if greater than one using length and then deleting those objects but it always goes through the

if (activeObject) {

instead of

else if (activeObject.length >= 2) {

But neither will work. Doesn't Fabric have a function with multiple selected items?

ServerSideSkittles
  • 2,713
  • 10
  • 34
  • 60

2 Answers2

2

As it is mentioned on change log getActiveGroup this function is removed now.

So you need to get the object using canvas.getActiveObjects() then loop through the objects present and remove them from canvas.

Here is jsFiddle

Durga
  • 15,263
  • 2
  • 28
  • 52
  • In that example I getUncaught TypeError: activeObject.forEachObject is not a function on single delete. Multiple works just fine. How would I check if object is multiple? I tried if (activeObject.length >= 2) { and doesnt work – ServerSideSkittles Nov 28 '17 at 11:20
  • @ServerSideSkittles My bad, didnt check for single object. Check updated fiddle link. – Durga Nov 28 '17 at 11:32
0

canvas.getActiveGroup was removed in the new versions. so here's the code work fine. try this. more information about fabricJs changes click to refer this fabricJS breaking changes

deleteSelectedObject() {
    let activeObject = this.canvas.getActiveObjects();
    if (activeObject) {
      let objectsInGroup = activeObject;
      this.canvas.discardActiveObject();
      let self = this;
      objectsInGroup.forEach(function(object) {
        self.canvas.remove(object);
      });
    }
  }
Raheem Mohamed
  • 789
  • 3
  • 6
  • 15