2013-08-02 32 views
9

Tôi đang cố gắng đơn giản nhận hướng của cử chỉ vuốt bằng Chuyển động bước nhảy vọt bằng API javascript. Mã của tôi là:Phát hiện hướng cử chỉ vuốt bằng 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){ 
     // } 
    } 
}); 

tôi có thể nhận được cử chỉ hiện tại từ mảng, nhưng không thể có được các loại hoặc hướng. Ý tưởng nào?

Cảm ơn.

Trả lời

0

Sau khi phân tích nhanh chóng tôi tìm thấy những thông tin bạn đang tìm kiếm:

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

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

Tôi nhận được "Loại lỗi không bắt buộc: Không thể đọc thuộc tính 'loại' không xác định" trên var gestureType = frame.gestures [0] .type; Xin lỗi, nên lưu ý rằng trong câu hỏi của tôi. – mrEmpty

0

Đối tượng khung có một cử chỉ duy nhất khi có một. Khung có thể chứa 0 cử chỉ, 0 ngón tay hoặc 0 cử chỉ. Bạn phải kiểm tra xem có cử chỉ hay không, sau đó lấy thông tin cần thiết.

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') 
     } 

đây là cách tôi đã làm nó bản thân mình, tbh tôi không thể REM nếu nó sang trái hoặc phải nhưng nó không phải là một quá trình lâu dài của elmination

0

đây là một mẫu mã mà hoạt động hoàn hảo cho tôi:

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

Nếu bạn quan tâm về phải, trái, lên hoặc xuống, bạn có thể so sánh giá trị tuyệt đối của tọa độ ngang và dọc của vectơ hướng vuốt để xem Vuốt có dọc hay ngang hơn không so sánh tọa độ có liên quan với số không để xem liệu thao tác vuốt có sang phải hay trái hay lên hoặc xuống):

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

}) 

Với so sánh hơi phức tạp hơn, bạn có thể phân loại các lần vuốt về phía trước hoặc phía sau.

+0

Cho đến nay là ví dụ hoàn chỉnh nhất. Cảm ơn bạn :) Tôi ghét nó khi mọi người không đánh dấu câu trả lời đúng ... – thgie

Các vấn đề liên quan