JavaScript Element requestPointerLock() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Limited availability
Instance method

What You’ll Learn

Element.requestPointerLock() is an instance method from the Pointer Lock API. It locks the mouse pointer on an element—ideal for FPS games and 3D viewers. Learn Promise handling, unadjustedMovement, pointerlockchange events, and five try-it labs.

01

Kind

Instance method

02

Action

Lock pointer

03

Returns

Promise

04

Gesture

User activation

05

Exit

exitPointerLock()

06

Status

Limited availability

Introduction

First-person games and 3D demos often need the mouse to control a camera without the cursor leaving the game area or hitting the screen edge. requestPointerLock() hides the pointer and reports movement through movementX and movementY on pointermove events.

The call is asynchronous: it returns a Promise. MDN also recommends listening for pointerlockchange and pointerlockerror on document to track success or failure across browsers. You must call it from a user gesture.

💡
Beginner tip

Use document.pointerLockElement to see which element currently has the lock. Press Esc or call document.exitPointerLock() to unlock.

This page is part of JavaScript Element. Related: releasePointerCapture(), requestFullscreen(), and the JavaScript hub.

Understanding the requestPointerLock() Method

Calling el.requestPointerLock() (or with an options object) asks the browser to confine pointer events to that element and hide the system cursor.

  • It is an instance method on Element (Pointer Lock API).
  • Returns a Promise resolved with undefined on success (MDN).
  • Requires transient user activation (MDN).
  • Movement is read via movementX / movementY on pointer events.
  • Option unadjustedMovement: true disables OS mouse acceleration (MDN).
  • Listen for pointerlockchange and pointerlockerror on document.

📝 Syntax

General forms of Element.requestPointerLock (MDN):

JavaScript
requestPointerLock()
requestPointerLock(options)

Parameters

  • options (optional) — object with:
    • unadjustedMovement — boolean, default false. When true, disables OS mouse acceleration for raw input (MDN).

Return value

A Promise resolved with undefined when the lock succeeds (MDN).

Exceptions

  • The promise may reject if the request fails. Also handle pointerlockerror on document for broader browser support (MDN).

Common patterns

JavaScript
const canvas = document.getElementById("canvas");

// Basic — call from a click handler
canvas.addEventListener("click", async () => {
  await canvas.requestPointerLock();
});

// Raw mouse input for FPS games
canvas.requestPointerLock({ unadjustedMovement: true });

// Check lock state
if (document.pointerLockElement === canvas) {
  document.exitPointerLock();
}

⚡ Quick Reference

GoalCode
Lock pointerel.requestPointerLock()
Raw mouse inputel.requestPointerLock({ unadjustedMovement: true })
Check active elementdocument.pointerLockElement
Exit pointer lockdocument.exitPointerLock()
Return valuePromise<undefined>
MDN statusLimited availability (not Baseline)

🔍 At a Glance

Four facts to remember about Element.requestPointerLock().

Returns
Promise

Resolves undefined

Status
limited

Not Baseline

Movement
movementX/Y

On pointermove

Check
pointerLockElement

Current target

📋 requestPointerLock() vs setPointerCapture() vs requestFullscreen()

requestPointerLock()setPointerCapture()requestFullscreen()
PurposeHide cursor; FPS inputRoute events to elementFill the screen
CursorHiddenVisibleVaries
Movement datamovementX/YNormal pointer coordsN/A
ExitEsc / exitPointerLockreleasePointerCaptureEsc / exitFullscreen
Best for3D games, viewersDrag sliders, drawingVideo, immersive UI

Examples Gallery

Examples follow MDN Element.requestPointerLock() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

MDN canvas click lock and raw mouse input.

Example 1 — Canvas Click Lock (MDN)

Lock the pointer when the user clicks a game canvas.

JavaScript
const canvas = document.getElementById("canvas");

canvas.addEventListener("click", async () => {
  await canvas.requestPointerLock();
});
Try It Yourself

How It Works

MDN: call requestPointerLock() from a user interaction. The cursor hides and pointer events stay on the canvas.

Example 2 — unadjustedMovement (MDN)

Disable OS mouse acceleration for FPS-style camera control.

JavaScript
canvas.addEventListener("click", async () => {
  await canvas.requestPointerLock({
    unadjustedMovement: true,
  });
});
Try It Yourself

How It Works

By default OS acceleration is on. unadjustedMovement: true requests raw mouse deltas—preferred by many FPS games (MDN).

📈 Practical Patterns

Movement handling, events, and exiting lock.

Example 3 — Read movementX / movementY

Rotate or pan a view using pointer movement while locked.

JavaScript
let yaw = 0;

document.addEventListener("pointermove", (event) => {
  if (document.pointerLockElement !== canvas) return;
  yaw += event.movementX * 0.1;
  canvas.style.transform = `rotate(${yaw}deg)`;
});
Try It Yourself

How It Works

While locked, use movementX and movementY instead of clientX/clientY for relative motion.

