There is a difference between the height and width of the CSS height and the attributes of the HTML canvas: the first defines the space that the canvas occupies on the page; the latter defines the rendering surface. In concreto, suppose you have the following canvas:
<canvas height="400" width="600"></canvas>
with a window size of 1200x800, and the "CSS" canvas is set to width: 100%; height: 100%; width: 100%; height: 100%; , then your canvas will appear as stretched twice as large and blurred in height and width (for example, in your violin, these rectangles are clearly more than 10 pixels). As a result, page coordinates are not synchronized with canvas coordinates .
According to the specification, your canvas rendering surface will be 300x150 because you did not specify width / height attributes:
The default width attribute is 300, and the default height attribute is 150.
Check out the slightly “fixed” version of your script .
As a result, my advice (as a non-specialist on HTML canvas) should always indicate these 2 attributes and not interfere with different sides of rendering and display (of course, not relative such as vw, vh,%, em, ...) if You do not want unpredictable results; although some SO users were looking for a solution .
source share