21

I'm actually working on a html5 canvas project which uses the fabric.js Framework for the canvas interactions. Now I'm struggeling with the deletion of multiple objects. The following code does not seem to track the selected objects, but tracks all objects on the canvas.

var deleteSelectedObject = document.getElementById('delete-item');
deleteSelectedObject.onclick = function(){
    var curSelectedObjects = new Array();
    curSelectedObjects = canvas.getObjects(canvas.getActiveGroup);
    canvas.discardActiveGroup();
    for (var i = 0; i < curSelectedObjects.length; i++){
        canvas.setActiveObject(curSelectedObjects[i]);
        canvas.remove(canvas.getActiveObject());
    }
};

Don't get my failure.

John Mayer
  • 1,039
  • 1
  • 11
  • 17
  • 3
    Just a side note: don't use `var curSelectedObjects = new Array();` new Array, just use `[]`. Especially in this case, where you're not dealing with an array, but a node list (!== the same thing) – Elias Van Ootegem Aug 06 '12 at 14:10
  • 1
    Are you trying to delete a selected group of objects? – kangax Aug 06 '12 at 19:06
  • @kangax yes, thatÄs exactly what I'm trying to do. Any suggestions? – John Mayer Aug 07 '12 at 07:36
  • 4
    `canvas.getActiveGroup().forEachObject(function(o){ canvas.remove(o) }); canvas.discardActiveGroup().renderAll();` – kangax Aug 07 '12 at 09:41

6 Answers6

37

Due to @Kangax comment which solved most of the problem, I found the following solution to delete the currently selected objects from the canvas.

var deleteSelectedObject = document.getElementById('delete-item');
deleteSelectedObject.onclick = function()
{
if(canvas.getActiveGroup()){
      canvas.getActiveGroup().forEachObject(function(o){ canvas.remove(o) });
      canvas.discardActiveGroup().renderAll();
    } else {
      canvas.remove(canvas.getActiveObject());
    }
};

The function checks whether a group is selected. If a group is selected every object of the group gets removed. If no group is selected the function tries to remove a selected object. If nothing is selected, the canvas is not changed.

Alex W
  • 37,233
  • 13
  • 109
  • 109
John Mayer
  • 1,039
  • 1
  • 11
  • 17
5

Your code seems like it is selecting and then de-selecting the objects.

This may work better:

var deleteSelectedObject = document.getElementById('delete-item');
deleteSelectedObject.onclick = function()
{
    var curSelectedObjects = canvas.getObjects(canvas.getActiveGroup);

    canvas.discardActiveGroup();
    for (var i = 0; i < curSelectedObjects.length; i++)
    {
        canvas.remove(curSelectedObjects[i]);
    }
};

Good information link:

https://github.com/kangax/fabric.js/wiki/Tutorial-2#wiki-modifying-objects

Alex W
  • 37,233
  • 13
  • 109
  • 109
3

You can check any object property and can remove

var objects = canvas.getObjects();

for (var i = 0; i < objects.length; ) {
  if (objects[i].name == 'cropArea' || objects[i].name == 'bleedLine') {
    canvas.remove(objects[i]);
    i = 0;
  } else {
    i++;
  }
}
Joseph Callaars
  • 1,770
  • 1
  • 19
  • 28
Amit Gaur
  • 31
  • 2
0

I done this:

  var selectedObj = canvas.getObjects(canvas.getActiveGroup()) 

return me the array of the selected objects. :) the last function paranthesis is missing in your code snippet

Ajit kohir
  • 481
  • 5
  • 14
0

None of the solutions above (or anywhere elese on stackoverflow) worked for me except for this one solution I found on jsfiddle:

function deleteObjects(){
var activeObject = canvas.getActiveObject(),
activeGroup = canvas.getActiveGroup();
if (activeObject) {
    if (confirm('Are you sure?')) {
        canvas.remove(activeObject);
    }
}
else if (activeGroup) {
    if (confirm('Are you sure?')) {
        var objectsInGroup = activeGroup.getObjects();
        canvas.discardActiveGroup();
        objectsInGroup.forEach(function(object) {
        canvas.remove(object);
        });
    }
}
}

http://jsfiddle.net/beewayne/z0qn35Lo/

Hooman Askari
  • 1,486
  • 16
  • 30
0

Did you know that canvas.remove can take more than one parameter? So the easiest way should be this one:

canvas.remove(...canvas.getObjects());

Other than canvas.clear this will only remove the objects in the canvas and not also the background.

TAB
  • 1,944
  • 8
  • 28
  • 45