使用Leap Motion检测滑动手势方向 [英] Detecting swipe gesture direction with Leap Motion

查看:492
本文介绍了使用Leap Motion检测滑动手势方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

$(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?

谢谢.

推荐答案

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

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):

// 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.

这篇关于使用Leap Motion检测滑动手势方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