Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi I am not sure if the topic is explain my problem but the situation is: I have a canvas and draw things when on mousedown , and I have a generate button that generates new canvas' again you can draw things in same way.. BUT the problem is when you click the previous canvas it started to draw the new one.. It must not draw the new canvas the function has to stop there .. here is the drawing function

canvas.onmousedown = function(e) {
if (!painting) {
    painting = true;
} else {
    painting = false;
}

lastX = e.pageX - this.offsetLeft;
lastY = e.pageY - this.offsetTop;
};

canvas.onmousemove = function(e) {
if (painting) {
    mouseX = e.pageX - this.offsetLeft;
    mouseY = e.pageY - this.offsetTop;

    // find all points between        
    var x1 = mouseX,
        x2 = lastX,
        y1 = mouseY,
        y2 = lastY;


    var steep = (Math.abs(y2 - y1) > Math.abs(x2 - x1));
    if (steep){
        var x = x1;
        x1 = y1;
        y1 = x;

        var y = y2;
        y2 = x2;
        x2 = y;
    }
    if (x1 > x2) {
        var x = x1;
        x1 = x2;
        x2 = x;

        var y = y1;
        y1 = y2;
        y2 = y;
    }

    var dx = x2 - x1,
        dy = Math.abs(y2 - y1),
        error = 0,
        de = dy / dx,
        yStep = -1,
        y = y1;

    if (y1 < y2) {
        yStep = 1;
    }

    for (var x = x1; x < x2; x++) {
        if (steep) {
            ctx.fillRect(y, x, 1, 1);
        } else {
            ctx.fillRect(x, y, 1, 1);
        }

        error += de;
        if (error >= 0.5) {
            y += yStep;
            error -= 1.0;
        }
    }



    lastX = mouseX;
    lastY = mouseY;

}
};

Here is jsfiddle for the issue http://jsfiddle.net/regeme/eV6kW/3/ any help would be great

share|improve this question
your question is more confusing tha your code!!! – VAGABOND 39 mins ago
ok,so the previous canvas cant be used when you comeback on it?? – VAGABOND 36 mins ago
you can understand if you look jsfiddle.. generate a canvas and try to draw something to first one you can see that it will draw the second one.. this is the problem.. – regeme 34 mins ago
when you click the previous one you cannot see any drawing on it thats ok but it is drawing the next one which is weird.. you move your mouse on first canvas but it draws on next one – regeme 30 mins ago
the reason is.its performing drawing on the same id...after creating canvas what you need is to assign id to that 2nd canvas...detect mouseover of respective canvas...and switch drawing to respective canvas – VAGABOND 26 mins ago
show 4 more commentsadd comment (requires an account with 50 reputation)

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.