gpt4 book ai didi

javascript - 检测带有跳跃 Action 的滑动手势

转载 作者:行者123 更新时间:2023-11-28 20:13:41 27 4
gpt4 key购买 nike

我知道如何检测左右手势

this

我想知道如何检测向上、向下和画圈的手势。

我的英语很差。我认为你不能理解,但请帮助我。

最佳答案

对于滑动方向,您可以比较Gesture 对象的direction 属性的x 和y 坐标。在 Leap Motion JavaScript API 中,向量由 3 元素数组表示。所以:

gesture.direction[0] is the x coordinate (left to right)
gesture.direction[1] is the y coordinate ( up, down)
gesture.direction[2] is the z coordinate (front to back)

您引用的示例仅查看 x 坐标的符号 - 因此所有滑动都被分类为向右或向左。要将滑动分类为向上或向下,您必须比较 x 和 y 坐标的大小,以确定滑动是更水平还是更垂直,然后比较坐标的符号以确定是水平滑动还是向左滑动向右或垂直向上或向下滑动。

圆形手势被报告为一种不同类型的手势,因此您可以查看gesture.type属性。

下面是一些 JavaScript 来说明这一点(改编自 Leap Motion SDK 中包含的 Sample.html 文件):

// Store frame for motion functions
var previousFrame = null;

// Setup Leap loop with frame callback function
var controllerOptions = {enableGestures: true};

Leap.loop(controllerOptions, function(frame) {

// Display Gesture object data
var gestureOutput = document.getElementById("gestureData");
var gestureString = "";
if (frame.gestures.length > 0) {
for (var i = 0; i < frame.gestures.length; i++) {
var gesture = frame.gestures[i];

switch (gesture.type) {
case "circle":
gestureString += "<br>ID: " + gesture.id + "<br>type: " + gesture.type + ", "
+ "<br>center: " + vectorToString(gesture.center) + " mm, "
+ "<br>normal: " + vectorToString(gesture.normal, 2) + ", "
+ "<br>radius: " + gesture.radius.toFixed(1) + " mm, "
+ "<br>progress: " + gesture.progress.toFixed(2) + " rotations"
+ "<br>";
break;
case "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";
}
}
gestureString += "<br>ID: " + gesture.id + "<br>type: " + gesture.type + ", "
+ "<br>direction " + swipeDirection
+ "<br>gesture.direction vector: " + vectorToString(gesture.direction, 2) + ", "
+ "<br>";
break;
}
}
}
gestureOutput.innerHTML = gestureString + gestureOutput.innerHTML;

})

function vectorToString(vector, digits) {
if (typeof digits === "undefined") {
digits = 1;
}
return "(" + vector[0].toFixed(digits) + ", "
+ vector[1].toFixed(digits) + ", "
+ vector[2].toFixed(digits) + ")";
}

要使用它,请将其放在将要执行的位置,并在 HTML 文档正文中包含一个具有 idgestureData 的元素:

<div id="gestureData"></div>

关于javascript - 检测带有跳跃 Action 的滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19514282/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com