-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
173 lines (152 loc) · 5.69 KB
/
index.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
<!DOCTYPE html>
<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
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 = "Hands: " + frame.hands.length + "<br />"
// Frame motion factors
if (previousFrame && previousFrame.valid) {
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 += "Type: " + hand.type + " hand" + "<br />";
handString += "Direction: " + vectorToString(hand.direction, 2) + "<br />";
handString += "Arm direction: " + vectorToString(hand.arm.direction()) + "<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 extendedFingers = 0;
for(var f = 0; f < hand.fingers.length; f++){
var finger = hand.fingers[f];
if(finger.extended) extendedFingers++;
}
handString += "Extended Fingers: " + extendedFingers + "<br />";
handString += "</div>";
}
}
else {
handString += "No hands";
}
handOutput.innerHTML = handString;
// 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>
<div id="pointableData"></div>
<div style="clear:both;"></div>
<h3>Gesture data:</h3>
<div id="gestureData"></div>
</div>
</body>
</html>