当我在 Firefox 上时,画布调用requestPointerLock
并按 F11 进行全屏显示 - 我看到该事件mousemove
正在被触发。这使得一些必须是静态的东西移动,因为鼠标没有移动。
我试图创建一个演示,但我得到了错误Blocked pointer lock on an element because the element's frame is sandboxed and the 'allow-pointer-lock' permission is not set.
因此,这是一个您至少可以阅读的代码示例。
HTML部分:
<canvas id="canvas" width="800" height="600"></canvas>
JavaScript 部分:
canvas = document.getElementById('canvas');
document.addEventListener("click", function(e) {
canvas.requestPointerLock = canvas.requestPointerLock ||
canvas.mozRequestPointerLock ||
canvas.webkitRequestPointerLock;
if (canvas.requestPointerLock)
canvas.requestPointerLock();
}, false);
document.addEventListener("pointerlockchange", plChange, false);
document.addEventListener("mozpointerlockchange", plChange, false);
function plChange(e) {
var controlEnabled = (document.mozPointerLockElement === canvas ||
document.webkitPointerLockElement === canvas ||
document.pointerLockElement === canvas);
if (!controlEnabled) {
window.removeEventListener("mousemove", mouseMove);
} else {
window.addEventListener("mousemove", mouseMove, false);
}
}
function mouseMove(e) {
// This is being executed on window resize
console.log(e.movementY);
}
结果,当窗口全屏显示时(而 Firefox 缓慢地进行) - 我在控制台中打印e.movementY并且值并不总是0。
问题是如何防止这个 Firefox “功能”,以便不触发鼠标移动事件?