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.
Your code seems like it is selecting and then de-selecting the objects.
This may work better:
Good information link:
https://github.com/kangax/fabric.js/wiki/Tutorial-2#wiki-modifying-objects
You can check any object property and can remove
Did you know that canvas.remove can take more than one parameter? So the easiest way should be this one:
Other than
canvas.clear
this will only remove the objects in the canvas and not also the background.I done this:
return me the array of the selected objects. :) the last function paranthesis is missing in your code snippet
Due to @Kangax comment which solved most of the problem, I found the following solution to delete the currently selected objects from the canvas.
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.
None of the solutions above (or anywhere elese on stackoverflow) worked for me except for this one solution I found on jsfiddle:
http://jsfiddle.net/beewayne/z0qn35Lo/