8

Here's a fiddle: http://jsfiddle.net/MZ9Xm/

Note: The following occurs in Chrome 22.0.1221.1, but not in Firefox 14.0.1. [Ubuntu linux]

Move your mouse to the top canvas and press and hold the mouse button. Drag the mouse, and the cursor will change to a text-selection mouse cursor (I-bar). This does not happen if there are no other elements on the page.

I've messed around with setting user-selection to none, but have not had any luck.

shino
  • 4,562
  • 5
  • 38
  • 57
  • Linking [this question](http://stackoverflow.com/q/4945874/94197), some of the comments discuss this bug. – Andy E May 12 '13 at 11:56

3 Answers3

25

You can bind a mousedown event in your canvas to prevent default behavior.

Something like:

// with jQuery
$( "#canvasId" ).mousedown(function(event){
    event.preventDefault();
});

// without jQuery
document.getElementById( "canvasId" ).onmousedown = function(event){
    event.preventDefault();
};

Here is the updated fiddle: http://jsfiddle.net/MZ9Xm/1/

You will need to test this to see if there is some side effect in what you are doing.

davidbuzatto
  • 9,207
  • 1
  • 43
  • 50
3

Have you tried using the CSS cursor property ?

canvas {
    cursor: pointer;
}

It should display the default cursor.

m-r-r
  • 555
  • 6
  • 15
  • Yes, I tried this but no dice. Try it out in the fiddle. The cursor still changes to a text-selection cursor. (In Chrome 22.0.1221.1. I don't have to do anything at all to get it to work with Firefox 14.0.1.) – shino Aug 04 '12 at 02:12
  • It should be a Chrome bug... Anyway, they had the same problem in [this thread](http://stackoverflow.com/questions/2659999/html5-canvas-hand-cursor-problems) and it seems they resolved it. – m-r-r Aug 04 '12 at 02:21
  • Great! I found the answer there (it was Damon's answer). I'm not sure about the protocol here. I guess if you change this answer to link to damon's, I will accept it? – shino Aug 04 '12 at 02:27
  • Unfortunately, it seems the stackoverfow's edit button doesn't works: i get a [beautiful error page](http://stackoverflow.com/posts/11805366/edit) :-p Thanks anyway – m-r-r Aug 04 '12 at 02:33
1

Try this:

var canvasEls = document.getElementsByTagName('canvas'),
    preventHl = function () { return false; },
    i = 0;

while (i < canvasEls.length) {
    canvasEls[i].onmousedown = preventHl;
    i += 1;
}

Returning false will prevent default actions such as highlighting from occurring.

Brian Ustas
  • 62,713
  • 3
  • 28
  • 21