When I click on a canvas and drag my mouse, the cursor changes to a text-selection cursor. How can I prevent this?

徘徊边缘 提交于 2019-12-03 13:09:00

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.

Have you tried using the CSS cursor property ?

canvas {
    cursor: pointer;
}

It should display the default cursor.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!