-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleapify.js
458 lines (372 loc) · 12.8 KB
/
leapify.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
(function(){
function leapify(){
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;
// trial and error determined range of values
var minX = -200;
var maxX = 200;
var minY = 30;
var maxY = 300;
var minZ = -100;
var maxZ = 200;
var depth = 300;
var motionScale = 2.0;
var minR = 5;
var maxR = 15;
function FingerMarker(paper){
this.x = 100;
this.y = 75;
this.z = 0;
this.fingerOutline = paper.circle(this.x, this.y, maxR);
this.fingerOutline.attr("stroke", fingerOutlineColor);
this.fingerCenter = paper.circle(this.x, this.y, minR);
this.fingerCenter.attr("fill", fingerCenterColor);
this.touching = false;
}
FingerMarker.prototype.setPosition = function(x, y, z){
this.x = x;
this.y = y;
this.z = z;
this.fingerOutline.attr("cx", x);
this.fingerOutline.attr("cy", y);
this.fingerCenter.attr("cx", x);
this.fingerCenter.attr("cy", y);
var r = scaleValue(z * motionScale, 0, maxZ, maxR, minR);
this.fingerCenter.attr("r", r);
var touching = z < touchDepth;
if(this.touching != touching){
this.setTouching(touching);
}
return this.touching;
};
FingerMarker.prototype.updateState = function(){
var pos = {
x: this.x,
y: this.y
};
// now touching
if(this.touching){
if(this.touchStart){
// was previously touching
fireMouseMove(pos, window);
}
else{
// start of a new touch
this.onTouch(pos);
}
}
else if(this.touchStart){
// was previously touching
this.onTouchUp(pos);
}
};
FingerMarker.prototype.setTouching = function(touching) {
var newOutlineColor;
var newCenterColor;
if(touching){
newOutlineColor = fingerOutlineTouchingColor;
newCenterColor = fingerCenterTouchingColor;
}
else{
newOutlineColor = fingerOutlineColor;
newCenterColor = fingerCenterColor;
}
this.fingerOutline.attr("stroke", newOutlineColor);
this.fingerCenter.attr("fill", newCenterColor);
this.touching = touching;
};
FingerMarker.prototype.onTouch = function(pos){
this.touchStart = pos;
this.touchStartEl = document.elementFromPoint(pos.x, pos.y);
if(this.touchStartEl){
var offset = $(this.touchStartEl).offset();
this.touchStartClient = {
x: this.touchStart.x - offset.x,
y: this.touchStart.y - offset.y
};
}
else{
this.touchStartEl = window;
this.touchStartClient = this.touchStart;
}
fireMouseDown(this.touchStartClient, this.touchStartEl);
};
FingerMarker.prototype.clearTouchInfo = function(){
this.touchStart = null;
this.touchStartEl = null;
this.touchStartClient = null;
};
FingerMarker.prototype.onTouchUp = function(pos){
var touchEndClient;
var touchEndEl = document.elementFromPoint(pos.x, pos.y);
if(touchEndEl){
var offset = $(touchEndEl).offset();
touchEndClient = {
x: pos.x - offset.x,
y: pos.y - offset.y
};
}
else{
touchEndEl = window;
touchEndClient = pos;
}
fireMouseUp(touchEndClient, touchEndEl);
if(touchEndEl === this.touchStartEl){
fireMouseClick(touchEndClient, touchEndEl);
}
this.clearTouchInfo();
};
FingerMarker.prototype.hide = function() {
this.fingerOutline.hide();
this.fingerCenter.hide();
};
FingerMarker.prototype.show = function() {
this.fingerOutline.show();
this.fingerCenter.show();
};
FingerMarker.prototype.reset = function(){
this.hide();
this.setTouching(false);
this.clearTouchInfo();
};
var fingerMarkers = [];
// values less than this are considered touching
var touchDepth = 0;
// position of where a touch event started in screen coordinates (or null if not touching)
var touchStart = null;
// position of where a touch event started in element parent coordinates (or null if not touching)
var touchStartClient = null;
// The element that was touched, if any
var touchStartEl = null;
function createMouseEvent(pos, type, detail){
var bubbles = true;
var cancellable = true;
var mouseEvent = document.createEvent("MouseEvent");
mouseEvent.initMouseEvent(type, bubbles, cancellable, window,
detail,
0, 0,
pos.x, pos.y,
false, false, false, false, // modifier keys ctrl, alt, shift, meta
0, null);
return mouseEvent;
}
function fireMouseClick(pos, element){
var numberOfClicks = 1;
var clickEvent = createMouseEvent(pos, 'click', numberOfClicks);
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(pos, element){
var buttonPressed = 0; // left
var downEvent = createMouseEvent(pos, 'mousedown', buttonPressed);
element.dispatchEvent(downEvent);
}
function fireMouseUp(pos, element){
var buttonPressed = 0; // left
var upEvent = createMouseEvent(pos, 'mouseup', buttonPressed);
element.dispatchEvent(upEvent);
}
function fireMouseMove(pos, element){
var moveEvent = createMouseEvent(pos, 'mousemove', 0);
element.dispatchEvent(moveEvent);
}
function getFingers(leapData){
var fingers = [];
if(leapData.hands && leapData.hands.length > 0){
var hand = leapData.hands[0];
if(hand.fingers && hand.fingers.length > 0){
return hand.fingers;
}
}
return fingers;
}
var scrollStart = null;
function processScrollGesture(fingerMarkers, lastTouch){
// check if currenly scrolling
if(scrollStart){
// check if we still have at least one touch
if(lastTouch){
// var deltaX = lastTouch.x - scrollStart.x;
// var deltaY = lastTouch.y - scrollStart.y;
// window.scrollBy(deltaX, deltaY);
window.scrollTo(lastTouch.x, lastTouch.y);
return;
}
}
else if(lastTouch){
// started scrolling
scrollStart = {
x: lastTouch.x,
y: lastTouch.y
};
return;
}
scrollStart = null;
}
function updateFingers(leapData){
// redo this every time incase of window resize (TODO add resize listener instead)
var windowWidth =$(window).width();
var windowHeight = $(window).height();
var fingers = getFingers(leapData);
var noFingers = fingers.length;
var noMarkers = fingerMarkers.length;
var lastTouch = null;
var m = 0;
for(var f=0; f < noFingers && m < noMarkers; f++, m++){
var finger = fingers[f];
var marker = fingerMarkers[m];
marker.show();
var leapPos = finger.tip.position;
var x = scaleValue(leapPos[0] * motionScale, minX, maxX, 0, windowWidth);
var y = scaleValue(leapPos[1] * motionScale, minY, maxY, windowHeight, 0);
//console.log("leap y =", leapPos[1], "(",minY,"/",maxY,") ", "scaled y =", y, "(",0,"/",windowHeight,")");
//var z = scaleValue(leapPos[2] * motionScale, minZ, maxZ, -depth/2, depth/2);
var z = leapPos[2];
var touching = marker.setPosition(x, y, z);
if(touching){
lastTouch = marker;
}
}
if(noFingers == 1){
fingerMarkers[0].updateState();
}
else if(noFingers > 1){
processScrollGesture(fingerMarkers, lastTouch);
}
// reset any unmatched markers
for(; m < noMarkers; m++){
fingerMarkers[m].reset();
}
}
function main(){
var fingerOverlay = document.createElement('div');
fingerOverlay.id = 'fingerOverlay';
var overlayStyle = fingerOverlay.style;
overlayStyle.backgroundColor = 'rgba(255,255,255,0.0)';
overlayStyle.pointerEvents = 'none';
overlayStyle.position = 'fixed';
overlayStyle.left = '0px';
overlayStyle.top = '0px';
overlayStyle.margin = '0px';
overlayStyle.padding = '0px';
overlayStyle.zIndex = 99999;
document.body.appendChild(fingerOverlay);
//Create and open the socket
ws = new WebSocket("ws://localhost:6437/");
// On successful connection
ws.onopen = function(event) {
console.log("WebSocket connection open!");
};
var processMessage = true;
// On message received
ws.onmessage = function(event) {
if(processMessage){
var obj = JSON.parse(event.data);
var str = JSON.stringify(obj, undefined, 2);
updateFingers(obj);
}
// do this so we only process every 2nd message to rate limit a bit
processMessage = !processMessage;
};
// On socket close
ws.onclose = function(event) {
ws = null;
console.log("WebSocket connection closed");
};
//On socket error
ws.onerror = function(event) {
alert("Received error from Leap");
};
var width =$(window).width();
var height = $(window).height();
var paper = Raphael('fingerOverlay', width, height);
// support up to 2 fingers for now
var noFingers = 2;
for(var i=0; i < noFingers; i++){
fingerMarkers.push( new FingerMarker(paper) );
}
console.log("leapify loaded");
}
function loadScript(path, onLoad){
var script = document.createElement('script');
script.src = path;
script.onload = onLoad;
document.body.appendChild(script);
}
function loadScripts(scripts){
var scriptsToLoad = scripts.length;
var onLoad = function(){
scriptsToLoad--;
if(scriptsToLoad === 0){
main();
}
};
for(var i=0, l=scripts.length; i < l; i++){
loadScript(scripts[i], onLoad);
}
}
loadScripts(['https://raw.github.com/markmsmith/LeapBrowserMouseEvents/master/lib/raphael-min.js',
'https://raw.github.com/markmsmith/LeapBrowserMouseEvents/master//lib/jquery-1.8.3.min.js']);
}
if(typeof(chrome) !== "undefined" &&
typeof(chrome.extension) !== "undefined"){
var refreshListener = function (request, sender, sendResponse) {
if (request.refresh) {
location.reload();
}
};
chrome.extension.onMessage.addListener(refreshListener);
chrome.extension.sendMessage({
checkActivated: true
},
function (response) {
if( response.isActivated ) {
leapify();
}
}
);
}
else{
leapify();
}
})();