【发布时间】:2012-04-10 03:12:15
【问题描述】:
我正在使用 HTML5 画布创建一个 Web 应用程序来绘制图像(如绘制网络),我尝试实现“撤消”(ctrl+Z)和“重做”功能,在这里我遇到了一个奇怪的数组问题画布元素。 有时,当我按 ctrl+Z 撤消时,会出现一个空白图像,但是数据在数组中并且我指向正确的元素(因为当我使用撤消/重做时,我设法以正确的顺序获得正确的图像)。
如果你能看看下面的代码,我将不胜感激,我已经花了很多时间,但我无法找到问题... :-(
function Stack(firstImg , size) {
var drawStack = new Array();
var stackIndex = 0;
var stackTop = 0;
var stackFloor = 0;
var stackSize = size;
drawStack[0] = firstImg;
this.add = function() {
drawStack[++stackIndex%stackSize] = cvs.toDataURL("image/png");
if (stackIndex >= stackSize) stackFloor = (stackIndex +1) % stackSize ;
stackTop = stackIndex % stackSize;
}
this.undo = function () {
if (stackIndex%stackSize == stackFloor ) return;
clearCanvas();
var tmpImg = new Image();
tmpImg.src = drawStack[--stackIndex%stackSize];
cvsCtx.drawImage(tmpImg, 0, 0);
}
this.redo = function () {
if (stackIndex%stackSize == stackTop) return;
clearCanvas();
var tmpImg = new Image();
tmpImg.src = drawStack[++stackIndex%stackSize];
cvsCtx.drawImage(tmpImg, 0, 0);
}
}
任何解决方案或解决方法,我都会接受,非常感谢!!
【问题讨论】:
-
javascript 数组有 push、pop 和 shift。我不确定是否有理由进行所有模块化划分和索引...
-
致对这篇文章感兴趣的人。我的代码有效,我只是忘记了著名的 img.onload 函数......这会导致我的应用程序中出现空白图片
标签: javascript jquery image html canvas