Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deactivate all objects in Fabric.js

How to deactivate all objects in Fabric.js before saving an image?

function rasterize() {
  if (!myscript.Canvas.supports('toDataURL')) {
    alert('This browser doesn\'t provide means to serialize canvas to an image');
  }
  else {
    /*i want to deactivate all object here*/
    window.open(canvas.toDataURL('png'));
  }
}
like image 291
MP. Avatar asked Jan 15 '14 07:01

MP.


3 Answers

canvas.deactivateAll(); => no events are fired http://fabricjs.com/docs/fabric.Canvas.html#deactivateAll

or

canvas.deactivateAllWithDispatch(); => if activeObject exists the events 'before:selection:cleared' and 'selection:cleared' are fired. http://fabricjs.com/docs/fabric.Canvas.html#deactivateAllWithDispatch

like image 85
Kienz Avatar answered Oct 19 '22 06:10

Kienz


What worked for me (fabric 2.7):

this.canvas.forEachObject(object => {
    object.selectable = false;
    object.evented = false;
});

Another solution is to apply some CSS to the canvas : pointer-events: none

like image 36
Emerica Avatar answered Oct 19 '22 05:10

Emerica


canvas.deactivateAll();
canvas.renderAll();

only this two lines also do the same no need to make canvas.selection as false. It is used to disable group selection.

like image 1
Jitendra Pawar Avatar answered Oct 19 '22 05:10

Jitendra Pawar