I am trying to make a layered drawing application (a canvas on with something behind it) where I want to erase and see through the canvas on top of the z-index. But for some reason onmousemove I cant make it continue.
I think something is wrong with the way I deal with the canvas in the draw method but I am not sure.
The following code puts a canvas on top of another canvas where the upper one is blue and the one beneath is red, and the purpose is to erase with a circular shape on the upper blue canvas on mouse move, in order to wipe the canvas with the mouse, like with an eraser in Photoshop or GIMP.
<html>
<script type="text/javascript">
//inspired by http://stackoverflow.com/questions/2368784/draw-on-html5-canvas-using-a-mouse
var canvas, canvasUnder, ctx, ctxUnder, circle, drawing = false,
prevX = 0,
currX = 0,
prevY = 0,
currY = 0;
var x = "black",
y = 2;
var w,h;
function init() {
canvas = document.getElementById('can');
ctx = canvas.getContext("2d");
canvasUnder = document.getElementById('under');
ctxUnder = canvasUnder.getContext("2d");
w = canvas.width;
h = canvas.height;
canvas.addEventListener("mousemove", function (e) {
findxy('move', e)
}, false);
canvas.addEventListener("mousedown", function (e) {
findxy('down', e)
}, false);
canvas.addEventListener("mouseup", function (e) {
findxy('up', e)
}, false);
canvas.addEventListener("mouseout", function (e) {
findxy('out', e)
}, false);
ctx.rect(0,0,canvas.width,canvas.height);
ctx.fillStyle="blue";
ctx.fill();
//fill the canvas under with solid color to start with
ctxUnder.rect(0,0,canvasUnder.width,canvasUnder.height);
ctxUnder.fillStyle="red";
ctxUnder.fill();
}
function draw() {
circle = new Path2D();
circle.arc(currX,currY,16,0,2*Math.PI);
ctx.clip(circle);
ctx.clearRect(0, 0, w, h);
ctx.restore();
}
function findxy(res, e) {
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;
drawing = true;
}
if (res == 'up' || res == "out") {
drawing = false;
}
if (res == 'move') {
if (drawing) {
prevX = currX;
prevY = currY;
currX = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;
draw();
}
}
}
</script>
<body onload="init()">
<canvas id="under" width="400" height="400" style="position:absolute;top:10%;left:10%;border:2px solid;" z-index="1;"></canvas>
<canvas id="can" width="400" height="400" style="position:absolute;top:10%;left:10%;border:2px solid;" z-index="2;"></canvas>
</body>
</html>
The question is: Why does it only work on first call of draw() and not after?
I set up a fiddle for this here: https://jsfiddle.net/avpv1ahr/1/