基於HTML5 Canvas和jQuery的畫圖工具的實現
阿新 • • 發佈:2019-01-07
-
//undo redo
-
var history =new Array();
-
var cStep = -1;
-
/**
-
* put current canvas to cache
-
*/
-
function historyPush()
-
{
-
cStep++;
-
if (cStep < history.length)
-
{
-
history.length = cStep;
-
}
-
history.push($("#myCanvas").get(0).toDataURL());
-
}
-
/**
-
* function: undo
-
*/
-
function undo()
-
{
-
if (cStep >= 0)
-
{
-
cStep--;
-
var tempImage = new Image();
-
tempImage.src = history[cStep];
-
tempImage.onload = function () { ctx.drawImage(tempImage, 0, 0);};
-
}
-
}
-
/**
-
* function: redo
-
*/
-
function redo()
-
{
-
if (cStep <history.length-1)
-
{
-
clearCanvas();
-
cStep++;
-
var tempImage = new Image();
-
tempImage.src = history[cStep];
-
tempImage.onload = function () { ctx.drawImage(tempImage, 0, 0); };
-
}
- }