PointerLock API Updates

A quick update on the Firefox PointerLock API implementation

Lets start with mochitests. While writing mochitests for pointerlock we stumbled on two problems

  1. Not being able to specify how many tests should run (different platforms were running different number of tests)
  2. Mochitest iframe not allowed to go fullscreen, making us run all the tests on a different window

David Humphrey came up with a solution for our first problem and added an “expect” functionality to the mochitest framework.
So now we can specify how many tests should occur when making asynchronous tests, for example:
SimpleTest.waitForExplicitFinish(3)
Bug 724578

For our second problem, I added the attribute mozallowfullscreen=true to the mochitest iframe that runs all tests.
I’m not sure if there was a specific reason for not allowing fullscreen on the mochitest iframe, but if it wasn’t it will simplify a lot writing tests for pointerlock
Bug 728893

Spec Updates

The spec had two major changes

  1. Switching from callbacks to events
  2. Moving functionality to the Document and Element

For example:
Everytime the pointer is locked/unlocked a mozpointerchange event will be dispatched to the document
A mozpointererror event will be dispatched if there are any errors while locking the pointer
Now It’s possible to access the element with the pointer locked via the document

var div = document.createElement("div");

document.addEventListener("mozpointerlockchange", function (e) {
  if (document.mozPointerLockElement === div) {
    // Pointer is locked
  }
}, false);

document.addEventListener("mozpointerlockerror", function (e) {

}, false);

document.addEventListener("mozfullscreenchange", function (e) {
  if (document.mozFullScreen &&
      document.mozFullScreenElement === div) {
      div.mozRequestPointerLock();
  }
}, false);

div.mozRequestFullScreen();

Instead of something like this:

var div = document.createElement("div");

div.addEventListener("mozpointerlocklost", function (e) {
  // Dispatched when pointer is unlocked
}, false);

document.addEventListener("mozfullscreenchange", function (e) {
  if (document.mozFullScreen &&
      document.mozFullScreenElement === div) {
      navigator.mozPointer.lock(
        div, // Element
        function () {
          // Success callback
        },
        function () {
          // Failure callback
        }
      );
  } 
}, false);

div.mozRequestFullScreen();



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.