-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo.html
251 lines (201 loc) · 7.11 KB
/
Demo.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html>
<head>
<title>Leap -> Mouse Events Adapter</title>
<style>
body { margin: 0px; }
#fingerOverlay {
background-color: rgba(255,255,255,0.0);
pointer-events: none; /* ensure regular mouse events go through it */
position: fixed;
left: 0px; top: 0px;
margin: 0px; padding: 0px; }
</style>
<script type='text/JavaScript' src='./lib/raphael-min.js'></script>
<script type='text/JavaScript' src='./lib/jquery-1.8.3.min.js'></script>
<script>
var ws;
// Support both the WebSocket and MozWebSocket objects
if ((typeof(WebSocket) == 'undefined') &&
(typeof(MozWebSocket) != 'undefined')) {
WebSocket = MozWebSocket;
}
// function drawFinger(leapPos, context){
// context.fillStyle = 'blue';
// context.strokeStyle = '#0044FF';
// context.beginPath();
// context.arc(100,75,50,0, Math.PI * 2, true );
// context.stroke();
// console.log("drew finger");
// }
function scaleValue(val, min, max, rangeMin, rangeMax){
if(val < min){ val = min; }
if(val > max ){ val = max; }
return ((val - min) / (max - min) ) * (rangeMax - rangeMin) + rangeMin;
};
// function setParticlePosition(particle, leapPos){
// var x = scaleValue(leapPos.x, minX, maxX, -window.innerWidth, window.innerWidth);
// var y = scaleValue(leapPos.y, minY, maxY, -400, window.innerHeight);
// var z = scaleValue(leapPos.z, minZ, maxZ, -depth/2, depth/2);
// particle.position.set(x,y,z);
// };
var fingerOutlineColor = "#0000FF";
var fingerOutlineTouchingColor = "#00FF00";
var fingerCenterColor = "#0044FF";
var fingerCenterTouchingColor = fingerOutlineTouchingColor;
var minX = -200;
var maxX = 200;
var minY = 20;
var maxY = 300;
var minZ = -100;
var maxZ = 200;
var depth = 300;
var motionScale = 2.0;
var minR = 5;
var maxR = 15;
// values less than this are considered touching
var touchDepth = 0;
// position of where a touch event started (or null if not touching)
var touchStart = null;
var touchStartEl = null;
function createMouseEvent(x, y, type){
var mouseEvent = document.createEvent("MouseEvent");
mouseEvent.initMouseEvent(type, true, true, window, 1, x, y, x, y, false, false, false, false, 0, null);
return mouseEvent;
}
function fireMouseClick(x, y, element){
var clickEvent = createMouseEvent(x, y, 'click');
element.dispatchEvent(clickEvent);
// if (event.initMouseEvent) { // all browsers except IE before version 9
// var clickEvent = document.createEvent ("MouseEvent");
// clickEvent.initMouseEvent ("click", true, true, window, 0,
// event.screenX, event.screenY, event.clientX, event.clientY,
// event.ctrlKey, event.altKey, event.shiftKey, event.metaKey,
// 0, null);
// event.target.dispatchEvent (clickEvent);
// } else {
// if (document.createEventObject) { // IE before version 9
// var clickEvent = document.createEventObject (window.event);
// clickEvent.button = 1; // left click
// event.srcElement.fireEvent ("onclick", clickEvent);
// }
// }
};
function fireMouseDown(x, y, element){
var downEvent = createMouseEvent(x, y, 'mousedown');
element.dispatchEvent(downEvent);
};
function fireMouseUp(x, y, element){
var upEvent = createMouseEvent(x, y, 'mouseup');
element.dispatchEvent(upEvent);
};
function onTouch(x, y){
touchStart = {
x: x,
y: y
};
touchStartEl = document.elementFromPoint(x, y) || window;
fireMouseDown(x, y, touchStartEl);
};
function onTouchUp(x, y){
var touchEndEl = document.elementFromPoint(x, y) || window;
fireMouseUp(x, y, touchEndEl);
if(touchEndEl === touchStartEl){
fireMouseClick(x, y, touchStartEl);
}
touchStart = null;
touchStartEl = null;
};
function updateFingers(leapData, fingerOutline, fingerCenter){
// redo this every time incase of window resize TODO add resize listener
var windowWidth =$(window).width();
var windowHeight = $(window).height();
if(leapData.hands && leapData.hands.length > 0){
var hand = leapData.hands[0];
if(hand.fingers && hand.fingers.length > 0){
fingerOutline.show();
fingerCenter.show();
var leapPos = hand.fingers[0].tip.position;
var x = scaleValue(leapPos[0] * motionScale, minX, maxX, 0, windowWidth);
var y = scaleValue(leapPos[1] * motionScale, minY, maxY, windowHeight, 0);
//var z = scaleValue(leapPos[2] * motionScale, minZ, maxZ, -depth/2, depth/2);
var z = leapPos[2];
var r = scaleValue(z * motionScale, 0, maxZ, maxR, minR);
//TODO move this to an animation loop, so hides and shows work (unless Leap fixes their socket server)
fingerOutline.attr("cx", x);
fingerOutline.attr("cy", y);
fingerCenter.attr("cx", x);
fingerCenter.attr("cy", y);
fingerCenter.attr("r", r);
if(z < touchDepth){
fingerOutline.attr("stroke", fingerOutlineTouchingColor);
fingerCenter.attr("fill", fingerCenterTouchingColor);
onTouch(x, y);
}
else{
fingerOutline.attr("stroke", fingerOutlineColor);
fingerCenter.attr("fill", fingerCenterColor);
onTouchUp(x, y);
}
return;
}
}
fingerOutline.hide();
fingerCenter.hide();
}
// Create the socket with event handlers
function init() {
//Create and open the socket
ws = new WebSocket("ws://localhost:6437/");
// On successful connection
ws.onopen = function(event) {
document.getElementById("main").style.visibility = "visible";
document.getElementById("connection").innerHTML = "WebSocket connection open!";
};
// On message received
ws.onmessage = function(event) {
var obj = JSON.parse(event.data);
var str = JSON.stringify(obj, undefined, 2);
document.getElementById("output").innerHTML = '<pre>' + str + '</pre>';
updateFingers(obj, fingerOutline, fingerCenter);
};
// On socket close
ws.onclose = function(event) {
ws = null;
document.getElementById("main").style.visibility = "hidden";
document.getElementById("connection").innerHTML = "WebSocket connection closed";
};
//On socket error
ws.onerror = function(event) {
alert("Received error");
};
var width =$(window).width();
var height = $(window).height();
var paper = Raphael('fingerOverlay', width, height);
var fingerOutline = paper.circle(100, 75, maxR);
fingerOutline.attr("stroke", fingerOutlineColor);
var fingerCenter = paper.circle(100, 75, minR);
fingerCenter.attr("fill", fingerCenterColor);
}
function toggleColor(element){
if( element.style.backgroundColor == "rgb(255, 0, 0)" ){
element.style.backgroundColor = "rgb(0, 255, 0)";
}
else{
element.style.backgroundColor = "rgb(255, 0, 0)";
}
};
</script>
</head>
<body onload="init();">
<div id='fingerOverlay'></div>
<!-- <canvas id='fingerOverlay' width='100%' height= '100%' ></canvas> -->
<h1>Leap JavaScript/WebSocket Sample</h1>
<div style="width:400px;height:500px;background-color:#FF0000" onclick="toggleColor(this)"></div>
<div id="connection">WebSocket not connected</div>
<div id="main" style="visibility:hidden">
<p>JSON Frame data:</p>
<div id="output"></div>
</div>
</body>
</html>