I have implemented line drawing in canvas. But I faced a problem:
when line width is small (3), curved line looks nice;
when line width is large (20), curved line looks not good because of breaks.

canvas.node.onmousemove = function (e) {
if (!canvas.isDrawing) {
return;
}
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
ctx.beginPath();
ctx.moveTo(canvas.lastX, canvas.lastY);
ctx.strokeStyle = '#000000';
ctx.lineWidth = self.lineWidth();
ctx.lineTo(x, y);
ctx.stroke();
ctx.closePath();
canvas.lastX = x;
canvas.lastY = y;
};
canvas.node.onmousedown = function (e) {
canvas.isDrawing = true;
canvas.lastX = e.pageX - this.offsetLeft;
canvas.lastY = e.pageY - this.offsetTop;
};
canvas.node.onmouseup = function (e) {
canvas.isDrawing = false;
};
How can I avoid breaks in large lines and make my line solid?
Thank you.
Have you tried setting the
lineJoinproperty to your canvas context?Add the following line after you set your
lineWidth.This will not work if you have closed your path before completing rendering all the
lineTo‘s usingctx.closePath().You will have to open the path before you start drawing the line and close it after you have finished drawing the line.
Also if you move the drawing cursor using
moveToduring drawing thelineJoindoesn’t come into play.You can try the following modified code (I have also included a JSFiddle link in the end).
EDIT 1: Updated the code as I forgot to add the
lineJoinproperty to it.EDIT 2: Moved the
moveToline to the appropriate listener method.Working JSFiddle here.