I am testing things right now (just started out with web development a few days ago) so I pretty much just copied and pasted the undo function from http://www.codicode.com/art/undo_and_redo_to_the_html5_canvas.aspx. Right now I am unable to access the full source code so i'm kinda just guessing where to place things.
Here is a snippet of the code that I think is relevant to the question:
var cPushArray = new Array();
var cStep = -1;
var ctx = document.getElementById('canvas').getContext("2d");
function cPush() {
cStep++;
if (cStep < cPushArray.length) {
cPushArray.length = cStep;
}
cPushArray.push(document.getElementById('canvas').toDataURL());
}
function cUndo() {
if (cStep > 0) {
cStep--;
var canvasPic = new Image();
canvasPic.src = cPushArray[cStep];
canvasPic.onload = function() {
ctx.drawImage(canvasPic, 0, 0);
}
}
}
//This draws the dots on the face.
function drawCoordinates(x, y) {
var pointSize = 3; // Change according to the size of the point.
var ctx = document.getElementById("canvas").getContext("2d");
ctx.fillStyle = "#ff2626";
ctx.beginPath(); //Start path
ctx.arc(x, y, pointSize, 0, Math.PI * 2, true); // Draw a point using the arc function of the canvas with a point structure.
ctx.fill(); // Close the path and fill.
cPush();
}
//count variable keeps track of the flicks
var count = 1;
//this listens for button clicks and displays the elements
document.getElementById('button').onclick = function() {
document.getElementById(count).style.display = "block";
count++;
}
document.getElementById('buttonUndo').onclick = function() {
cUndo();
}
So far I know for a fact that the undo button is correctly linked because when I code alert("hello"), the alert pops up when the button is clicked. However, the undo function doesn't do anything in the code and i'm having trouble figuring out why it's behaving that way and how to fix it.
cUndowill only undo what was done whencPushwas called. Make sure you're callingcPushafter modifying the canvas.cUndo()function it checks forif (cStep > 0)so everything inside that block will only run if cStep is greater than 0.