我们正在使用Leap Motion控制器开发JavaScript项目。代码在今天早上工作正常,但是,六小时后,它现在功能失调,不稳定,反应迟钝。我们发出警报,直到它停止运行为止,当它到达Leap.loop时停止运行;我们认为问题是某个地方正在变得过载,但是我们不知道编程是否足以实际掌握什么是错误的。我们在不同的硬件(不同的服务器和不同的本地驱动器)上尝试过它,我们得到的唯一响应是“有些网页没有响应。杀死或忽略?”弹出窗口。
<title>THIS SUCKS</title>
<style>
html, body {
width: 100%;
height:100%;
margin: 0;
padding: 0;
background: #024;
color: #fff;
}
#wrap {
width: 600px;
margin: 0 auto;
}
.half {
width: 50%;
margin-top: 100px;
float: left;
}
div {
text-align: center;
font-size: 60px;
font-family: Helvetica, Arial, sans-serif;
}
#hands,
#fingers {
font-weight: bold;
font-size: 200px;
}
</style>
<script src="//js.leapmotion.com/0.2.0-beta1/leap.min.js"></script>
<div id="gestureData"></div>
<script>
// Store frame for motion functions
var previousFrame = null;
// Setup Leap loop with frame callback function
var controllerOptions = {enableGestures: true};
var url = "";
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}
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>";
url = "http://192.168.130.234:5050/layerApi.aspx?cmd=move&move=Play";
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 (Math.abs(gesture.direction[0]) > Math.abs(gesture.direction[2])){
if(gesture.direction[0] > 0){
swipeDirection = "Right";
} else {
swipeDirection = "Left";
}
} else {
if(Math.abs(gesture.direction[2].toFixed(2)) > 0.5){
swipeDirection = "Play";
}
}
} else { //vertical
if (Math.abs(gesture.direction[1]) > Math.abs(gesture.direction[2])){
if(gesture.direction[1] > 0){
swipeDirection = "Up";
} else {
swipeDirection = "Down";
}
} else {
if(Math.abs(gesture.direction[2].toFixed(2)) > 0.5){
swipeDirection = "Play";
}
}
}
url = "http://192.168.130.234:5050/layerApi.aspx?cmd=move&move=" + swipeDirection;
gestureString += "<br>ID: " + gesture.id + "<br>type: " + gesture.type + ", "
+ "<br>direction " + swipeDirection
+ "<br>url " + url
+ "<br>gesture.direction vector: " + vectorToString(gesture.direction, 2) + ", "
+ "<br>";
break;
}
}
}
gestureOutput.innerHTML = gestureString + gestureOutput.innerHTML;
PlayShow();
})
var notStarted = true;
function vectorToString(vector, digits) {
if (typeof digits === "undefined") {
digits = 1;
}
return "(" + vector[0].toFixed(digits) + ", "
+ vector[1].toFixed(digits) + ", "
+ vector[2].toFixed(digits) + ")";
}
function PlayShow () {
var xhReq = new XMLHttpRequest();
//The "false" puts XMLHttpRequest in synchronous mode, and the code following will wait for a response before executing.
xhReq.open("GET", url, false); //(If these are really on the same domain, you might need to make this a relative call rather than an absolute one for it to work, not sure)
xhReq.send(null);
//put the response into reqResult
var reqResult = xhReq.responseText;
//check that the call was successful
if (reqResult=="<Status>Success</Status>") { //may want to change to a "contains" compare rather than an "equals"
showRunning = true;
}
else {
showRunning = false;
}
// result is now in reqResult
if (showRunning){
showText.innerHTML="Running Show";
} else
showText.innerHTML="Error! Try Again?";
notStarted=true;
}
</script>