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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18018642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:29:17  来源:igfitidea点击:

Detecting swipe gesture direction with Leap Motion

javascriptjquerygestureleap-motion

提问by mrEmpty

I'm trying to simply get the direction of a swipe gesture with the Leap Motion using the javascript API. My code is:

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

I can get the current gesture from the array, but cannot get the type or direction. Any ideas?

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

Thanks.

谢谢。

回答by Charles Ward

If you care about right, left, up, or down, you can compare the absolute value of the horizontal and vertical coordinates of the Swipe direction vector to see if the Swipe is more vertical or more horizontal (and then compare the relevant coordinate to zero to see if the swipe is going right or left, or up or down):

如果关心右、左、上、下,可以比较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";
              }                  
          }
       }
     }
  }

})

With a slightly more complex comparison, you could classify swipes as forward or backward, too.

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

回答by devolved

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

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

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

this is how I did it myself,tbh I can't rem if it's left or right but it's not a long process of elmination

这就是我自己做的,tbh 我不能确定是左还是右,但这不是一个漫长的消除过程

回答by abisib

here is a code sample that works perfectly for me:

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

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

回答by Sga

After a quick analysis I found the information you are looking for:

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

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

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

回答by Marcassin

The frame object has a gesture only when there is one. A frame can contains 0 hand, 0 finger or 0 gesture. You have to check if a gesture is present, then retrieve needed information.

框架对象只有在有手势时才有手势。一个框架可以包含 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;
  }
}