Do we have canvas Modified Event in Fabric.js?
Asked Answered
D

2

9

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?

Darrel answered 29/8, 2013 at 9:36 Comment(4)
How do you expect this event to be different from "object:modified"? "object:modifed" covers all cases of modification on canvas, since any modification implies object change (except something like canvas background color, but that's usually not supposed to be part of undo/redo)Tyrrell
When we are adding any new objects to canvas object:modified event is not firing. It is firing only when we perform modifications(scaling,rotating etc) on any added object.Darrel
You can use "object:added" and/or "object:removed" for that — fabricjs.com/eventsTyrrell
@Tyrrell Those events don't appear to be triggered on setBackgroundColor, setHeight, or setWidth actions.Alburnum
C
5

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);
Cowan answered 23/1, 2018 at 13:54 Comment(0)
C
3

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'));
});
Cinchona answered 24/10, 2013 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.