-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leap.html
305 lines (268 loc) · 12.1 KB
/
Leap.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
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
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script src="http://js.leapmotion.com/0.2.0-beta6/leap.js"></script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script>/*! Copyright (c) 2013 Brandon Aaron (http://brandonaaron.net)
* Licensed under the MIT License (LICENSE.txt).
*
* Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
* Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
* Thanks to: Seamus Leahy for adding deltaX and deltaY
*
* Version: 3.1.3
*
* Requires: 1.2.2+
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS style for Browserify
module.exports = factory;
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
var toFix = ['wheel', 'mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'];
var toBind = 'onwheel' in document || document.documentMode >= 9 ? ['wheel'] : ['mousewheel', 'DomMouseScroll', 'MozMousePixelScroll'];
var lowestDelta, lowestDeltaXY;
if ($.event.fixHooks) {
for (var i = toFix.length; i;) {
$.event.fixHooks[toFix[--i]] = $.event.mouseHooks;
}
}
$.event.special.mousewheel = {
setup: function () {
if (this.addEventListener) {
for (var i = toBind.length; i;) {
this.addEventListener(toBind[--i], handler, false);
}
} else {
this.onmousewheel = handler;
}
},
teardown: function () {
if (this.removeEventListener) {
for (var i = toBind.length; i;) {
this.removeEventListener(toBind[--i], handler, false);
}
} else {
this.onmousewheel = null;
}
}
};
$.fn.extend({
mousewheel: function (fn) {
return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
},
unmousewheel: function (fn) {
return this.unbind("mousewheel", fn);
}
});
function handler(event) {
var orgEvent = event || window.event,
args = [].slice.call(arguments, 1),
delta = 0,
deltaX = 0,
deltaY = 0,
absDelta = 0,
absDeltaXY = 0,
fn;
event = $.event.fix(orgEvent);
event.type = "mousewheel";
// Old school scrollwheel delta
if (orgEvent.wheelDelta) { delta = orgEvent.wheelDelta; }
if (orgEvent.detail) { delta = orgEvent.detail * -1; }
// New school wheel delta (wheel event)
if (orgEvent.deltaY) {
deltaY = orgEvent.deltaY * -1;
delta = deltaY;
}
if (orgEvent.deltaX) {
deltaX = orgEvent.deltaX;
delta = deltaX * -1;
}
// Webkit
if (orgEvent.wheelDeltaY !== undefined) { deltaY = orgEvent.wheelDeltaY; }
if (orgEvent.wheelDeltaX !== undefined) { deltaX = orgEvent.wheelDeltaX * -1; }
// Look for lowest delta to normalize the delta values
absDelta = Math.abs(delta);
if (!lowestDelta || absDelta < lowestDelta) { lowestDelta = absDelta; }
absDeltaXY = Math.max(Math.abs(deltaY), Math.abs(deltaX));
if (!lowestDeltaXY || absDeltaXY < lowestDeltaXY) { lowestDeltaXY = absDeltaXY; }
// Get a whole value for the deltas
fn = delta > 0 ? 'floor' : 'ceil';
delta = Math[fn](delta / lowestDelta);
deltaX = Math[fn](deltaX / lowestDeltaXY);
deltaY = Math[fn](deltaY / lowestDeltaXY);
// Add event and delta to the front of the arguments
args.unshift(event, delta, deltaX, deltaY);
return ($.event.dispatch || $.event.handle).apply(this, args);
}
}));</script>
</head>
<body>
<div id="container"></div>
<script>
var lastframe;
var lasthand1;
var lasthand2;
var zoom = 1.0;
var gesture_id;
var oldgesture;
function handleFrame(frame) {
//var mydiv = document.getElementById("foo");
//foo.innerHTML = frame;
if (frame.gestures.length > 0) {
var gesture = frame.gestures[0];
if (gesture.state == "start") {
window.gesture_id = gesture.id;
window.oldgesture = gesture;
return;
}
if (gesture.type == "swipe") {
if (gesture.direction[0] > 0) { //to the left
chart.xAxis[0].setExtremes(chart.xAxis[0].getExtremes().min - 1, chart.xAxis[0].getExtremes().max - 1)
}
else {
chart.xAxis[0].setExtremes(chart.xAxis[0].getExtremes().min + 1, chart.xAxis[0].getExtremes().max + 1);
}
}
}
else if (frame.hands.length == 2) {
if (window.lastframe != undefined) {
if (frame.id - window.lastframe.id < 15)
return;
}
// console.log(frame.hands[0].palmVelocity + "|" + frame.hands[1].palmVelocity);
// to detect zoom in / out :
// leapjs returns (x,y,z) speed in mm/s for each hand
//coordinate system :
//
// ^ +Y
// |
// |
// |
// --------------------------
//| |
//| --- | ----------> +X
//|__________________________|
// /
// /
// /
// V +Z
// we will ignore the z component
// Calculate the vector from the coordinattes of 2 frames for each hand to get their movement direction
// from there, 4 cases : (gands 1 is the lower hand and 2 the upper)
// Hands 1 goes down and hands 2 go up -> zoom in
// Hands 1 goes up and hands 2 go down -> zoom out
// Hands 1 and 2 go up or down -> ignore
// Zooming will increase or decrease a scaling factor
var hand1 = frame.hands[0].stabilizedPalmPosition[1] < frame.hands[1].stabilizedPalmPosition[1] ? frame.hands[0] : frame.hands[1];
var hand2 = frame.hands[0].stabilizedPalmPosition[1] > frame.hands[1].stabilizedPalmPosition[1] ? frame.hands[0] : frame.hands[1];
if (window.lasthand1 == undefined || window.lasthand2 == undefined) {
console.log(hand1);
window.lasthand1 = hand1;
console.log("wtf " + window.lasthand1);
window.lasthand2 = hand2;
}
else {
var hand1_vec = [hand1.stabilizedPalmPosition[0] - window.lasthand1.stabilizedPalmPosition[0], hand1.stabilizedPalmPosition[1] - window.lasthand1.stabilizedPalmPosition[1]];
var hand2_vec = [hand2.stabilizedPalmPosition[0] - window.lasthand2.stabilizedPalmPosition[0], hand2.stabilizedPalmPosition[1] - window.lasthand2.stabilizedPalmPosition[1]];
if (Math.sqrt(Math.pow(hand1_vec[0], 2) + Math.pow(hand1_vec[1], 2)) < 10 && Math.sqrt(Math.pow(hand2_vec[0], 2) + Math.pow(hand2_vec[1], 2)) < 10)
return;
console.log("norme : " + Math.sqrt(Math.pow(hand1_vec[0], 2) + Math.pow(hand1_vec[1], 2)))
var direct_hand1 = hand1_vec[0] / Math.sqrt(Math.pow(hand1_vec[0], 2) + Math.pow(hand1_vec[1], 2));
var direct_hand2 = hand2_vec[0] / Math.sqrt(Math.pow(hand2_vec[0], 2) + Math.pow(hand2_vec[1], 2));
if (direct_hand1 * (180 / Math.PI) >= 0 && direct_hand1 * (180 / Math.PI) <= 180) {
// console.log("hand1 up");
var hand1_txt_dir = "up";
}
else {
// console.log("hand1 down");
var hand1_txt_dir = "down";
}
if (direct_hand1 * (180 / Math.PI) >= 0 && direct_hand1 * (180 / Math.PI) <= 180) {
// console.log("hand2 down");
var hand2_txt_dir = "down"
}
else {
// console.log("hand2 up");
var hand2_txt_dir = "up";
}
if (hand1_txt_dir == "down" && hand2_txt_dir == "up") {
window.zoom += 0.1;
setZoom();
$("#container").trigger("mousewheel");
console.log("zooming");
}
else if (hand1_txt_dir == "up" && hand2_txt_dir == "down") {
window.zoom -= 0.1;
$("#container").trigger("mousewheel");
setZoom();
console.log("dezooming");
}
else {
console.log("wtf are you doing ?!?!?!");
}
window.lasthand1 = hand1;
window.lasthand2 = hand2;
}
}
window.lastframe = frame;
}
var controller = new Leap.Controller({enableGestures: true});
controller.loop(handleFrame);
Highcharts.setOptions({
lang: {
resetZoom: ''
}
});
function createData() {
var arr = [];
for (var i = 0; i < 200; i++) {
arr.push(Math.random() * 100);
}
return arr;
}
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
animation: false
},
xAxis: {
min: 50,
max: 150
},
yAxis: {
endOnTick: false,
startOnTick: false
},
series: [{
data: createData()
}]
});
var setZoom = function () {
var xMin = chart.xAxis[0].getExtremes().dataMin;
var xMax = chart.xAxis[0].getExtremes().dataMax;
var yMin = chart.yAxis[0].getExtremes().dataMin;
var yMax = chart.yAxis[0].getExtremes().dataMax;
chart.xAxis[0].setExtremes(xMin + (1 - window.zoom) * xMax, xMax * window.zoom);
chart.yAxis[0].setExtremes(yMin + (1 - window.zoom) * yMax, yMax * window.zoom);
};
$('#resetZoom').click(function () {
var xExtremes = chart.xAxis[0].getExtremes;
var yExtremes = chart.yAxis[0].getExtremes;
chart.xAxis[0].setExtremes(xExtremes.dataMin, xExtremes.dataMax);
chart.yAxis[0].setExtremes(yExtremes.dataMin, yExtremes.dataMax);
window.zoom = 1;
});
</script>
</body>
</html>