使画布不可选择
我有一个画布,我正在上面绘制一个 JavaScript 游戏。问题是,当玩家移动鼠标时,有一半的时间他们最终会选择看起来很难看的画布。
我尝试过谷歌搜索并使用一些CSS,例如 -webkit-user-select: none;和变体,但似乎没有任何东西适用于画布。
I have a canvas onto which I am drawing a JavaScript game. The problem is that when the player moves the mouse, half the time they end up selecting the canvas which looks ugly.
I have tried googling around and using some CSS like -webkit-user-select: none; and variants, but nothing seems to work with a canvas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题不在于画布是可选的,而是您没有告诉浏览器您希望鼠标专门用于您的游戏。
在您的
mousedown
/mouseup
/click
处理程序中,您应该运行event.preventDefault()
或return false
。您应该在键盘事件中执行相同的操作,以避免与键盘快捷键发生冲突。
Your problem is not that canvas is selectable, but that you're not telling the browser that you want mouse used exclusively for your game.
In your
mousedown
/mouseup
/click
handlers you should runevent.preventDefault()
orreturn false
.You should do the same thing in keyboard events to avoid colliding with keyboard shortcuts.
canvas.onselectstart = function () { return false; } }
canvas.onselectstart = function () { return false; }