-
Notifications
You must be signed in to change notification settings - Fork 4
/
leapMotion.html
262 lines (231 loc) · 10.1 KB
/
leapMotion.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<!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. |
\------------------------------------------------------------------------------/
-->
<!--
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>
// Store frame for motion functions
var previousFrame = null;
var paused = false;
var pauseOnGesture = false;
// Setup Leap loop with frame callback function
var controllerOptions = {enableGestures: true};
// 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 />"
+ "Tools: " + frame.tools.length + "<br />"
+ "Gestures: " + frame.gestures.length + "<br />";
// Frame motion factors
if (previousFrame && previousFrame.valid) {
var translation = frame.translation(previousFrame);
frameString += "Translation: " + vectorToString(translation) + " mm <br />";
var rotationAxis = frame.rotationAxis(previousFrame);
var rotationAngle = frame.rotationAngle(previousFrame);
frameString += "Rotation axis: " + vectorToString(rotationAxis, 2) + "<br />";
frameString += "Rotation angle: " + rotationAngle.toFixed(2) + " radians<br />";
var scaleFactor = frame.scaleFactor(previousFrame);
frameString += "Scale factor: " + scaleFactor.toFixed(2) + "<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];
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 />";
// Hand motion factors
if (previousFrame && previousFrame.valid) {
var translation = hand.translation(previousFrame);
handString += "Translation: " + vectorToString(translation) + " mm<br />";
var rotationAxis = hand.rotationAxis(previousFrame, 2);
var rotationAngle = hand.rotationAngle(previousFrame);
handString += "Rotation axis: " + vectorToString(rotationAxis) + "<br />";
handString += "Rotation angle: " + rotationAngle.toFixed(2) + " radians<br />";
var scaleFactor = hand.scaleFactor(previousFrame);
handString += "Scale factor: " + scaleFactor.toFixed(2) + "<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 and tool) 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'>";
if (pointable.tool) {
pointableString += "Pointable ID: " + pointable.id + "<br />";
pointableString += "Classified as a tool <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 += "Tip position: " + vectorToString(pointable.tipPosition) + " mm<br />"
pointableString += "</div>";
}
else {
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;
// Display Gesture object data
var gestureOutput = document.getElementById("gestureData");
var gestureString = "";
if (frame.gestures.length > 0) {
if (pauseOnGesture) {
togglePause();
}
for (var i = 0; i < frame.gestures.length; i++) {
var gesture = frame.gestures[i];
gestureString += "Gesture ID: " + gesture.id + ", "
+ "type: " + gesture.type + ", "
+ "state: " + gesture.state + ", "
+ "hand IDs: " + gesture.handIds.join(", ") + ", "
+ "pointable IDs: " + gesture.pointableIds.join(", ") + ", "
+ "duration: " + gesture.duration + " µs, ";
switch (gesture.type) {
case "circle":
gestureString += "center: " + vectorToString(gesture.center) + " mm, "
+ "normal: " + vectorToString(gesture.normal, 2) + ", "
+ "radius: " + gesture.radius.toFixed(1) + " mm, "
+ "progress: " + gesture.progress.toFixed(2) + " rotations";
break;
case "swipe":
gestureString += "start position: " + vectorToString(gesture.startPosition) + " mm, "
+ "current position: " + vectorToString(gesture.position) + " mm, "
+ "direction: " + vectorToString(gesture.direction, 1) + ", "
+ "speed: " + gesture.speed.toFixed(1) + " mm/s";
break;
case "screenTap":
case "keyTap":
gestureString += "position: " + vectorToString(gesture.position) + " mm";
break;
default:
gestureString += "unkown gesture type";
}
gestureString += "<br />";
}
}
else {
gestureString += "No gestures";
}
gestureOutput.innerHTML = gestureString;
// Store frame for motion functions
previousFrame = frame;
})
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";
}
}
function pauseForGestures() {
if (document.getElementById("pauseOnGesture").checked) {
pauseOnGesture = true;
} else {
pauseOnGesture = false;
}
}
</script>
</head>
<body>
<h1>Leap Motion JavaScript Sample</h1>
<div id="main">
<button id="pause" onclick="togglePause()">Pause</button>
<input type="checkbox" id="pauseOnGesture" onclick="pauseForGestures()">Pause on gesture</input>
<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 and tool data:</h3>
<div id="pointableData"></div>
<div style="clear:both;"></div>
<h3>Gesture data:</h3>
<div id="gestureData"></div>
</div>
</body>
</html>