SwipeGesture

Attributes:

class SwipeGesture()

Extends Gesture()

The SwipeGesture class represents a swiping motion of a hand and a finger or tool.

SwipeGestureImage

SwipeGesture objects are generated for each swiping finger or tool. Swipe gestures are continuous; a gesture object with the same ID value will appear in each frame while the gesture continues.

An uninitialized SwipeGesture object is considered invalid. Get valid instances of the SwipeGesture class 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
});
SwipeGesture()

Constructs a new SwipeGesture object.

An uninitialized SwipeGesture object is considered invalid. Get valid instances of the SwipeGesture class from a Frame object.

SwipeGesture.direction
Type:number[] – a 3-element array representing a unit direction vector.

The unit direction vector parallel to the swipe motion.

You can compare the components of the vector to classify the swipe as appropriate for your application. For example, if you are using swipes for two dimensional scrolling, you can compare the x and y values to determine if the swipe is primarily horizontal or vertical.

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

  // Display Gesture object data
  if (frame.gestures.length > 0) {
    for (var i = 0; i < frame.gestures.length; i++) {
      var gesture = frame.gestures[i];
      if(gesture.type == "swipe") {
          //Classify swipe as either horizontal or vertical
          var isHorizontal = Math.abs(gesture.direction[0]) > Math.abs(gesture.direction[1]);
          //Classify as right-left or up-down
          if(isHorizontal){
              if(gesture.direction[0] > 0){
                  swipeDirection = "right";
              } else {
                  swipeDirection = "left";
              }
          } else { //vertical
              if(gesture.direction[1] > 0){
                  swipeDirection = "up";
              } else {
                  swipeDirection = "down";
              }                  
          }
          console.log(swipeDirection)
       }
     }
  }

});
SwipeGesture.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.

SwipeGesture.handIds
Type:number[]

The list of the ids for the hands associated with this Gesture, if any.

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 no hands are related to this gesture, the list is empty.

SwipeGesture.id
Type:number

The gesture ID.

var gestureIdOfInterest = gesture.id;

All Gesture objects belonging to the same recognized movement share the same ID value.

SwipeGesture.pointableIds
Type:number[]

The list of ids for the fingers and tools associated with this Gesture, if any.

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 no Pointable objects are related to this gesture, the list is empty.

SwipeGesture.position
Type:number[] – a three element array representing a position vector.

The current swipe position within the Leap frame of reference, in mm.

if(gesture.type == "swipe"){
  var currentPosition = gesture.position;
}
SwipeGesture.speed
Type:number

The speed of the finger performing the swipe gesture in millimeters per second.

if(gesture.type == "swipe"){
  var velocity = gesture.speed;
}
SwipeGesture.startPosition
Type:number[] – a 3-element array representing a position vector.

The starting position within the Leap frame of reference, in mm.

if(gesture.type == "swipe"){
  var originPosition = gesture.startPosition;
}
SwipeGesture.state
Type:String

The gesture ID.

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;
SwipeGesture.type
Type:String

The gesture type. In this case, always: “swipe”.

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;
        }
    });
  }
});