我正在尝试为我的绘图应用创建撤消和重做功能。我用console.log检查了函数的执行情况。 单击撤消/重做按钮时,函数会执行,但drawImage函数不会绘制相关图像(较旧的画布状态,较新的画布状态,如此)。
我不确定我哪里出错了。
//Calls this function every time a piece is drawn - mouseup event
function cPush() {
cStep += 1;
var length = cPushArray.length;
if (cStep < cPushArray.length) {
cPushArray.length = cStep;
}
cPushArray.push(document.getElementById("canvas").toDataURL("image/png"));
}
//Undo
function undoClick() {
if (cStep > 0) {
cStep -= 1;
var canvasPic = new Image();
canvasPic.onload = function () {
ctx.drawImage(canvasPic, 0, 0);
};
canvasPic.src = cPushArray[cStep];
}
}
#Redo
function redoClick() {
if (cStep < cPushArray.length - 1) {
cStep += 1;
var canvasPic = new Image();
canvasPic.onload = function () {
ctx.drawImage(canvasPic, 0, 0);
};
canvasPic.src = cPushArray[cStep];
}
}