Answer the question
In order to leave comments, you need to log in
How to draw rectangles on canvas like in Paint?
You need to draw a rectangle on the canvas, as is done in Paint. Namely: poke at a certain point on the canvas and drag the diagonal. Here is what I have at the moment
canvas.addEventListener("mousedown", (e) => {
last_mousex = e.clientX - canvasx;
last_mousey = e.clientY - canvasy;
mousedown = true;
});
canvas.addEventListener("mouseup", (e) => {
mousedown = false;
});
canvas.addEventListener("mousemove", (e) => {
mousex = e.clientX - canvasx;
mousey = e.clientY - canvasy;
if (mousedown) {
ctx.clearRect(0, 0, canvas.width, canvas.height); //clear canvas
ctx.beginPath();
var width = mousex - last_mousex;
var height = mousey - last_mousey;
ctx.rect(last_mousex, last_mousey, width, height);
ctx.strokeStyle = "black";
ctx.lineWidth = 10;
ctx.stroke();
}
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question