JS - 防止鼠标离开屏幕

Posted

技术标签:

【中文标题】JS - 防止鼠标离开屏幕【英文标题】:JS - Prevent mouse from leaving screen 【发布时间】:2018-08-23 12:25:52 【问题描述】:

我需要防止光标离开窗口。 我已经读过这是不可能的,但是我已经通过 Unity 项目的 WebGL 导出完成了这项工作。您首先必须单击画布,然后浏览器会向您显示一条通知,告诉您您应该按“退出”退出并返回光标。 既然 Unity WebGL 画布可以做到这一点,我假设它可以在没有 Unity 的情况下完成?

【问题讨论】:

即使有可能,也不要这样做,因为它对用户不友好,除非有正当理由这样做,例如在游戏中。 其实是为了游戏。 【参考方案1】:

使用 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  >
    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();
    );
  

【讨论】:

以上是关于JS - 防止鼠标离开屏幕的主要内容,如果未能解决你的问题,请参考以下文章

PyGame-角色离开屏幕

UITableView - 当它们离开屏幕时不要卸载单元格

使用固定位置时,jQuery UI 对话框离开屏幕

防止反应导航 StackNavigator 的后退行为

我想用JS HTML CSS JQuery创建一个预加载屏幕(可选),没有后端

JS 获取屏幕中鼠标的坐标值