-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample.html
197 lines (172 loc) · 7.19 KB
/
Sample.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<!--
/------------------------------------------------------------------------------\
| Copyright (C) 2012-2016 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. |
\------------------------------------------------------------------------------/
-->
<!--
See additional libraries, guides, and examples at:
- https://developer.leapmotion.com/downloads/javascript
- https://developer.leapmotion.com/getting-started/javascript
- https://developer.leapmotion.com/gallery/tags/javascript
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Leap Motion JavaScript Sample</title>
<script src="http://js.leapmotion.com/leap-0.6.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
// Store frame for motion functions
var previousFrame = null;
var paused = false;
// Setup Leap loop with frame callback function
var controllerOptions = {};
// to use HMD mode:
// controllerOptions.optimizeHMD = true;
Leap.loop(controllerOptions, function(frame) {
if (paused) {
return; // Skip this update
}
// Display Frame object data
var frameOutput = document.getElementById("frameData");
var frameString = "Frame ID: " + frame.id + "<br />"
+ "Timestamp: " + frame.timestamp + " µs<br />"
+ "Hands: " + frame.hands.length + "<br />"
+ "Fingers: " + frame.fingers.length + "<br />";
frameOutput.innerHTML = "<div style='width:300px; float:left; padding:5px'>" + frameString + "</div>";
// Display Hand object data
var handOutput = document.getElementById("handData");
var handString = "";
if (frame.hands.length > 0) {
for (var i = 0; i < frame.hands.length; i++) {
var hand = frame.hands[i];
$.ajax({
url: "http://api-m2x.att.com/v2/devices/a24ae3830e6418252dba44f657265be8/streams/outfingers/value",
type: "PUT",
data: {"value": hand.palmPosition[1]},
headers: {"X-M2X-KEY": 'b54f444b4974fb7c5a58dc9031010954'}
});
console.log(hand.palmPosition[1]);
handString += "<div style='width:300px; float:left; padding:5px'>";
handString += "Hand ID: " + hand.id + "<br />";
handString += "Type: " + hand.type + " hand" + "<br />";
handString += "Direction: " + vectorToString(hand.direction, 2) + "<br />";
handString += "Palm position: " + vectorToString(hand.palmPosition) + " mm<br />";
handString += "Grab strength: " + hand.grabStrength + "<br />";
handString += "Pinch strength: " + hand.pinchStrength + "<br />";
handString += "Confidence: " + hand.confidence + "<br />";
handString += "Arm direction: " + vectorToString(hand.arm.direction()) + "<br />";
handString += "Arm center: " + vectorToString(hand.arm.center()) + "<br />";
handString += "Arm up vector: " + vectorToString(hand.arm.basis[1]) + "<br />";
// IDs of pointables associated with this hand
if (hand.pointables.length > 0) {
var fingerIds = [];
for (var j = 0; j < hand.pointables.length; j++) {
var pointable = hand.pointables[j];
fingerIds.push(pointable.id);
}
if (fingerIds.length > 0) {
handString += "Fingers IDs: " + fingerIds.join(", ") + "<br />";
}
}
handString += "</div>";
}
}
else {
handString += "No hands";
}
handOutput.innerHTML = handString;
// Display Pointable (finger) object data
var pointableOutput = document.getElementById("pointableData");
var pointableString = "";
if (frame.pointables.length > 0) {
var fingerTypeMap = ["Thumb", "Index finger", "Middle finger", "Ring finger", "Pinky finger"];
var boneTypeMap = ["Metacarpal", "Proximal phalanx", "Intermediate phalanx", "Distal phalanx"];
for (var i = 0; i < frame.pointables.length; i++) {
var pointable = frame.pointables[i];
pointableString += "<div style='width:250px; float:left; padding:5px'>";
{
pointableString += "Pointable ID: " + pointable.id + "<br />";
pointableString += "Type: " + fingerTypeMap[pointable.type] + "<br />";
pointableString += "Belongs to hand with ID: " + pointable.handId + "<br />";
pointableString += "Classified as a finger<br />";
pointableString += "Length: " + pointable.length.toFixed(1) + " mm<br />";
pointableString += "Width: " + pointable.width.toFixed(1) + " mm<br />";
pointableString += "Direction: " + vectorToString(pointable.direction, 2) + "<br />";
pointableString += "Extended?: " + pointable.extended + "<br />";
pointable.bones.forEach( function(bone){
pointableString += boneTypeMap[bone.type] + " bone <br />";
pointableString += "Center: " + vectorToString(bone.center()) + "<br />";
pointableString += "Direction: " + vectorToString(bone.direction()) + "<br />";
pointableString += "Up vector: " + vectorToString(bone.basis[1]) + "<br />";
});
pointableString += "Tip position: " + vectorToString(pointable.tipPosition) + " mm<br />";
pointableString += "</div>";
}
}
}
else {
pointableString += "<div>No pointables</div>";
}
pointableOutput.innerHTML = pointableString;
// Store frame for motion functions
previousFrame = frame;
})
function post(path, params, method) {
method = method || "post"; // Set method to post by default if not specified.
// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
for(var key in params) {
if(params.hasOwnProperty(key)) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
function vectorToString(vector, digits) {
if (typeof digits === "undefined") {
digits = 1;
}
return "(" + vector[0].toFixed(digits) + ", "
+ vector[1].toFixed(digits) + ", "
+ vector[2].toFixed(digits) + ")";
}
function togglePause() {
paused = !paused;
if (paused) {
document.getElementById("pause").innerText = "Resume";
} else {
document.getElementById("pause").innerText = "Pause";
}
}
</script>
</head>
<body>
<h1>Leap Motion JavaScript Sample</h1>
<div id="main">
<button id="pause" onclick="togglePause()">Pause</button>
<h3>Frame data:</h3>
<div id="frameData"></div>
<div style="clear:both;"></div>
<h3>Hand data:</h3>
<div id="handData"></div>
<div style="clear:both;"></div>
<h3>Finger data:</h3>
<div id="pointableData"></div>
<div style="clear:both;"></div>
</div>
</body>
</html>