Connecting to the Controller

The Controller object mediates the connection between your application and the Leap Motion service. You can create a Controller using the Leap.loop() function.

var controller = Leap.loop({enableGestures:true}, function(frame){

You can also create a Controller directly:

var controller = new Leap.Controller();

Use the Controller object to get information about the state of the connection and connected hardware and to set connection options for your application.

Getting Frames

Get Frame objects from one of the two looping mechanisms proveded by the LeapJS library. The animationFrame mechanism calls a function you define once during each browser animation loop, passing in the moce recent Frame object. Typically, the animation loop runs at 60 frames per second. The deviceFrame mechanism calls your function as soon as a new frame is ready. The device frame loop runs at the same rate as the Leap Motion frame rate, which can vary depending on the user’s settings and available computing power.

See Frames for more information.

Foreground and Background Applications

Ordinarily, the Leap Motion service/daemon only sends tracking data to the JavaScript application that has requested focus. This prevents applications idling in the background from receiving input intended for another applications. The LeapJS library manages focus requests automatically.

If your application should receive data only when it is in the foreground and you want to know whether the service/daemon considers your application to be the foreground application, you can listen to focus and blur events dispatched by the controller. See Listening for Controller Events.

Note that your application does not receive frames if another Leap-enabled application is in the foreground, even when the background frames policy is in effect. The policy only matters when a non-Leap-enabled app has the operating system input focus.

You can tell the controller that your application wants data even when it is in the background using the setBackground() function.

var controller = new Leap.Controller();

Access to Images

JavaScript applications cannot currently receive images from the Leap Motion cameras.

Enabling Gestures

To use the built-in gestures, you must enable them using the options passed to Leap.loop() or the Controller constructor:

Connection State

When you start the Leap.loop() (or call Controller.connect() directly), the LeapJS library connects to the LeapMotion service/daemon through a WebSocket. When the connection is established, the Controller dispatches a connect event. If the WebSocket becomes disconnected for any reason, the Controller dispatches a disconnect event.

If the user plugs in or unplugs the Leap Motion peripheral while your application is running, the Controller dispatches deviceConnected and deviceDisconnected events.

See Listening for Controller Events

Listening for Controller Events

The Controller object dispatches a number of events using the Listener mechanism. To handle these events, you can extend the Listener class to implement callback functions. The Controller invokes the relevant callback function when an event occurs.

The events include: