1

I'm observing strange behaviour when attempting to add a mousemove listener to document after requesting a pointer lock.

I've implemented the following, mostly based on documentation from MDN:

  var onLockChange = () => {
    env.handlers.onLockChange.forEach(lis => { lis(); });
  };
  document.addEventListener('pointerlockchange', onLockChange, false);
  document.addEventListener('mozpointerlockchange', onLockChange, false);
  document.addEventListener('pointerlockerror', (e) => {
    console.error(e);
  }, false);

...

  var updatePosition = (e) => {
    console.log('EVENT!', e);
  };

  env.handlers.onLockChange.push(() => {
    console.log('INVOKED');
    if ( hasPointerLock() ) {
      document.addEventListener('mousemove', updatePosition, false);
      console.log('Added Listener');
    } else {
      document.removeEventListener('mousemove', updatePosition, false);
      console.log('Removed Listener');
    }
  })

  canvas.onclick = () => {
    if ( ! hasPointerLock() ) {
      canvas.requestPointerLock();
    }
  };

I see the console logs exactly as I expect. When I clock on my canvas element, the pointer gets trapped and I see:

INVOKED
Added Listener

but I never see EVENT!.

To make things weirder, if I comment out the call to document.removeEventListener I observe that the mousemove event gets captured but only while locking is off.

KernelDeimos
  • 503
  • 4
  • 12

2 Answers2

4

It's a bug in Google Chrome.

mousemove won't fire under the following conditions: - Console is open, AND - Pointer is locked

Since I had the console open the entire time while debugging my code, I thought my code wasn't working.

I spent an hour trying to figure this shit out. Hopefully the next person doesn't have to.

KernelDeimos
  • 503
  • 4
  • 12
0

Try to use event.movementX and event.movementY instead of clientX/clientY.

Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
J Decker
  • 537
  • 5
  • 9