2014-01-13 119 views
1

我想實現的撤消和用我的程序下面的代碼重新在我canvas.I選項撤消選項:我想實現我的畫布

var cPushArray = new Array(); 
var cStep = -1; 
function cPush() 
{ 
    cStep++; 
    if (cStep < cPushArray.length) 
    { 
     cPushArray.length = cStep; 
    } 
    cPushArray.push(document.getElementById("drawingCanvas").toDataURL()); 
} 

function cUndo() 
{ 
    if(cStep > 0) 
    { 
     cStep--; 
     var canvasPic = new Image(); 
     canvasPic.src = cPushArray[cStep]; 
     canvasPic.onload = function() { ctx.drawImage(canvasPic,0,0);} 
    } 
} 
function cRedo() 
{ 
    if(cStep < cPushArray.length-1) 
    { 
     cStep++; 
     var canvasPic = new Image(); 
     canvasPic.src = cPushArray[cStep]; 
     canvasPic.onload = function() {ctx.drawImage(canvasPic,0,0);} 
    } 
} 

但我不能畫什麼在我的畫布上,如果我調用cPush()方法。 你能告訴我在上面的代碼中我錯了嗎?

+0

謝謝您的回覆。這對我幫助很大 – user3188826

回答

1

不要使用onload事件,因爲它只是由DOM就緒事件觸發的。

var canvasPic = new Image(); 
canvasPic.src = cPushArray[cStep]; 
//if ctx has get the context of canvas, draw the pic immediately 
ctx.drawImage(canvasPic,0,0);