[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/API/Pointer_Lock_API [Back]  [Original]

API - Web API | MDN

MDN Web Docs

View in English Always switch to English

API

Want more browser support for this feature? Tell us why.

API API 3D

API

3D

  • API

/

requestPointerLock()

API API DOM requestPointerLock() canvas

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

canvas.requestPointerLock();

pointerLockElement exitPointerLock()

API Document pointerLockElement Document exitPointerLock()

pointerLockElement

pointerLockElement

js
if (
  document.pointerLockElement === canvas ||
  document.mozPointerLockElement === canvas
) {
  console.log("The pointer lock status is now locked");
} else {
  console.log("The pointer lock status is now unlocked");
}

Document.exitPointerLock() requestPointerLock() pointerlockchange pointerlockerror

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

// 
document.exitPointerLock();

pointerlockchange

requestPointerLock() exitPointerLock() ESC pointerlockchange document

js
if ("onpointerlockchange" in document) {
  document.addEventListener("pointerlockchange", lockChangeAlert, false);
} else if ("onmozpointerlockchange" in document) {
  document.addEventListener("mozpointerlockchange", lockChangeAlert, false);
}

function lockChangeAlert() {
  if (
    document.pointerLockElement === canvas ||
    document.mozPointerLockElement === canvas
  ) {
    console.log("The pointer lock status is now locked");
    // 
  } else {
    console.log("The pointer lock status is now unlocked");
    // 
  }
}

pointerlockerror

requestPointerLock() exitPointerLock() pointerlockerror document

js
document.addEventListener("pointerlockerror", lockError, false);
document.addEventListener("mozpointerlockerror", lockError, false);

function lockError(e) {
  alert("Pointer lock failed");
}

: Firefox 50 moz

Mouse

API MouseEvent movement 2 movementX movementY MouseEvent screenX screenY 2 mousemove eNow ePrevious movementX eNow.screenX - ePrevious.screenX

MouseEvent clientXclientYscreenXscreenY movementX movementY movementX movementY

movementX movementY

movementX movementY 0

()JavaScript <canvas> canvas

canvas x y

js
const x = 50;
const y = 50;

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

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

requestPointerLock()

js
canvas.onclick = () => {
  canvas.requestPointerLock();
};

pointerlockchange lockChangeAlert()

js
// 

// 
document.addEventListener("pointerlockchange", lockChangeAlert, false);
document.addEventListener("mozpointerlockchange", lockChangeAlert, false);

pointLockElement updatePosition()

js
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);
  }
}

updatePosition() (x y) if() requestAnimationFrame() canvasDraw() X Y tracker

js
const tracker = document.getElementById("tracker");

let 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(() => {
      animation = null;
      canvasDraw();
    });
  }
}

canvasDraw() x y

js
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();
}

iframe

iframe 1 iframe iframe iframe iframe

iframe iframe <iframe sandbox="allow-pointer-lock">

Pointer Lock 2.0

api.Document.exitPointerLock

api.Element.requestPointerLock


Web Proxy Viewer  |  New URL  |  Original Page