Example 4 — pointerlockchange Listener

Update UI when lock state changes (MDN recommendation).

JavaScript
const status = document.getElementById("status");

document.addEventListener("pointerlockchange", () => {
  if (document.pointerLockElement) {
    status.textContent = "Locked: " + document.pointerLockElement.id;
  } else {
    status.textContent = "Pointer unlocked";
  }
});
Try It Yourself

How It Works

MDN: listen for pointerlockchange and pointerlockerror on document to track success or failure across browsers.

Example 5 — Exit With exitPointerLock()

Programmatically release the lock from a button.

JavaScript
document.getElementById("unlock").addEventListener("click", () => {
  if (document.pointerLockElement) {
    document.exitPointerLock();
  }
});
Try It Yourself

How It Works

Users can press Esc by default. Your UI can also call document.exitPointerLock() for an explicit unlock button.

🚀 Common Use Cases

  • First-person shooter camera control on a game canvas.
  • 3D model viewers that need unlimited mouse rotation.
  • Architectural walkthroughs and virtual tours in the browser.
  • Map applications with drag-to-look navigation.
  • Creative tools that use relative pointer motion while drawing.
  • Teaching the Pointer Lock API with movement and event patterns.

🧠 How requestPointerLock() Works

1

User activates the page

A click provides transient user activation on an active document (MDN).

Gesture
2

Call requestPointerLock on target

You call el.requestPointerLock() on the canvas or container.

Request
3

Browser locks the pointer

The cursor hides; movement is reported via movementX/Y.

Lock
4

Promise resolves; pointerlockchange fires

document.pointerLockElement points at the locked element.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN. Limited availability (not Baseline).
  • Returns a Promise (MDN). Also listen for pointerlockchange / pointerlockerror.
  • Requires transient user activation (MDN).
  • Iframes need the allow-pointer-lock sandbox token (MDN).
  • Only one element can be pointer-locked at a time.
  • Pair with document.exitPointerLock() and check document.pointerLockElement.
  • Related: releasePointerCapture(), requestFullscreen(), JavaScript hub.

Limited Availability Support

Element.requestPointerLock() is part of the Pointer Lock API and is not Baseline on MDN. Desktop browsers support it well for games; mobile support is limited. Always feature-detect, call from a user gesture, and handle promise rejection plus pointerlockerror.

Limited availability · Not Baseline

Element.requestPointerLock()

Async request to lock the mouse pointer on an element.

Limited Not Baseline
Google Chrome Supported · Desktop
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop
Yes
Apple Safari Limited · check target platform
Limited
Opera Supported · Chromium-based
Yes
Internet Explorer Legacy mozPointerLockElement APIs
No
requestPointerLock() Limited

Bottom line: Call from a click handler, use movementX/Y while locked, listen for pointerlockchange, and test on desktop targets — mobile pointer lock is often unavailable.

Conclusion

Element.requestPointerLock() is the standard way to hide the cursor and read relative mouse movement for games and 3D viewers. It returns a Promise, needs a user gesture, and pairs with document.pointerLockElement and document.exitPointerLock().

Continue with requestFullscreen(), releasePointerCapture(), scroll(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Call requestPointerLock() from a click handler
  • Use movementX / movementY while locked
  • Listen for pointerlockchange to sync UI state
  • Offer unadjustedMovement: true for FPS games when supported
  • Provide Esc instructions or an unlock button

❌ Don’t

  • Request lock on page load without user activation
  • Assume mobile browsers support pointer lock
  • Confuse pointer lock with setPointerCapture()
  • Ignore pointerlockerror when the request fails
  • Trap users with no way to exit lock mode

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.requestPointerLock()

Lock the pointer from a click—read relative motion with movementX/Y.

5
Core concepts
📄 02

Gesture

required

Security
✍️ 03

Motion

movementX/Y

Input
04

Status

limited

MDN
05

Exit

exitPointerLock

Pair

❓ Frequently Asked Questions

It asynchronously asks the browser to lock the pointer on the given element (MDN). The mouse cursor is hidden and movement is delivered via movementX and movementY on pointer events.
No. MDN does not mark it Deprecated, Experimental, or Non-standard. It is Limited availability (not Baseline) because support varies across browsers and platforms.
A Promise that resolves with undefined when the lock succeeds (MDN). Also listen for pointerlockchange and pointerlockerror on document for broader compatibility.
Yes. MDN: transient user activation is required — call it from a click or other user gesture on an active document.
An optional option that disables OS mouse acceleration and uses raw input (MDN). Set { unadjustedMovement: true } for FPS-style camera control.
Press Esc (default unlock gesture) or call document.exitPointerLock(). Listen for pointerlockchange and check document.pointerLockElement.
Did you know?

Pointer lock is different from setPointerCapture(), which routes events to an element but keeps the cursor visible. Pointer lock hides the cursor and is built for unlimited relative movement in games and 3D apps.

Next: scroll()

Continue the Element series with programmatic scrolling.

scroll() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful