var canvas=document.getElementById("canvas"); image = new Image(); image.src = 'https://d13yacurqjgara.cloudfront.net/users/4094/screenshots/2846992/drib106_1x.jpg'; image.onload = function() { ctx.drawImage(image, 0, 0); $(canvas).css('background-image', 'url(https://d13yacurqjgara.cloudfront.net/users/413551/screenshots/2846695/06_img_dribbble_2_2_1x.jpg)'); }; var ctx=canvas.getContext("2d"); var lastX; var lastY; var strokeColor="red"; var strokeWidth=5; var mouseX; var mouseY; var canvasOffset=$("#canvas").offset(); var offsetX=canvasOffset.left; var offsetY=canvasOffset.top; var isMouseDown=false; function handleMouseDown(e){ mouseX=parseInt(e.clientX-offsetX); mouseY=parseInt(e.clientY-offsetY); // Put your mousedown stuff here lastX=mouseX; lastY=mouseY; isMouseDown=true; } function handleMouseUp(e){ mouseX=parseInt(e.clientX-offsetX); mouseY=parseInt(e.clientY-offsetY); // Put your mouseup stuff here isMouseDown=false; } function handleMouseOut(e){ mouseX=parseInt(e.clientX-offsetX); mouseY=parseInt(e.clientY-offsetY); // Put your mouseOut stuff here isMouseDown=false; } function handleMouseMove(e){ mouseX=parseInt(e.clientX-offsetX); mouseY=parseInt(e.clientY-offsetY); // Put your mousemove stuff here if(isMouseDown){ ctx.beginPath(); ctx.globalCompositeOperation="destination-out"; ctx.arc(lastX,lastY,8,0,Math.PI*2,false); ctx.fill(); lastX=mouseX; lastY=mouseY; } } $("#canvas").mousedown(function(e){handleMouseDown(e);}); $("#canvas").mousemove(function(e){handleMouseMove(e);}); $("#canvas").mouseup(function(e){handleMouseUp(e);}); $("#canvas").mouseout(function(e){handleMouseOut(e);}); var mode="pen"; $("#pen").click(function(){ mode="pen"; }); $("#eraser").click(function(){ mode="eraser"; }); #canvas:hover { cursor: pointer; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <canvas id="canvas" width="300" height="300"></canvas> I already have a ready canvas and a function for cleaning the image and showing the bottom layer of the image.
Just help me change the shape of the cleaning area of the cursor (now it is round) to an arbitrary (if possible).
And so that on cell phones (on the sensor) it worked (with a double press or with a long press), but now on the sensors only scrolling takes place instead of cleaning.
ctx.arc(lastX,lastY,8,0,Math.PI*2,false);- instead of it, you can draw any shape and also fill it up - Grundy