forked from nandico/leap-JS-RockPaperScissors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeapHelper.js
137 lines (116 loc) · 2.67 KB
/
LeapHelper.js
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
/**
* @desc Simple JS Helper methods used in Rock Paper Scissors JS
* @author: Nandico [email protected]
*/
function LeapHelper()
{
// frame reference for current instance
this.frame = null;
// system z treshold used to ignore head
this.HAND_Z_THRESHOLD = 200;
/**
* @desc Writes a generic output
*/
this.log = function( key, value )
{
document.getElementById("output").innerHTML += "<pre>" + key + ": " + value + "</pre>";
}
/**
* @desc Writes a screen output
*/
this.logScream = function( value )
{
document.getElementById("output").innerHTML += "<h2>" + value + "</h2>";
}
/**
* @desc Reset screen log
*/
this.resetLog = function()
{
document.getElementById("output").innerHTML = "";
}
/**
* @desc Setter for actual LEAP frame
*/
this.setFrame = function( frame )
{
this.frame = frame;
}
// return the actual system pointable count
this.getPointablecount = function()
{
if( this.frame && this.frame.pointables )
{
return this.frame.pointables.length;
}
else
{
return 0;
}
}
/**
* @desc Count hands over LEAP sensor
*/
this.getHandscount = function()
{
if( this.frame && this.frame.hands )
{
return this.frame.hands.length;
}
else
{
return 0;
}
}
/**
* @desc Count pointables grouping for each hand
*/
this.getPointableCountByHand = function()
{
var pointableCount = new Object();
if( this.frame && this.frame.hands && this.frame.pointables )
{
pointableCount.hands = new Array();
for( var hand in this.frame.hands )
{
handZ = this.frame.hands[ hand ].palmPosition[ 2 ];
if( handZ < this.HAND_Z_THRESHOLD ) // apply Z-axis treshold to avoid head detection
{
var handObject = new Object();
handObject.id = this.frame.hands[ hand ].id;
handObject.z = this.frame.hands[ hand ].palmPosition[ 2 ];
handObject.pointableCount = 0;
for( var pointable in this.frame.pointables )
{
if( this.frame.pointables[ pointable ].handId == handObject.id )
{
handObject.pointableCount ++;
}
}
pointableCount.hands.push( handObject );
}
}
}
return pointableCount;
}
/**
* @desc Get the most proeminent hand over leap sensor
* This strategy consider the hand with smaller z-axis position
*/
this.getMoreProeminentHand = function()
{
pointableCount = this.getPointableCountByHand();
closer = 5000;
selectedHand = null;
for( var hand in pointableCount.hands )
{
actualHand = pointableCount.hands[ hand ];
if( actualHand.z < closer )
{
selectedHand = pointableCount.hands[ hand ];
closer = actualHand.z;
}
}
return selectedHand;
}
}