To use LeapJS, start by creating a Leap.Controller:
var controller = new Leap.Controller();
There are a few options you can pass to this controller:
host - The host used by the WebSocket connection. Default is "127.0.0.1"port - The port used by the WebSocket connection. Default is 6437enableGestures - Enables or disables gesture recognition for this controller. Default is trueframeEventName - Lets you specify which frame detection loop to use for the frame event type. In the browser, it will default to the animation loop. In Node, it will use the device frame loop
var controller = new Leap.Controller({host: "24.6.0.1", port: 1337});
The controller supports a number of event types. When LeapJS connects to the websocket server, it first firest the connect event. After that, once the protocol has been selected, it fires a ready event. Once a frame has been received, a deviceConnected event gets fired once. Then, a deviceFrame gets fired for each frame coming from the websocket server connection. (If you're using the animationFrame though, you won't get this information until the animation loop procs.)
connect - The client is connected to the websocket serverprotocol - The protocal has been selected for the connection. The protocol object is passed as an argument to the event handlerready - The protocol has been selecteddisconnect - The client disconnects from the websocket serverblur - The browser loses focusdeviceConnected - A Leap device has been connected
deviceDisconnected - A Leap device has been disconnected
frame - A frame is finishing being processed by the controller. This event is either driven by the animationFrame or the deviceFrame event, depending which is the controller was created with. The frame is passed as an argument to the event handleranimationFrame - A frame being emitted in time with the animation loop. The frame is passed as an argument to the event handlerdeviceFrame - A frame being emitted by the connection to the Leap device. The frame is passed as an argument to the event handlerExample use of event type:
var controller = new Leap.Controller();
controller.on('connect', function() {
console.log("Successfully connected.");
});
controller.on('deviceConnected', function() {
console.log("A Leap device has been connected.");
};
controller.on('deviceDisconnected', function() {
console.log("A Leap device has been disconnected.");
};
controller.connect();
There are two types of frame detection loops. In general, in the browser, the animation loop will be the most useful. This will tick at 60fps and grab whatever the latest frame is at that point. The other type of loop is a device loop, where the device will ping you with every new frame as soon as it is available. Those working in Node will likely want this type of loop. Whichever frame event name the controller is declared for will be the default given by the event type frame, but you also have the flexibility to write functions for each type of frame specificially. Some apps may even want to use both types of frames; this is allowed.
To illustrate, this will use the animation loop since we're in a browser and didn't specify otherwise in the controller options:
var controller = new Leap.Controller();
controller.on('frame', function() {
// your code here
});
controller.connect();
A more verbose version of above:
var controller = new Leap.Controller();
controller.on('animationFrame', function() {
// your code here
});
controller.connect();
To use the deviceFrame instead you can do:
var controller = new Leap.Controller({frameEventName: "deviceFrame"});
controller.on('frame', function() {
// your code here
});
controller.connect();
Or:
var controller = new Leap.Controller();
controller.on('deviceFrame', function() {
// your code here
});
controller.connect();
Some examples have been included in the examples/ directory. To run them do the following:
npm installmake serveOr in code:
node examples/node.jsYou can build your own leap.js file by using make build. If you're doing any amount of development, you'll find it convenient to run make watch. This takes care of building and testing leapjs for you on every edit.
There are currently rudamentary test. To get them running, do the following:
npm installmake testOr use make watch-test as noted above.