简体   繁体   English

在HTML5 Canvas Javascript中隐藏鼠标光标

[英]Hide Mouse Cursor when it's within HTML5 Canvas Javascript

I'm in the process of making a game using Javascript and the html5 canvas element as an alternative to Flash. 我正在使用Javascript制作游戏,并使用html5 canvas元素作为Flash的替代品。 My question is: is there any bit of code I can use to hide the mouse cursor/pointer as it passes within the canvas? 我的问题是:是否有任何代码可以用来隐藏鼠标光标/指针,因为它在画布中传递? Help is much appreciated! 非常感谢帮助!

I don't think you need Javascript for this, you can just use CSS. 我认为你不需要Javascript,你可以使用CSS。 Assign your canvas a div id/class, and then use this in your CSS template: 为画布指定div id / class,然后在CSS模板中使用它:

* {cursor: none;}

You can use javascript to manipulate cursor style. 您可以使用javascript来操作光标样式。 Code: 码:

<div id="canvas_div_no_cursor">
   <!-- canvas here -->
</div>
<script type="text/javascript">
  document.getElementById('canvas_div_no_cursor').style.cursor = "none";
</script>

The simplest way (on modern browsers) is to set the cursor to none on the canvas. 最简单的方法(在现代浏览器上)是在画布上将光标设置为none

If your canvas is created in HTML, do: 如果画布是用HTML创建的,请执行以下操作:

<canvas id="canvas" style="cursor: none;"></canvas>

I would favour this over a style-sheet because you want to guarantee the cursor value is not over-ridden. 我赞成使用样式表,因为您希望保证游标值不会被覆盖。

If your canvas is created in JavaScript, do: 如果画布是用JavaScript创建的,请执行以下操作:

const canvas = document.createElement('canvas');
canvas.style.cursor = 'none';
document.body.appendChild(canvas);

Select the canvas element: 选择canvas元素:

 canvas = document.getElementById("canvas");

Get context : 获取上下文:

context = canvas.getContext("2d");

Set none : 设置无:

context.strokeStyle = none; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM