Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change cursor over HTML5 Canvas when dragging the mouse

Tags:

html

css

canvas

I have made a paint brush type of application using the Canvas Tag . I wanted that when the mouse is on the canvas the Cursor Changes ,

<canvas id="draw" style="cursor: url(image/pencil.cur)">

I have accomplished this but i cant figure out how shall i change the cursor while I Drag the mouse for drawing the image

like image 326
Yahoo Avatar asked Feb 09 '11 14:02

Yahoo


People also ask

How do I change my cursor on canvas?

The canvas element only receives mouse events relative to its whole. To change cursors on sub-sections of the canvas you will indeed have to do hit-testing using mouse position relative to the canvas.

How do I customize my cursor in HTML?

Answer: Use the CSS cursor property You can define a custom cursor using the CSS cursor property. The cursor property takes the comma-separated list of user-defined cursors value followed by the generic cursor.

How do I make my cursor drag?

To move an object, place the mouse cursor over it, press and hold down the left mouse button, then move the mouse while still holding down the left mouse button. When you have "dragged" the object to the location you want, let go of the mouse button.


2 Answers

For any one still looking for a solution to this webkit bug: https://bugs.webkit.org/show_bug.cgi?id=105355, this is what I did.

I added this property to the body element to make all text unselectable in my page and that's it.

body {
    -webkit-touch-callout: none;

    -webkit-user-select: none;

    -moz-user-select: none;

    user-select: none;
}

The problem is that if you really need an element to be selectable, you will have to change its CSS to make it so.

like image 38
Callistino Avatar answered Oct 19 '22 16:10

Callistino


Use the :active CSS pseudo-class to change the cursor when the mouse button is down over the element:

#draw:active { 
    cursor: url(image/pencil.cur);
}

Working example: http://jsfiddle.net/nXv63/

like image 141
Andy E Avatar answered Oct 19 '22 14:10

Andy E