使用Leap Motion检测滑动手势方向

时间:2013-08-02 13:53:37

标签: javascript jquery gesture leap-motion

我正在尝试使用javascript API简单地使用Leap Motion获取滑动手势的方向。我的代码是:

$(document).ready(function() {
    controller = new Leap.Controller("ws://localhost:6437/");
    listener = new Leap.Listener();

    listener.onFrame = function(controller) {
        var frame = controller.frame();
        var hands = frame.hands();
        var pointables = frame.pointables();

        var gestures = frame.gestures();

        $("#rotationAxis").text(pointables.length);
        $("#gestureDetected").text(gestures[0]);
    }

    controller.addListener(listener);
    controller.enableGesture("swipe", true);
    listener.onConnect = function(controller) {
        // calibrate = new Leap.Calibrate(controller);
        // calibrate.onComplete = function(screen){
        // }
    }
});

我可以从数组中获取当前手势,但无法获取类型或方向。有什么想法吗?

感谢。

5 个答案:

答案 0 :(得分:11)

如果您关心向右,向左,向上或向下,您可以比较滑动方向向量的水平和垂直坐标的绝对值,以查看滑动是更垂直还是更水平(然后比较相关坐标为零,以查看滑动是向右还是向左,向上或向下):

// Setup Leap loop with frame callback function
var controllerOptions = {enableGestures: true};

Leap.loop(controllerOptions, function(frame) {

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

})

通过稍微复杂的比较,您可以将滑动分类为向前或向后。

答案 1 :(得分:2)

with enableGestures:true和var gesture = frame.gestures [i];

     // detect swipe   
        if (gesture.direction[0] > gesture.direction[2]) {
         console.log('swipe left')
        }

这就是我自己做的方式,如果它是向左或向右我可以提醒,但这不是一个漫长的过程,

答案 2 :(得分:0)

经过快速分析后,我找到了您要查找的信息:

var gestureType = frame.gestures[0].type;

if (gestureType === "swipe")
{
    if (frame.gestures[0].direction[0] > 0)
        console.log(">>> swipe >>>");
    else
        console.log("<<< swipe <<<");
}

答案 3 :(得分:0)

框架对象仅在有一个手势时才有手势。一帧可以包含0手,0手指或0手势。您必须检查手势是否存在,然后检索所需信息。

var frame = controller.frame();
if (frame.gestures.length > 0)
{
  for (var i = 0; i < frame.gestures.length; i++)
  {
   var gesture = frame.gestures[i];
   var type = gesture.type;
   var direction = gesture.direction;
  }
}

答案 4 :(得分:0)

这是一个适合我的代码示例:

var controller = new Leap.Controller({enableGestures: true});

controller.on('gesture', function (gesture){
    console.log(gesture);
    if(gesture.type === 'swipe'){
        handleSwipe(gesture);
    }
});

function handleSwipe (swipe){
    var startFrameID;
    if(swipe.state === 'stop'){
        if (swipe.direction[0] > 0){
            //this means that the swipe is to the right direction
            moveRight();
        }else{
            //this means that the swipe is to the left direction
            moveLeft();
        }
    }
}

controller.connect();