Created
September 23, 2018 01:01
-
-
Save wmcmurray/4ee113059b0f65da515fa7bcd58b2b58 to your computer and use it in GitHub Desktop.
A quick and simple implementation of the pointer lock API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PointerLockManager { | |
constructor(elem) { | |
this.elem = elem || document.body; | |
this.elem.addEventListener('click', this.lock.bind(this)); | |
document.addEventListener('pointerlockchange', this.onChangeHandler.bind(this)); | |
} | |
isLocked() { | |
return document.pointerLockElement === this.elem; | |
} | |
lock() { | |
if(!this.isLocked()){ | |
this.elem.requestPointerLock(); | |
} | |
} | |
unlock() { | |
if(this.isLocked()){ | |
document.exitPointerLock(); | |
} | |
} | |
onChangeHandler() { | |
console.log('pointer is '+(this.isLocked() ? 'locked' : 'unlocked')); | |
} | |
} | |
export default PointerLockManager |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment