Gesture

Notice Gestures are deprecated in version 3.0 and may not behave the same as they did in earlier versions.

Attributes:

class Gesture()

The Gesture class represents a recognized movement by the user.

The Leap watches the activity within its field of view for certain movement patterns typical of a user gesture or command. For example, a movement from side to side with the hand can indicate a swipe gesture, while a finger poking forward can indicate a screen tap gesture.

When the Leap recognizes a gesture, it assigns an ID and adds a Gesture object to the frame gesture list. For continuous gestures, which occur over many frames, the Leap updates the gesture by adding a Gesture object having the same ID and updated properties in each subsequent frame.

Important: Recognition for gestures must be enabled; otherwise no gestures are recognized or reported.

Subclasses of Gesture define the properties for the specific movement patterns recognized by the Leap Motion controller.

The Gesture subclasses for include:

Circle and swipe gestures are continuous and these objects can have a state of start, update, and stop.

The screen tap gesture is a discrete gesture. The Leap Motion software only creates a single ScreenTapGesture object appears for each tap and it always has a stop state.

Get valid Gesture instances from a Frame object. You can get a list of gestures from the Frame gestures array. You can also use the Frame gesture() method to find a gesture in the current frame using an ID value obtained in a previous frame.

Gesture objects can be invalid. For example, when you get a gesture by ID using Frame.gesture(), and there is no gesture with that ID in the current frame, then gesture() returns an Invalid Gesture object (rather than a null value). Always check object validity in situations where a gesture might be invalid.

An uninitialized Gesture object is considered invalid. Get valid instances of the Gesture class, which will be one of the Gesture subclasses, from a Frame object or a gesture event listener.

The following example illustrates how to get gesture objects from a frame of tracking data:

var controller = Leap.loop({enableGestures: true}, function(frame){
  if(frame.valid && frame.gestures.length > 0){
    frame.gestures.forEach(function(gesture){
        switch (gesture.type){
          case "circle":
              console.log("Circle Gesture");
              break;
          case "keyTap":
              console.log("Key Tap Gesture");
              break;
          case "screenTap":
              console.log("Screen Tap Gesture");
              break;
          case "swipe":
              console.log("Swipe Gesture");
              break;
        }
    });
  }
});

You can also add a listener callback to a Controller object. Your callback is called for each gesture object in each device frame:

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

controller.on("gesture", function(gesture){
  //... handle gesture object
});
Gesture.duration
Type:number

The elapsed duration of the recognized movement up to the frame containing this Gesture object, in microseconds.

var duration = gesture.duration;

The duration reported for the first Gesture in the sequence (with the start state) will typically be a small positive number since the movement must progress far enough for the Leap to recognize it as an intentional gesture.

Gesture.handIds
Type:number[]

The list of hand ids associated with this Gesture, if any.

If no hands are related to this gesture, the list is empty.

var controller = Leap.loop({enableGestures: true}, function(frame){
  if(frame.valid && frame.gestures.length > 0){
    frame.gestures.forEach(function(gesture){
        var handIds = gesture.handIds;
        handIds.forEach(function(handId){
          var hand = frame.hand(handId);
        });
    });
  }
});

If the Controller object dispatching this event is not set to use the deviceFrame loop, then the Frame object associated with this device may not be the current frame and may not be stored in the history buffer. The position and other physical attributes of the hand may have changed slightly between frames. In some cases, a Hand object with the same ID may not be present at all in the current frame.

Gesture.id
Type:number

The gesture ID.

All Gesture objects belonging to the same recognized movement share the same ID value. Use the ID value with the Frame.gesture() method to find updates related to this Gesture object in subsequent frames.

var gestureIdOfInterest = gesture.id;
Gesture.pointableIds

The list of fingers associated with this Gesture, if any.

If no Pointable objects are related to this gesture, the list is empty.

var controller = Leap.loop({enableGestures: true}, function(frame){
  if(frame.valid && frame.pointables.length > 0){
    frame.gestures.forEach(function(gesture){
        var pointableIds = gesture.pointableIds;
        pointableIds.forEach(function(pointableId){
          var pointable = frame.pointable(pointableId);
        });
    });
  }
});

If the Controller object dispatching this event is not set to use the deviceFrame loop, then the Frame object associated with this device may not be the current frame and may not be stored in the history buffer. The position and other physical attributes of the finger may have changed slightly between frames. In some cases, the Pointable object may not be present at all in the current frame.

Type: Array

Gesture.state
Type:string

The gesture state.

Recognized movements occur over time and have a beginning, a middle, and an end. The state attribute reports where in that sequence this Gesture object falls.

Possible values for the state field are:

  • start
  • update
  • stop
var state = gesture.state;
Gesture.type
Type:string

The gesture type.

Possible values for the type field are:

  • circle
  • swipe
  • screenTap
  • keyTap
var controller = Leap.loop({enableGestures: true}, function(frame){
  if(frame.valid && frame.gestures.length > 0){
    frame.gestures.forEach(function(gesture){
        switch (gesture.type){
          case "circle":
              console.log("Circle Gesture");
              break;
          case "keyTap":
              console.log("Key Tap Gesture");
              break;
          case "screenTap":
              console.log("Screen Tap Gesture");
              break;
          case "swipe":
              console.log("Swipe Gesture");
              break;
        }
    });
  }
});