Deleting All objects(Grouped/Ungrouped) from Canvas with fabricjs

Viewed 10976

I am trying to remove all the objects on my Canvas without selecting them. The objects in the Canvas include Grouped and Ungrouped object. All the Examples I've seen demonstrate how to delete a single ungroup object.

Canvas.ForEachObject(function(o){
     o.remove();
    });

Please see the fiddle for an example of what I'm trying to achieve. https://jsfiddle.net/Shane00711/r8su3ya0/

3 Answers

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.

Related