【问题标题】:How to detect circle gesture direction?如何检测圆圈手势方向?
【发布时间】:2013-08-03 10:02:09
【问题描述】:

这是我的代码要点。

Leap.loop({enableGestures: true}, function(frame) {
var gestures = frame.gestures;

for (var i = 0; i < gestures.length; i++) { 
  // I want to do something when draw circle with one pointable 
   if (gesture.type == "circle" && gesture.state == "stop" && gesture.pointableIds.length == 1) {
    var isClockWise = ? ;//  how to know the direction of circle ?
  }
}
} );

如何通过手势对象知道圆是顺时针还是逆时针?

我只使用了 2 天的跳跃动作,真的需要你的帮助。

【问题讨论】:

  • 我收到了意外的令牌。当我使用 Leap.Loop() 时?为什么?

标签: javascript leap-motion


【解决方案1】:
Leap.loop({enableGestures: true},function(frame) {
var gestures = frame.gestures,
    circle,
    pointable,
    direction,
    normal;
// Check if is there any gesture going on
if(gestures.length > 0) {
    // In this example we will focus only on the first gesture, for the sake of simplicity
    if(gestures[0].type == 'circle') {
        circle = gestures[0];
        // Get Pointable object
        circle.pointable = frame.pointable(circle.pointableIds[0]);
        // Reset circle gesture variables as nedded, not really necessary in this case
        if(circle.state == 'start') {
            clockwise = true;
        } else if (circle.state == 'update') {
            direction = circle.pointable.direction;
            // Check if pointable exists
            if(direction) {
                normal = circle.normal;
                // Check if product of vectors is going forwards or backwards
                // Since Leap uses a right hand rule system
                // forward is into the screen, while backwards is out of it
                clockwise = Leap.vec3.dot(direction, normal) > 0;
                if(clockwise) {
                    //Do clockwose stuff
                } else {
                    //Do counterclockwise stuff
                }
            }
        }
    }
}

});

【讨论】:

  • 这比公认的答案更有效(angleTo 通常需要一路计算点积)。
【解决方案2】:

查看leap网站上给出的C++示例,给出一段代码检测圆是顺时针方向。

C++ 代码:

if (circle.pointable().direction().angleTo(circle.normal()) <= PI/4)
   {
      clockwiseness = "clockwise";
   }
   else
   {
      clockwiseness = "counterclockwise";
   }

我没有使用过 Javascript API,但我认为这可以是等效的 此代码尚未经过测试,但在 Javascript 中可能类似于:

// considere your gesture is a circle, and there is at least one pointable object.
if (gesture.type == "circle" && gesture.state == "stop" && gesture.pointableIds.length >= 1)
{
  var dir = frame.pointables[gesture.pointableIds[0] ].direction; // get direction of the Pointable used for the circle gesture
  var angle = dir.AngleTo (circle.normal);
  var isClockWise =  angle <= (3.14 / 4);
}

GitHubLeap Motion Developers site 获取 Leap JS API 的所有信息

-> 小心frame.pointables 以任意顺序返回指向对象。(参见 JS API 文档)。这段代码只是为了解释算法

【讨论】:

【解决方案3】:

这是最简单的方法

var isClockwise = (circleGesture.normal[2] <= 0);

它会返回真或假

【讨论】:

    【解决方案4】:

    在此页面上尝试了其他答案,但无法使其正常工作,稍微简化了代码并最终使其正常工作。 pointableID 根据圆圈手势的方向将正常记录为负/正。

    function pageScrollDown() {
       window.scrollBy(0,10);
    };
    function pageScrollUp(){
       window.scrollBy(0,-15);
    };
    
    $(window).bind('circle', function(e, gesture){
    
    var circle = gesture;
    
    circle.pointable = circle.pointableIds[0];
    direction = gesture.normal[1];
    
        if(direction < 0 ) {
          pageScrollDown();
          } else {
            pageScrollUp();
    }
    });
    

    【讨论】:

      【解决方案5】:

      我一直在为我的项目使用“Leap Cursor 库”,它真的很酷。使用这个库识别元素上的圆圈手势非常简单。

      var circleTrackElements = document.querySelectorAll(".myDOMElements");
      for (var i = 0; i < allTiles.length; i++) {
          circleTrackElements[i].addEventListener("leap-circle-stop", function(e) {
              console.log("CIRCLE DETECTED");
          });
      };
      LeapManager.init({
          maxCursors : 1,
          interactiveSelector : ".myDOMElements"
      }); 
      

      GITHUB link for the library

      【讨论】:

      • -1 因为它没有解决实际问题,即关于绘制圆圈的方向。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      • 2011-01-24
      • 2021-03-08
      • 1970-01-01
      相关资源
      最近更新 更多