Skip to content
Advertisement

Weird shape when drawing line on canvas (js)

I’m trying to make a very simple paint in JS but I have a problem when I increase the line width it create weird shape at the end of shape but not when I use a lineWidth = 1 and I don’t know where the problem come from furthermore it create space between lines while with a lineWidth=1 I don’t have that problem

without problem :
lineWidth = 1

with problem :
lineWidth = 15

this is my code :

JavaScript

}

Advertisement

Answer

This happens because of two things:

  1. Inside your draw() function you’re setting the starting point and the end point with every call. Usually you determine the start as soon as the user pushes the mousebutton – once.
  2. even with #1 fixed, the line end might still look a bit ‘fuzzy’. This can be fixed by setting the context’s lineCap style to round instead of it’s default butt – which squares of line endpoints.

Here’s an example based on your code (just click ‘Run code snippet’):

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement