Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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/

like image 455
Sifiso Mfeya Avatar asked Jul 03 '17 08:07

Sifiso Mfeya


2 Answers

You just need to call

canvas.clear()

it will remove all object

like image 55
Durga Avatar answered Nov 03 '22 10:11

Durga


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.

like image 30
mbe Avatar answered Nov 03 '22 11:11

mbe