画布:基于指针的视口/包含鼠标指针

发布于 2024-10-21 20:36:32 字数 366 浏览 2 评论 0原文

似乎没有一个好的方法可以使 视口基于鼠标指针的位置并能够自由移动。基本上,就像市场上所有其他第一人称游戏一样。

  1. 没有办法捕获鼠标 在 元素内。
  2. 没有办法设置位置 的鼠标指针。
  3. 无法全屏显示 使用 ,即使一旦到达边缘,功能也会被破坏。

也有充分的理由。想象一下恶意者可能(并且肯定会)采用什么可能的场景。

也许现在考虑一些几乎只在 3D 环境中有用的东西还为时过早,而且还没有规范。

您的采取或解决方案是什么?

There doesn't seem to be a good way to base the <canvas> viewport on the location of the mouse pointer and being able to move around freely. Basically, like every other first-person game on the market.

  1. There is no way to capture the mouse
    inside a <canvas> element.
  2. There is no way to set the position
    of the mouse pointer.
  3. It is not possible to go full screen
    with <canvas>, and even if, once the edge has been reached, functionality will be broken.

For good reasons, too. Imagine what possible scenarios could (and definitely would) be employed by malicious persons.

Perhaps it's too early to be thinking of something that is almost only of any use in a 3D environment, something that there isn't yet a spec for.

What's your take or solution?

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

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

发布评论

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

评论(2

女中豪杰 2024-10-28 20:36:32

可以获取画布内的鼠标位置。

function getCursorPosition(e) {
    var x;
    var y;
    if (e.pageX != undefined && e.pageY != undefined) {
    x = e.pageX;
    y = e.pageY;
    }
    else {
    x = e.clientX + document.body.scrollLeft +
            document.documentElement.scrollLeft;
    y = e.clientY + document.body.scrollTop +
            document.documentElement.scrollTop;
    }

    x -= gCanvasElement.offsetLeft;
    y -= gCanvasElement.offsetTop;

    var cell = new Cell(Math.floor(y/kPieceHeight),
                    Math.floor(x/kPieceWidth));
    return cell;
}

深入了解 HTML5:让我们称之为绘图表面

You can get the mouse position inside of a canvas.

function getCursorPosition(e) {
    var x;
    var y;
    if (e.pageX != undefined && e.pageY != undefined) {
    x = e.pageX;
    y = e.pageY;
    }
    else {
    x = e.clientX + document.body.scrollLeft +
            document.documentElement.scrollLeft;
    y = e.clientY + document.body.scrollTop +
            document.documentElement.scrollTop;
    }

    x -= gCanvasElement.offsetLeft;
    y -= gCanvasElement.offsetTop;

    var cell = new Cell(Math.floor(y/kPieceHeight),
                    Math.floor(x/kPieceWidth));
    return cell;
}

From Dive Into HTML5: Let's Call it a Draw(ing Surface)

提笔落墨 2024-10-28 20:36:32

我认为对此没有一个好的解决方案——至少在我们获得鼠标锁定之前是这样。无论您的解决方案多么优雅,如果您制作了一个由鼠标外观驱动的抽搐游戏,用户将在某个时刻在画布区域之外抽搐。即使他们没有意外点击链接,当视图停止响应鼠标时,他们的沉浸感也会被破坏。

对于节奏较慢的游戏,您可以:

  • 使用单击并拖动来转动。一旦用户开始在画布内拖动,您就可以使用从开始拖动点开始的鼠标增量来确定转动多远。由于用户按住按钮,因此他们不会意外单击某些内容。
  • 将光标悬停在画布边缘附近即可转动,类似于 RTS。这会变得更慢,但可能是最直观的,并且最容易让用户意外发现。
  • 使用键盘看起来就像鼠标前的 FPS 游戏(例如 Doom)。

值得注意的是,Firefox 中有一个关于鼠标锁定的开放功能请求。但不幸的是,这个、鼠标隐藏或全屏都不是 WebGL 规范的一部分。

Unity 支持所有这些功能,因此如果您确实需要 FPS 控件,这可能是一个值得考虑的途径。

I don't think there is a good solution for this -- at least, not until we get mouse locking. No matter how elegant your solution, if you make a twitchy mouselook driven game, the user is going to twitch outside the canvas area at some point. Even if they don't accidentally click a link, their immersion will be broken when the view stops responding to their mouse.

For slower paced games, you could:

  • Use click and drag to turn. Once user starts dragging within the canvas, you use the mouse delta from the point where they started dragging to determine how far to turn. Because the user is holding down the button, they won't accidentally click things.
  • Hover cursor near the edges of the canvas to turn, similar to an RTS. This would turn more slowly, but is probably the most intuitive, and easiest for a user to accidentally discover.
  • Use the keyboard to look, like pre-mouse FPS games (such as Doom).

It's worth noting that there is an open feature request in Firefox for mouse locking. But, unfortunately, neither this, nor mouse hiding or fullscreen are part of the WebGL spec.

All of these features are supported by Unity, so that may be a path to look at if you really need FPS controls.

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