2013-08-02 57 views
9

我正嘗試使用JavaScript API簡單地獲取使用Leap Motion輕掃手勢的方向。我的代碼是:使用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){ 
     // } 
    } 
}); 

我可以從數組中獲取當前手勢,但無法獲取類型或方向。有任何想法嗎?

謝謝。

回答

0

快速分析後,我發現你正在尋找的信息:

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

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

我得到「Uncaught TypeError:無法讀取未定義的屬性'type'on var gestureType = frame.gestures [0] .type;對不起,應該在我的問題中注意到。 – mrEmpty

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; 
    } 
} 
2

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

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

這是我做到了我自己,TBH如果它向左或向右,我不能REM,但它不是elmination

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

如果您關心的是右,左,上,下,您可以比較滑動方向矢量的水平和垂直座標的絕對值,以查看滑動是更垂直還是更水平(然後比較相關的座標爲零,看看滑動是向右還是向左,或者向上或向下):

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

}) 

有了一個稍微複雜的比較,可以按向前或向後分類刷卡了。

+0

到目前爲止最完整的例子。謝謝:)當人們不把答案標記爲正確時,我討厭它... – thgie