我正在尝试使用 javascript API 通过 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){
// }
}
});
我可以从数组中获取当前手势,但无法获取类型或方向。有任何想法吗?
Thanks.
如果你关心右、左、上、下,可以比较Swipe方向向量的水平和垂直坐标的绝对值,看看Swipe是更垂直还是更水平(然后将相关坐标与零进行比较)查看滑动是向右还是向左、向上还是向下):
// 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";
}
}
}
}
}
})
通过稍微复杂一点的比较,您也可以将滑动分类为向前或向后。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)