-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleap-test-3.html
235 lines (198 loc) · 7.19 KB
/
leap-test-3.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Leap Test 3 - two hands</title>
<meta name="description" content="Leap Test 1">
<meta name="author" content="Bill Fisher">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
.avatar {
height:30px;
width: 30px;
-webkit-border-radius: 50%;
position: absolute;
}
#avatar1 {
background: #f00;
}
#avatar2 {
background: #00f;
}
</style>
<script src="js/leap.js"></script>
</head>
<body>
<h1>Leap Test 3 - two hands</h1>
<h2>Notes:</h2>
<ul>
<li>Hand data is more unreliable than pointable data.</li>
<li>This code got pretty messy, and I consider this test to be somewhat of a code disaster.</li>
</ul>
<h2>Data:</h2>
<div id="pointable-data"></div>
<div id="avatar1" class="avatar"></div>
<div id="avatar2" class="avatar"></div>
<script>
var docWidth = document.width;
var docHeight = document.height;
var dataContainer = document.getElementById("pointable-data");
var avatars = Array.prototype.slice.call(document.querySelectorAll(".avatar"));
avatars.forEach(function (avatar) {
avatar.style.left = (docWidth / 2) - (avatar.offsetWidth / 2) + "px";
avatar.style.top = (docHeight / 2) - (avatar.offsetHeight / 2) + "px";
});
// comparator
//
// Get a comparator function for use in sorting an array of objects or an array of arrays.
//
// example usage:
// [{foo: 2}, {foo: 1}, {foo: 3}].sort(comparator("foo")); // returns [{foo: 1}, {foo: 2}, {foo: 3}]
// [[1, 2], [0, 3], [2, 0]].sort(comparator(1)); // returns [[2, 0], [1, 2], [0, 3]]
//
// @param property String the object property or array index to compare.
var comparator = function (property) {
return function (a, b) {
if (a[property] < b[property]) return -1;
if (a[property] > b[property]) return 1;
return 0;
}
};
// convert
//
// Converts a number in range A to a number in range B.
//
// @param aRangeVal Number
// @param aRange Array [min, max]
// @param bRange Array [min, max]
var convert = function (aRangeVal, aRange, bRange) {
return ((aRangeVal - aRange[0]) / (aRange[1] - aRange[0])) * (bRange[1] - bRange[0]) + bRange[0];
};
var vectorToString = function (vector, digits) {
if (typeof digits === "undefined") {
digits = 1;
}
return "(" + vector[0].toFixed(digits) + ", "
+ vector[1].toFixed(digits) + ", "
+ vector[2].toFixed(digits) + ")";
};
// compact
//
// Compacts an array by removing unneeded values.
// By default it removes all falsy values, but it can be set to remove specific values such as undefined.
//
// @param arr Array the array to compact
// @param val Value any value to not include in the returned array. The special flag "falsyExceptZero" may be used as well.
var compact = function (arr, val) {
var len = arr.length,
i = len - 1,
valPresent = typeof val === undefined;
for (; i >= 0; i--) {
if (valPresent) {
if (val === "falsyExceptZero" && (arr[i] !== 0 && !arr[i])) {
arr.splice(i, 1);
}
else if (val === arr[i]) {
arr.splice(i, 1);
}
}
else if (!arr[i]) {
arr.splice(i, 1);
}
}
return arr;
};
var propertyAverage = function (arr, property) {
var total = 0;
var i = 0;
var len = arr.length;
for (; i<len; i+=1) {
total += arr[i][property];
}
return parseInt(total / arr.length, 10);
};
var average = function (arr) {
var sum = arr.reduce(function(a, b) { return a + b });
return sum / arr.length;
}
// Store frame data for motion functions
var previousFramesData = [];
// Setup Leap loop with frame callback function
var controllerOptions = {enableGestures: false};
// approximate min and max values of the pickup area
var positionRangeX = [-400, 400];
var positionRangeY = [50, 600];
// the area where the avatars will be drawn
var translateRangeX = [(docWidth / 2) * -1, (docWidth / 2)];
var translateRangeY = [(docHeight / 2), (docHeight / 2) * -1];
Leap.loop(controllerOptions, function(frame) {
if (frame.hands.length > 0) {
// sort the hands by the x position of the palm
var pointers = frame.hands
.map(function (hand) {
return {
hand: hand,
positionX: hand.palmPosition[0]
};
})
.sort(comparator("positionX"))
.slice(0,2);
var pointableString = "<div>";
pointers.forEach(function (pointer) {
pointableString
+= "handX: " + pointer.hand.palmPosition[0]
+ " handY: " + pointer.hand.palmPosition[1]
+ " handZ: " + pointer.hand.palmPosition[1]
+ "<br />";
});
var avgPositions = [ {x: null, y: null}, {x: null, y: null} ],
axisArray;
pointers.forEach( function (pointer, i) {
if (previousFramesData.length) {
["x", "y"].forEach(function (axis) {
axisArray = compact(previousFramesData.map(function (data) {
if (data[i]) {
return data[i][axis];
}
}));
if (axisArray.length) {
avgPositions[i][axis] = average(axisArray);
}
});
}
});
pointableString += "</div>";
pointers.forEach(function (pointer, i) {
if (typeof avgPositions[i].x === "number") {
var translateX = parseInt(convert(avgPositions[i].x, positionRangeX, translateRangeX), 10) + "px";
var translateY = parseInt(convert(avgPositions[i].y, positionRangeY, translateRangeY), 10) + "px";
if (avatars[i]) {
avatars[i].style.visibility = "visible";
avatars[i].style.webkitTransform = "translate3d(" + translateX + ", " + translateY + ", 0)";
}
}
});
if (previousFramesData.length > 9) {
previousFramesData.shift();
}
var frameData = pointers.map(function (pointer) {
return {
handId: pointer.hand.id,
x: pointer.hand.palmPosition[0],
y: pointer.hand.palmPosition[1]
};
});
previousFramesData.push(frameData);
}
else {
var pointableString = "no data";
avatars.forEach(function (avatar) {
avatar.style.visibility = "hidden";
avatar.style.webkitTransform = "translate3d(0, 0, 0)";
});
}
dataContainer.innerHTML = pointableString;
});
</script>
</body>
</html>