Do we have canvas Modified Event in Fabric.js?

8.8k Views Asked by At

In Fabric.js we have Object modified events like object:modified. Do we have similar event for the entire canvas.

Actually I am trying to implement undo and redo features. I am saving the canvas as JSON if something happens on it and loading it again for undo feature.

Do we have any better solution for this features in Fabric.js?

2

There are 2 best solutions below

0
On

This is better explained in this link. Use it this way:

canvas.on('object:moving', function(e) { // or 'object:added'
  var activeObject = e.target;
  console.log(activeObject.get('left'), activeObject.get('top'));
});
0
On

Don't forget to check for added/removed objects too. You could implement it like this:

var canvasModifiedCallback = function() {
console.log('canvas modified!');
};

canvas.on('object:added', canvasModifiedCallback);
canvas.on('object:removed', canvasModifiedCallback);
canvas.on('object:modified', canvasModifiedCallback);