如何制作html画布撤消和重做功能?

时间:2018-05-23 04:19:46

标签: javascript html canvas undo redo

首先,抱歉我的英语不好。 我在我的应用程序中制作重做和撤消功能有困难..我已经阅读了其他问题和答案但未能适用于我的项目。我试图遵循的最近答案是this一个。

我的HTML代码

<th><img src="images/undo.png" onclick="undoLastPoint()"></th>
<th><img src="images/redo.png" onclick="()"></th>

我的javascript代码

var canvas = document.getElementById('mycanvas');
var ctx = canvas.getContext('2d');
var isDrawing=false;

function pencil () {

canvas.onmousedown = function(e) {
    var pos = getMousePos(canvas, e);
    isDrawing = true;
    ctx.moveTo(pos.x, pos.y);

};
canvas.onmousemove = function(e) {
    var pos = getMousePos(canvas, e);
    if (isDrawing) {
        ctx.lineTo(pos.x, pos.y);
      ctx.stroke();

    }
};
canvas.onmouseup = function() {
    isDrawing = false;
};
}

function undoLastPoint() {

// remove the last drawn point from the drawing array
var lastPoint=points.pop();

// add the "undone" point to a separate redo array
redoStack.unshift(lastPoint);

// redraw all the remaining points
redrawAll();
}

function getMousePos(canvas, evt) {
  var rect = canvas.getBoundingClientRect();
  return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
  };
}

0 个答案:

没有答案