- 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
Latest commit
executable file
·83 lines (68 loc) · 2.43 KB
/
Copy pathLeap_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>
varws;
if((typeof(WebSocket)=='undefined')&&
(typeof(MozWebSocket)!='undefined')){
WebSocket=MozWebSocket;
}
functioninit(){
ws=newWebSocket("ws://localhost:6437/");
ws.onopen=function(event){
document.getElementById("main").style.visibility="visible";
document.getElementById("connection").innerHTML="WebSocket connection open!";
};
varactionStartTime=newDate().getTime();
vartimeThreshold=300;
// is gun ready to be fired?
varcocked;
// 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
varXCORRECTION=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
varYCORRECTION=480;// to be substracted from tip y position (position[1])
ws.onmessage=function(event){
varobj=JSON.parse(event.data);
varstr=JSON.stringify(obj,undefined,2);
varposX=XCORRECTION+Math.floor(obj.hands[0].fingers[0].tip.position[0]);
varposY=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.
varfingercount=obj.hands[0].fingers.length;
if(fingercount==2){
cocked=1;// 1 == true
}elseif(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>
<bodyonload="init();">
<h1>Leap JavaScript API</h1>
<divid="connection">WebSocket not connected</div>
<divid="main" style="visibility:hidden">
<p>Frame data:</p>
<divid="output"></div>
</div>
</body>
</html>