javascript - JS - 防止鼠标离开屏幕

标签 javascript css unity-game-engine unity-webgl

我需要防止光标离开窗口。 我读到这是不可能的,但是我已经通过从 Unity 元素导出 WebGL 来做到这一点。您首先必须单击 Canvas ,然后浏览器会向您显示一条通知,提示您应按“退出”退出并返回光标。 既然 Unity WebGL canvas 可以做到,我认为没有 Unity 也可以做到吗?

最佳答案

使用 HTML5 指针锁定 API

https://developer.mozilla.org/en-US/docs/Web/API/Pointer_Lock_API

下面不是我的代码示例,所有信用

https://github.com/mdn/dom-examples/tree/master/pointer-lock

HTML

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Pointer lock demo</title>
  <link type="text/css" rel="stylesheet" href="style.css">
</head>
<body>
  <div class="information">
    <h1>Pointer lock demo</h1>

    <p>This demo demonstrates usage of the pointer lock API. Click on the canvas area and your mouse will directly control the ball inside the canvas, not your mouse pointer. You can press escape to return to the standard expected state.</p>
  </div>

  <canvas width="640" height="360">
    Your browser does not support HTML5 canvas
  </canvas>
  <div id="tracker"></div>

  <script src="app.js"></script>
</body>
</html>

JS

// helper function
const RADIUS = 20;

function degToRad(degrees) {
  var result = Math.PI / 180 * degrees;
  return result;
}

// setup of the canvas

var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');

var x = 50;
var y = 50;

function canvasDraw() {
  ctx.fillStyle = "black";
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  ctx.fillStyle = "#f00";
  ctx.beginPath();
  ctx.arc(x, y, RADIUS, 0, degToRad(360), true);
  ctx.fill();
}
canvasDraw();

// pointer lock object forking for cross browser

canvas.requestPointerLock = canvas.requestPointerLock ||
                            canvas.mozRequestPointerLock;

document.exitPointerLock = document.exitPointerLock ||
                           document.mozExitPointerLock;

canvas.onclick = function() {
  canvas.requestPointerLock();
};

// pointer lock event listeners

// Hook pointer lock state change events for different browsers
document.addEventListener('pointerlockchange', lockChangeAlert, false);
document.addEventListener('mozpointerlockchange', lockChangeAlert, false);

function lockChangeAlert() {
  if (document.pointerLockElement === canvas ||
      document.mozPointerLockElement === canvas) {
    console.log('The pointer lock status is now locked');
    document.addEventListener("mousemove", updatePosition, false);
  } else {
    console.log('The pointer lock status is now unlocked');  
    document.removeEventListener("mousemove", updatePosition, false);
  }
}

var tracker = document.getElementById('tracker');

var animation;
function updatePosition(e) {
  x += e.movementX;
  y += e.movementY;
  if (x > canvas.width + RADIUS) {
    x = -RADIUS;
  }
  if (y > canvas.height + RADIUS) {
    y = -RADIUS;
  }  
  if (x < -RADIUS) {
    x = canvas.width + RADIUS;
  }
  if (y < -RADIUS) {
    y = canvas.height + RADIUS;
  }
  tracker.textContent = "X position: " + x + ", Y position: " + y;

  if (!animation) {
    animation = requestAnimationFrame(function() {
      animation = null;
      canvasDraw();
    });
  }
}

关于javascript - JS - 防止鼠标离开屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51985774/

相关文章:

javascript - 不同格式的相同日期返回不同的值

javascript - 在jquery中添加哈希

java - 如何在 Selenium 中单击带有 anchor 标记的图像或图标

javascript - 如何使用 JavaScript 将下拉按钮放入 HTML 表格中?

java - 适用于 PC 和平板电脑的浏览器游戏 - 依赖哪个插件?

unity-game-engine - 如何输出Unity3D调试日志?

c# - 将枪放置在固定位置

javascript - 使用 Angular UI Router 形成多个步骤

javascript - Javascript 过程是部分的还是全部的?

javascript - 在加载可视页面之前加载 CSS 'theme'