Skip to content
Advertisement

Drawing at cursor position on canvas with JavaScript

I am trying to draw over a canvas by clicking and dragging the mouse. My problem is that, apart from the fact that the line has a very poor quality (I want a more pronounced border) it only respects the mouse position when this is at 0,0. As I move the mouse to the lower corner, the line increments its distance from it as much as when I am in the middle of the canvas, the line is already out of it. I have my code at: http://jsfiddle.net/ajTkP/12/

I will also post it here:

JavaScript

Thanks for your help!

Advertisement

Answer

You need to set an explicit width and height on the canvas. The default dimensions of a canvas are a width of 300 and a height of 150 (see the spec here). By setting the width and height via CSS you are just stretching the canvas.

Either do:

JavaScript

or set the width/height via JavaScript:

JavaScript

See the updated jsfiddle: http://jsfiddle.net/ajTkP/13/

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