I want to draw on an HTML Canvas using a mouse (ex: draw a signature, draw a name,...)
Please help me how can I do? Please give some source code. Thank you
I want to draw on an HTML Canvas using a mouse (ex: draw a signature, draw a name,...)
Please help me how can I do? Please give some source code. Thank you
Here's the most straightforward way to create a drawing application with canvas:
mousedown
,mousemove
, andmouseup
event listener to the canvas DOMmousedown
, get the mouse coordinates, and use themoveTo()
method to position your drawing cursor and thebeginPath()
method to begin a new drawing path.mousemove
, continuously add a new point to the path withlineTo()
, and color the last segment withstroke()
.mouseup
, set a flag to disable the drawing.From there, you can add all kinds of other features like giving the user the ability to choose a line thickness, color, brush strokes, and even layers.
I was looking to use this method for signatures as well, i found a sample on http://codetheory.in/.
I've added the below code to a jsfiddle
Html:
Javascript:
Let me know if you have trouble implementing this. It uses processing.js and has features for changing colors and making the draw point larger and smaller.
check this http://jsfiddle.net/ArtBIT/kneDX/. This should direct you on the right direction
I had to provide a simple example for this subject so I'll share here:
http://jsfiddle.net/Haelle/v6tfp2e1