我是堆栈溢出的新手;但是发现这里讨论的许多主题对 Web 应用程序的开发很有帮助。
然而,这一次我被难住了。在使用新的 HTML5 画布时,我用 JavaScript 编写了一些简单的函数来添加撤消/重做功能。我编写的代码在 Chrome 中运行良好,但在 Firefox 中不起作用;我想我已将问题隔离到我将图像对象放入数组中的代码行(在 Chrome 中工作正常)。我对此进行了研究,但找不到任何理由说明这在 FireFox 中不起作用。任何帮助将不胜感激。
function push()
{
window.cStep++;
if (window.cStep < window.history.length) { window.history.length = window.cStep; }
var imageData = window.context.getImageData(0, 0, canvas.width, canvas.height);
alert(imageData);
window.history[window.cStep] = imageData;
document.title = "Step [" +window.cStep + "] Of A Possible [" + (window.history.length -1) + "]";
}
function undo()
{
if (window.cStep > 0) {
window.cStep--;
window.context.putImageData(window.history[window.cStep], 0, 0);
document.title = "Step [" +window.cStep + "] Of A Possible [" + (window.history.length -1) + "]";
if(window.cStep ==0)
{document.getElementById('middle_centre_canvas').style.opacity = 0.6;}
else
{document.getElementById('middle_centre_canvas').style.opacity = 1;}
}
}
function redo()
{
if (window.cStep < window.history.length-1) {
window.cStep++;
window.context.putImageData(window.history[window.cStep],0,0);
document.title = "Step [" +window.cStep + "] Of A Possible [" + (window.history.length -1) + "]";
if(window.cStep >0)
{document.getElementById('middle_centre_canvas').style.opacity = 1;}
}
}
function loadyLoader(){
window.canvas = document.getElementById("canvas");
window.context = window.canvas.getContext('2d');
window.history = new Array();
window.cStep = -1;
push();
}