-
Hi, I'm using the eraser brush; It is working properly but what if the user wants to undo the last action? How can I achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 6 replies
-
@egemenerd it is possible. |
Beta Was this translation helpful? Give feedback.
-
@ShaMan123 Thanks for the reply. Actually I just need to use the eraser for background image. I am able to find and remove the last added path with the following code. $("#undo").on("click", function () { var BGclipPath = canvas.backgroundImage.clipPath; var objects = BGclipPath.getObjects('path'); if (objects.length !== 0) { // Remove the last path BGclipPath._objects = BGclipPath._objects.slice(0, -1); // Update the clipPath canvas.backgroundImage.clipPath = BGclipPath; // Clear the canvas and load from json again var json = canvas.toJSON(); canvas.clear(); canvas.loadFromJSON(json, canvas.renderAll.bind(canvas)); } }); The problem is, the clippath is not being updated until I create another path with the eraser. I fixed this issue with clearing the canvas and loading from the json again after every button click. It is working but I'm not sure this is the best way :) Note: I tried using |
Beta Was this translation helpful? Give feedback.
-
@ShaMan123 I did what you said and it worked. Thank you very much :) Here is the final code; $("#undo").on("click", function () { var BGclipPath = canvas.backgroundImage.clipPath; BGclipPath.set("dirty", true); var objects = BGclipPath.getObjects('path'); if (objects.length !== 0) { BGclipPath._objects = BGclipPath._objects.slice(0, -1); canvas.backgroundImage.set({ clipPath: BGclipPath, dirty: true}); canvas.requestRenderAll(); } }); |
Beta Was this translation helpful? Give feedback.
-
This is now supported via |
Beta Was this translation helpful? Give feedback.
-
Hi @ShaMan123 I would make an undo system which can remove eraser actions step by step (and not all object eraser at once). I have tried to remove only the first object of this array, but it has no visual effect on canvas, i do not understand why ?
if i delete all Thanks ! |
Beta Was this translation helpful? Give feedback.
@egemenerd it is possible.
The easiest way that comes to mind is listening to
erasing:end
event.It provides enough context to make these decisions.
Take a look at the demo http://fabricjs.com/erasing
Then you can remove the paths from the objects' clip path.
BTW I think I'm going to change how eraser interacts with object, freeing it for clipPath, but in the near future