Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于跳跃运动的刷卡手势方向检测_Javascript_Jquery_Gesture_Leap Motion - Fatal编程技术网

Javascript 基于跳跃运动的刷卡手势方向检测

Javascript 基于跳跃运动的刷卡手势方向检测,javascript,jquery,gesture,leap-motion,Javascript,Jquery,Gesture,Leap Motion,我试图使用javascript API简单地通过跳跃动作获得滑动手势的方向。我的代码是: $(document).ready(function() { controller = new Leap.Controller("ws://localhost:6437/"); listener = new Leap.Listener(); listener.onFrame = function(controller) { var frame = controlle

我试图使用javascript API简单地通过跳跃动作获得滑动手势的方向。我的代码是:

$(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){
        // }
    }
});
我可以从数组中获取当前手势,但无法获取类型或方向。有什么想法吗


谢谢。

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

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

if (gestureType === "swipe")
{
    if (frame.gestures[0].direction[0] > 0)
        console.log(">>> swipe >>>");
    else
        console.log("<<< swipe <<<");
}
var gestureType=frame.signatures[0]。类型;
如果(手势类型==“滑动”)
{
if(帧.手势[0].方向[0]>0)
console.log(“>>>滑动>>>”;
其他的

console.log(“帧对象只有在有手势时才有手势。帧可以包含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;
  }
}
var frame=controller.frame();
如果(frame.signatures.length>0)
{
对于(变量i=0;i
带有启用手势:true和var手势=frame.signities[i]

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

这是我自己做的,tbh我不能rem它是左还是右,但这不是一个很长的定义过程

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

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

//使用帧回调函数设置跳跃循环
var controllerOptions={enablespirates:true};
跳跃循环(控制器选项、功能(帧){
如果(frame.signatures.length>0){
对于(变量i=0;iMath.abs(手势方向[1]);
//分类为左右或上下
if(水平){
if(手势方向[0]>0){
swipeDirection=“右”;
}否则{
swipeDirection=“左”;
}
}else{//垂直
如果(手势方向[1]>0){
swipeDirection=“向上”;
}否则{
swipeDirection=“向下”;
}                  
}
}
}
}
})

通过稍微复杂一点的比较,您也可以将刷卡分为向前或向后。

我在var gestureType=frame.signatures[0]上得到了“Uncaught TypeError:无法读取未定义的属性'type'。键入;抱歉,我的问题中应该注意到这一点。这是迄今为止最完整的示例。谢谢:)我讨厌人们不认为答案正确…
// 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";
              }                  
          }
       }
     }
  }

})