Skip to content

Instantly share code, notes, and snippets.

@gorhgorh
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorhgorh/8916319 to your computer and use it in GitHub Desktop.
Save gorhgorh/8916319 to your computer and use it in GitHub Desktop.
Modernizr test for leap motion
function checkLeap () {
// Hard dependency on websockets; quick exit if not supported
if (!Modernizr.websockets) {
Modernizr.addTest('leap', false);
}
// Try and connect to the leap daemon
var ws = new WebSocket("ws://localhost:6437/");
ws.onopen = function(event) {
Modernizr.addTest('leap', true);
};
ws.onerror = function(event) {
Modernizr.addTest('leap', false);
};
}
checkLeap();
@gorhgorh
Copy link
Author

here is a simple test for leapMotion support on the system that runs it. it works but rely on a websocket, so it will probably affect the page load somehow. so i see this gist as a starting point if someone else is interested in finding the most efficient way to do it.
On top of that i was wondering if these kind of tests (async) were to avoid at all cost, since i'm not sure it blocks the page load or not ?

@stucox
Copy link

stucox commented Feb 11, 2014

Nice. I don’t think there’s a problem with using a WebSocket, if that’s the only way to test it – but you might want to check that WebSockets are in fact supported first.

Should the socket be closed after the test has run, or will garbage collection do that automatically?

Modernizr has a few async detects – webp, datauri, etc. They don’t block page loading so aren’t a performance concern, but do need to be handled specially by the developer using them, because the result might not be ready immediately, which is why we try to avoid them if we can.

I don’t think what you’ve written here would work though because the value returned by the detect function is ws.onopen – i.e. the function object, not the value returned by ws.onopen. I’d structure it something like this:

function checkLeap () {
  // Hard dependency on websockets; quick exit if not supported
  if (!Modernizr.websockets) {
    Modernizr.addTest('leap', false);
  }

  // Try and connect to the leap daemon
  var ws = new WebSocket("ws://localhost:6437/");
  ws.onopen = function(event) {
    Modernizr.addTest('leap', true);
  };
  ws.onerror = function(event) {
    Modernizr.addTest('leap', false);
  };
}
checkLeap();

A developer could then use this with Modernizr.on() (added in v3.0.0, or there’s a polyfill for v2.x):

Modernizr.on('leap', function (result) {
  if (result) {
    // Leap motion present: do stuff with it!
  }
  else {
    // No leap motion :(
  }
});

@gorhgorh
Copy link
Author

thanks for the response !
i really love the Modernizr.addTest('leap', true); really handy, and clever work on the quick exit, i'll make a real github repo out if and post it to the twitter modernizr, so if someone else need it they could use it.

you confirmed my guesses on the async test, but in this case i could not boil it down to a "simpler" test.

on an other topic, is Modernizr.load deprecated ? i had to use yepnope directly to dynamically load my leap script

@stucox
Copy link

stucox commented Feb 11, 2014

Modernizr.load isn’t deprecated yet – but will be from v3.0.0. In v2.x you need to include it explicitly via the builder on http://modernizr.com/download though (in the “Extras” section on the right hand side).

@gorhgorh
Copy link
Author

ahh ok, i'm using the plain development version right now, planned on making a modular build later, i guess it is not included in it.

thanks for this wondefull tool by the way :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment