I'm trying to implement an undo-function for a canvas. Basically people can draw on a canvas, and then restore it to the state it was before. That's the theory. I save the current imageData every time I call a function draw. It's saved in an array eD(namespace).history. Drawing works fine, even if I manipulate the imageData. Here's my draw-function (cD is a namespace aswell):
editorClass.prototype.draw = function(){
eD.history.push(cD.imageData);
cD.context.putImageData(cD.imageData, 0, 0);
}
Now if I try to undo my changes I've made in between, I use a function called undo, which looks like that:
editorClass.prototype.undo = function(){
var temp = eD.history.pop();
cD.context.createImageData(cD.width,cD.height); //Can leave this out, nothing changes
cD.context.putImageData(temp, 0, 0);
}
As mentioned above, that won't work. The canvas I work with is created once the user loads the site. cD.imageData originates from this function:
editorClass.prototype.getPixels = function(){
cD.imageData = cD.context.getImageData(0, 0, cD.width, cD.height);
// get the image data of the context and save them into namespace
cD.pixels = cD.imageData.data;
// get the pixels
cD.pixelsLength = cD.width * cD.height * 4;
// precompute the length of the pixel array
}
Both namespaces are visible, if needed. Since I'm pretty new to canvas, it might be a simple question - so if you have improvements: I'm an empty bucked, fill me in ;)
Thanks