Make canvas unselected

I have a canvas on which I draw a JavaScript game. The problem is that when the player moves the mouse, half the time they end up choosing a canvas that looks ugly.

I tried googling around and using some CSS like -webkit-user-select: none; and options, but nothing works with the canvas.

+3
source share
2 answers

Your problem is not that canvas is selectable, but you are not telling the browser that you want the mouse to be used exclusively for your game.

In your handlers mousedown/ mouseup/ clickyou must run event.preventDefault()or return false.

You must do the same in keyboard events to avoid colliding with keyboard shortcuts.

+5
source

canvas.onselectstart = function () { return false; }

+1
source

All Articles