Skip to content

Instantly share code, notes, and snippets.

@wmcmurray
Created September 23, 2018 01:01
Show Gist options
  • Save wmcmurray/4ee113059b0f65da515fa7bcd58b2b58 to your computer and use it in GitHub Desktop.
Save wmcmurray/4ee113059b0f65da515fa7bcd58b2b58 to your computer and use it in GitHub Desktop.
A quick and simple implementation of the pointer lock API.
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