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'));
}
}
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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With