-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.html
144 lines (129 loc) · 4.82 KB
/
connection.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!--
Author: Austin Bearden
Date Created: 04/04/2019
Purpose: Get connect/info live from LeapMotion
Using code from: LeapMotion SDK: JSONViewer.html
-->
<!-- Get LeapMotin Data -->
<!DOCTYPE html>
<!------------------------------------------------------------------------------
| Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved. |
| Leap Motion proprietary and confidential. Not for distribution. |
| Use subject to the terms of the Leap Motion SDK Agreement available at |
| https://developer.leapmotion.com/sdk_agreement, or another agreement |
| between Leap Motion and you, your company or other organization. |
------------------------------------------------------------------------------->
<html>
<head>
<title>Leap WebSocket JSON Viewer</title>
<script>
var ws;
var paused = false;
var pauseOnGesture = false;
var focusListener;
var blurListener;
var handLeft = false;
var handFlat = false;
var handRight = false;
var rotation;
// Support both the WebSocket and MozWebSocket objects
if ((typeof(WebSocket) == 'undefined') &&
(typeof(MozWebSocket) != 'undefined')) {
WebSocket = MozWebSocket;
}
// Create the socket with event handlers
function init() {
// Create and open the socket
ws = new WebSocket("ws://localhost:6437/v6.json");
// On successful connection
ws.onopen = function(event) {
var enableMessage = JSON.stringify({enableGestures: true});
ws.send(enableMessage); // Enable gestures
ws.send(JSON.stringify({focused: true})); // claim focus
focusListener = window.addEventListener('focus', function(e) {
ws.send(JSON.stringify({focused: true})); // claim focus
});
blurListener = window.addEventListener('blur', function(e) {
ws.send(JSON.stringify({focused: false})); // relinquish focus
});
document.getElementById("main").style.visibility = "visible";
document.getElementById("connection").innerHTML = "WebSocket connection open!";
};
// On message received
ws.onmessage = function(event) {
if (!paused) {
var obj = JSON.parse(event.data);
var str = JSON.stringify(obj, undefined, 2);
var objRotation = obj.hands[0].armBasis[1];
var rotationValue = objRotation[2];
if(rotationValue >= 0.2 && rotationValue <= 1.0) {
handLeft = true;
handRight = false;
handFlat = false;
// this variable represents the angle of the plane
rotation = -30; //degrees
} else if(rotationValue < -0.5 && rotationValue > -1.0) {
handRight = true;
handLeft = false;
handFlat = false;
// this variable represents the angle of the plane
rotation = 30; //degress
} else {
// set hand to flat to true
handFlat = true;
// set hand left and hand right to false
handLeft = false;
handRight = false;
// this variable represents the angle of the plane
rotation = 0; //degrees
}
console.log(rotationValue, " HandFlat: ", handFlat);
console.log(rotationValue, " HandLeft: ", handLeft);
console.log(rotationValue, " HandRight: ", handRight);
document.getElementById("output").innerHTML = '<pre>' + str + '</pre>';
if (pauseOnGesture && obj.gestures.length > 0) {
togglePause();
}
}
};
// On socket close
ws.onclose = function(event) {
ws = null;
window.removeListener("focus", focusListener);
window.removeListener("blur", blurListener);
document.getElementById("main").style.visibility = "hidden";
document.getElementById("connection").innerHTML = "WebSocket connection closed";
}
// On socket error
ws.onerror = function(event) {
alert("Received error");
};
}
function togglePause() {
paused = !paused;
if (paused) {
document.getElementById("pause").innerText = "Resume";
} else {
document.getElementById("pause").innerText = "Pause";
}
}
function pauseForGestures() {
if (document.getElementById("pauseOnGesture").checked) {
pauseOnGesture = true;
} else {
pauseOnGesture = false;
}
}
</script>
</head>
<body onload="init();">
<h1>Leap WebSocket JSON Viewer</h1>
<button id="pause" onclick="togglePause()">Pause</button>
<input type="checkbox" id="pauseOnGesture" onclick="pauseForGestures()">Pause on gesture</input>
<div id="connection">WebSocket not connected</div>
<div id="main" style="visibility:hidden">
<h3>JSON Frame data:</h3>
<div id="output"></div>
</div>
</body>
</html>