这是我的代码要点。
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天,真的需要你的帮助。
答案 0 :(得分:4)
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
}
}
}
}
}
});
答案 1 :(得分:2)
查看在飞跃网站上给出的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);
}
从GitHub和Leap Motion Developers site
获取Leap JS API的所有信息 - &GT;小心frame.pointables
以任意顺序返回指针对象。(Cf JS API doc)。这段代码仅用于解释算法
答案 2 :(得分:1)
这是查找
的最简单方法var isClockwise = (circleGesture.normal[2] <= 0);
它将返回true或false
答案 3 :(得分:0)
在此页面上尝试了其他答案并且无法使其工作,简化了一些代码并最终使其正常工作。 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();
}
});
答案 4 :(得分:-1)
我一直在为我的项目使用“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"
});