使用鼠标在 HTML 画布中控制游戏

发布于 2024-10-09 02:35:56 字数 168 浏览 0 评论 0原文

我试图找出 JavaScript 和 HTML 是否有可能控制鼠标并引入相对移动控制 - 在第一人称游戏中获得的鼠标控制类型(其中鼠标可以向一个方向移动,并且玩家可以将无限转动直到鼠标停止)。

我认为实现这一目标的唯一方法是通过 JavaScript 控制鼠标,但我确信这是不可能的。有人对此有什么想法吗?

I am trying to find out if it is possible for JavaScript and HTML's to take control of the mouse and introduce relative movement control - the type of mouse control you get in first person games (where the mouse can be moved in one direction and the player will infinitely turn until the mouse stops).

The only way I can think this could be achieved is by taking control of the mouse via JavaScript, which I'm sure is not possible. Does anyone have any ideas on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不羁少年 2024-10-16 02:35:56

好消息,老问题,随着鼠标锁的引入,https://developer.mozilla.org/en /API/Mouse_Lock_API ,可以捕获相对移动。

Good news old question, with the introduction of Mouse Lock, https://developer.mozilla.org/en/API/Mouse_Lock_API , you can capture relative movement.

柠檬心 2024-10-16 02:35:56

根据你正在制作的内容,你可以做类似飞行模拟的事情。这意味着,变化率是由鼠标距中心/静止位置的距离决定的,而不是由鼠标的实际增量决定的。但这仅适用于车辆游戏。

Depending on what you are making you could do like flight sims. Meaning, the rate of change is determined by the distance of the mouse from the center/rest position, instead of the actual delta of the mouse. This only works well for vehicle games though.

人生戏 2024-10-16 02:35:56

并不真地。您可以模拟这一点:onmousemove,获取 X 和 Y 坐标,并据此计算光标相对于游戏视口的位置(例如,如果游戏发生在从 [100,100] 到 [200,200] 的正方形中,则将鼠标放在 [59,92 ] 表示“左转”,因为它位于游戏“中心”的左侧 [150,150])。

一个可能的问题:一旦鼠标位于浏览器窗口之外,您就会停止接收事件(要求播放器最大化窗口以获得最佳体验?)。

Not really. You could simulate this: onmousemove, get X and Y coordinates, and calculate from this the cursor's position relative to your game viewport (e.g. if the game happens in a square from [100,100] to [200,200], then mouse at [59,92] means "turn left", as it's to the left of the game's "center" at [150,150]).

A possible problem: you stop receiving events once the mouse is outside the browser window (ask player to maximize window for best experience?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文