10

我正在尝试使用 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){
        // }
    }
});

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

谢谢。

4

5 回答 5

11

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

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

})

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

于 2013-10-25T20:26:59.923 回答
2

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

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

我自己就是这样做的

于 2013-08-11T12:23:24.743 回答
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();
于 2013-08-17T16:18:02.063 回答
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;
  }
}
于 2013-08-02T15:40:03.700 回答
0

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

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

if (gestureType === "swipe")
{
    if (frame.gestures[0].direction[0] > 0)
        console.log(">>> swipe >>>");
    else
        console.log("<<< swipe <<<");
}
于 2013-08-02T14:32:53.113 回答