I did this before, saving the canvas state using the toDataUrl method for undo_array:
var undo_array = [];
so before any changes you make:
undo_array.push(myCanvas.toDataURL("image/png"));
now that the cancel button is pressed just restore the previous image:
var myImg = new Image(); oImg.onload = function() { var ctx = document.getElementById("canvasID").getContext("2d"); ctx.drawImage(myImg, 0, 0); } myImg.src = undo_array.pop();
but if you also change the capture of the canvas, I think you can use another array to store / restore the changes.
source share