-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeap_JavaScript_API_trigger_point.html
More file actions
executable file
·83 lines (68 loc) · 2.43 KB
/
Leap_JavaScript_API_trigger_point.html
File metadata and controls
executable file
·83 lines (68 loc) · 2.43 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Leap JavaScript API</title>
<script>
var ws;
if ((typeof(WebSocket) == 'undefined') &&
(typeof(MozWebSocket) != 'undefined')) {
WebSocket = MozWebSocket;
}
function init() {
ws = new WebSocket("ws://localhost:6437/");
ws.onopen = function(event) {
document.getElementById("main").style.visibility = "visible";
document.getElementById("connection").innerHTML = "WebSocket connection open!";
};
var actionStartTime = new Date().getTime();
var timeThreshold = 300;
// is gun ready to be fired?
var cocked;
// co-ordinate corrections for converting tip positon to canvas position
// where canvas top left corner is at (0,0)
// finger tip position at left of range of motion is approx -230
// finger tip position at right of range of motion is approx +230
var XCORRECTION = 230; // to be added to tip x position (position[0])
// tip position at top of range of motion is approx 480
// tip position at bottom of range of motion is approx 80
var YCORRECTION = 480; // to be substracted from tip y position (position[1])
ws.onmessage = function(event) {
var obj = JSON.parse(event.data);
var str = JSON.stringify(obj, undefined, 2);
var posX = XCORRECTION + Math.floor( obj.hands[0].fingers[0].tip.position[0]);
var posY = YCORRECTION - Math.floor( obj.hands[0].fingers[0].tip.position[1]);
document.getElementById("output").innerHTML = '<pre>' + "posX:"+ posX + " posY:" + posY+ '</pre>';
// gun fires when finger count goes from cocked position (2 fingers) to 1 finger.
var fingercount = obj.hands[0].fingers.length;
if (fingercount == 2) {
cocked = 1; // 1 == true
} else if (fingercount == 1) {
if (cocked) {
// shot fired!
console.log("shot fired at", posX, posY);
}
cocked = 0; // 0 == false
} else {
cocked = 0; // 0 == false
}
};
ws.onclose = function(event) {
ws = null;
document.getElementById("main").style.visibility = "hidden";
document.getElementById("connection").innerHTML = "WebSocket connection closed";
};
ws.onerror = function(event) {
alert("Received error");
};
}
</script>
</head>
<body onload="init();">
<h1>Leap JavaScript API</h1>
<div id="connection">WebSocket not connected</div>
<div id="main" style="visibility:hidden">
<p>Frame data:</p>
<div id="output"></div>
</div>
</body>
</html>