Javascript 如何检测圆手势方向?
这是我的代码要点Javascript 如何检测圆手势方向?,javascript,leap-motion,Javascript,Leap Motion,这是我的代码要点 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" && g
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 ?
}
}
} );
Leap.loop({enablespirates:true},函数(帧){
var手势=帧手势;
对于(var i=0;i
如何知道圆圈是顺时针方向还是逆时针方向
我只用了2天的跳动,确实需要你的帮助。
< P>查看LASH网站上给出的C++样本,给出一个代码来检测圆圈是顺时针的。 C++代码:if (circle.pointable().direction().angleTo(circle.normal()) <= PI/4)
{
clockwiseness = "clockwise";
}
else
{
clockwiseness = "counterclockwise";
}
if(circle.pointable().direction().angleTo(circle.normal())=1)
{
var dir=frame.pointables[手势.pointableIds[0]].direction;//获取用于圆手势的指针的方向
变量角度=方向角度(圆形法向);
var isClockWise=angle小心frame.pointables
返回按任意顺序给定的pointables对象。(Cf JS API doc)。这段代码只是为了解释算法
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
}
}
}
}
}
})) 我一直在为我的项目使用“Leap Cursor library”,它真的很酷。使用此库识别元素上的圆手势非常简单
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"
});
var circletracketelements=document.queryselectoral(“.mydoelements”);
对于(var i=0;i
这是最容易找到答案的方法
var isClockwise = (circleGesture.normal[2] <= 0);
var isClockwise=(circlegestrue.normal[2]尝试了此页面上的其他答案,但无法使其正常工作,对代码进行了一些简化,最终使其正常工作。pointableID根据圆圈手势的方向将normal记录为负/正
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();
}
});
函数页面向下滚动(){
滚动窗口(0,10);
};
函数pageScrollUp(){
滚动窗口(0,-15);
};
$(窗口).bind('circle',函数(e,手势){
var循环=手势;
circle.pointable=circle.pointableId[0];
方向=手势。正常[1];
如果(方向<0){
页面向下滚动();
}否则{
pageScrollUp();
}
});
事实上,新版本的leapjs库中不推荐使用angleTo()函数。改为使用equal函数。好的,感谢您的提示,另一个angleTo方法位于github()上leapjs中的Vector
类的文档中。这比公认的答案更有效(angleTo通常需要计算沿途的点积)。-1因为它没有解决实际问题,即绘制圆的方向。我得到了意外的标记。当我使用Leap.Loop()时,为什么?
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();
}
});