/    Sign up×
Community /Pin to ProfileBookmark

Canvas undo function

Hello guys. I have code for drawing on a canvas. Code working but cannot figure out how to add undo function.
Thank you for any help.

[code]
//Drawing canvas

var canvas, stage;
var drawingCanvas;
var oldPt;
var oldMidPt;
var color;
var stroke;
var index;

function init() {
if (window.top != window) {
document.getElementById(“header”).style.display = “none”;
}
canvas = document.getElementById(“canvas”);
index = 0;

//check to see if we are running in a browser with touch support
stage = new createjs.Stage(canvas);
stage.autoClear = false;
stage.enableDOMEvents(true);

createjs.Touch.enable(stage);
createjs.Ticker.setFPS(24);

drawingCanvas = new createjs.Shape();

stage.addEventListener(“stagemousedown”, handleMouseDown);
stage.addEventListener(“stagemouseup”, handleMouseUp);

stage.addChild(drawingCanvas);
stage.update();
}

function stop() {}

function handleMouseDown(event) {
color = $(‘#selColor’).val();
stroke = document.getElementById(“selWidth”).value;
oldPt = new createjs.Point(stage.mouseX, stage.mouseY);
oldMidPt = oldPt;
stage.addEventListener(“stagemousemove” , handleMouseMove);
}

function handleMouseMove(event) {
var midPt = new createjs.Point(oldPt.x + stage.mouseX>>1, oldPt.y+stage.mouseY>>1);

drawingCanvas.graphics.clear().setStrokeStyle(stroke, ’round’, ’round’).beginStroke(color).moveTo(midPt.x, midPt.y).curveTo(oldPt.x, oldPt.y, oldMidPt.x, oldMidPt.y);

oldPt.x = stage.mouseX;
oldPt.y = stage.mouseY;

oldMidPt.x = midPt.x;
oldMidPt.y = midPt.y;

stage.update();
}

function handleMouseUp(event) {
stage.removeEventListener(“stagemousemove” , handleMouseMove);
}

function clearArea() {
// Use the identity matrix while clearing the canvas
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}

function UndoDraw() {

// hire i need undo code
}
[code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMar 15.2020 — Check if this helps you:

https://stackoverflow.com/questions/20888252/removing-shape-objects-using-easeljs-canvas
Copy linkTweet thisAlerts:
@AhmoauthorMar 15.2020 — @Sempervivum#1616132

<i>
</i>function UndoDraw() {

stage.removeChild(drawingCanvas[i]);

}


How to implant foreach loop for this code?
Copy linkTweet thisAlerts:
@SempervivumMar 15.2020 — If I read your code correctly, there is only one drawingCanvas object, thus forEach would be not applicable.

Unfortunately I'm not familiar with easejs but you might try the following procedure:
  • - Create a new shape or drawingCanvas on mousedown and enter it to an array.

  • - Perform all actions in onmousemove on this shape.

  • - When undoing remove the last shape in the array.
  • Copy linkTweet thisAlerts:
    @AhmoauthorMar 15.2020 — Ok. thank you for you time.
    Copy linkTweet thisAlerts:
    @teskleinMar 23.2020 — Thank you very much for your help. Its [myloweslife](https://myloweslife.onl/) really useful.
    ×

    Success!

    Help @Ahmo spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.24,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...