-
Notifications
You must be signed in to change notification settings - Fork 0
/
leap-motion.js
467 lines (408 loc) · 11.3 KB
/
leap-motion.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
459
460
461
462
463
464
465
466
467
/* global Polymer, Leap */
'use strict';
/**
* An element providing a wrapper for Leap Motion.
*
* Example:
*
* <leap-motion
* auto
* enable-gestures
* last-frame="{{frame}}">
* </leap-motion>
*
* @group LeapMotion Elements
* @element leap-motion
* @demo demo/index.html
* @hero hero.svg
*/
Polymer({
is: 'leap-motion',
/**
* The `blur` event is fired when the browser page loses focus.
*
* @event blur
*/
/**
* The `connect` event is fired when this controller establishes a connection to the Leap Motion WebSocket server.
*
* @event connect
*/
/**
* The `device-attached` event is fired when the Leap Motion device is plugged in or turned on.
*
* @event device-attached
*/
/**
* The `device-connected` event is fired when the Leap Motion device is plugged in and
* when tracking is unpaused by the user.
*
* @event device-connected
*/
/**
* The `device-disconnected` event is fired when when the Leap Motion device is unplugged and
* when tracking is paused by the user.
*
* @event device-disconnected
*/
/**
* The `device-removed` event is fired when the Leap Motion device is unplugged or turned off.
*
* @event device-removed
*/
/**
* The `device-stopped` event is fired when the Leap Motion device stops providing data.
*
* @event device-stopped
*/
/**
* The `device-streaming` event is fired when the Leap Motion device starts providing data.
*
* @event device-streaming
*/
/**
* The `disconnect` event is fired when this Controller object disconnects
* from the Leap Motion WebSocket server.
*
* @event disconnect
*/
/**
* The `focus` event is fired when the browser tab gains focus.
*
* @event focus
*/
/**
* The `frame` event is fired on each loop.
*
* @event frame
* @detail {frame}
*/
/**
* The `frameEnd` event is fired when the frame of data has been fully constructed.
*
* @event frameEnd
* @detail {timestamp}
*/
/**
* The `gesture` event is fired for each gesture update in a frame.
*
* @event gesture
* @detail {gesture}
*/
/**
* The `protocol` event is fired when the WebSocket server and
* client have negotiated a protocol.
*
* @event protocol
* @detail {protocol}
*/
/**
* The `streaming-started` event is fired when the Leap Motion service/daemon starts providing data.
*
* @event streaming-started
*/
/**
* The `streaming-stopped` event is fired when the Leap Motion service/daemon stops providing data.
*
* @event streaming-stopped
*/
properties: {
/**
* The host name or IP address of the WebSocket server providing Leap Motion tracking data.
*/
host: {
type: String,
value: '127.0.0.1'
},
/**
* The port on which the WebSocket server is listening.
*/
port: {
type: Number,
value: 6437
},
/**
* Set to true to enable gesture recognition.
* Omit or set to false if your application does not use gestures.
*/
enableGestures: {
type: Boolean,
value: false
},
/**
* Set to true to enable this application to receive frames when not the foreground application.
*/
background: {
type: Boolean,
value: false
},
/**
* Set to true to when mounting the Leap Motion hardware to a head-mounted display.
*/
optimizeHmd: {
type: Boolean,
value: false
},
/**
* The type of update loop to use for processing frame data.
* * animationFrame uses the browser animation loop (generally 60 fps).
* * deviceFrame runs at the Leap Motion controller frame rate (20 to 200 fps depending on the user’s settings and available computing power).
*/
frameEventName: {
type: String,
value: 'animationFrame'
},
/**
* This tells the controller to use all plugins included on the page.
* For more information, see https://github.com/leapmotion/leapjs/wiki/plugins.
*/
useAllPlugins: {
type: Boolean,
value: false
},
/**
* Defaults to true, which means the animation frame loop runs at all times.
* If you set this to false, the animation loop does only runs when
* the Controller() object is connected to the Leap Motion service and
* only when a new frame of data is available.
* Setting loopWhileDisconnected to false can minimize your app’s use of computing resources,
* but may irregularly slow down or stop any animations driven by the frame loop.
*/
loopWhileDisconnected: {
type: Boolean,
value: true
},
/**
* If true, automatically performs a Leap Motion Controller when either `host` or
* `port` or controller opction change.
*/
auto: {
type: Boolean,
value: false
},
/**
* Reports whether this Controller is currently connected to the Leap Motion service.
*/
connected: {
type: Boolean,
value: false,
notify: true
},
/**
* Reports whether this Controller is currently recieving tracking data from the Leap Motion service.
*/
streaming: {
type: Boolean,
value: false,
notify: true
},
/**
* Last device info received by device events
*/
lastDeviceInfo: {
type: Object,
value: function () {
return {};
},
notify: true
},
/**
* Last frame received.
*/
lastFrame: {
type: Object,
value: function () {
return {};
},
notify: true
},
/**
* Last frame end time from connection.
*/
lastFrameEnd: {
type: Number,
value: 0,
notify: true
},
/**
* Last gesture detected.
*/
lastGesture: {
type: Object,
value: function () {
return {};
},
notify: true
},
isDeviceAttached: {
type: Boolean,
value: false,
readonly: true,
notify: true
},
isDeviceConnected: {
type: Boolean,
value: false,
readonly: true,
notify: true
},
isDeviceStreaming: {
type: Boolean,
value: false,
readonly: true,
notify: true
},
isFocus: {
type: Boolean,
value: false,
readonly: true,
notify: true
}
},
observers: [
'_configOptionsChanged(host, port, enableGestures, background, ' +
'optimizeHmd, frameEventName, useAllPlugins, loopWhileDisconnected, auto)',
'_backgroundChange(background)',
'_optimizeHmdChange(optimizeHmd)'
],
generateController: function () {
this.controller = new Leap.Controller({
host: this.host,
port: this.port,
enableGestures: this.enableGestures,
background: this.background,
optimizeHMD: this.optimizeHMD,
frameEventName: this.frameEventName,
useAllPlugins: this.useAllPlugins,
loopWhileDisconnected: this.loopWhileDisconnected
});
return this.controller;
},
/**
* Connects this Controller object to the Leap Motion WebSocket server.
*/
connect: function(){
this.controller.connect();
},
/**
* Disconnects from the WebSocket server.
*/
disconnect: function(){
this.controller.disconnect();
},
_bindEvents: function () {
if (this.controller) {
this.controller.on('blur', this._onBlur.bind(this));
this.controller.on('connect', this._onConnect.bind(this));
this.controller.on('deviceAttached', this._onDeviceAttached.bind(this));
this.controller.on('deviceConnected', this._onDeviceConnected.bind(this));
this.controller.on('deviceDisconnected', this._onDeviceDisconnected.bind(this));
this.controller.on('deviceRemoved', this._onDeviceRemoved.bind(this));
this.controller.on('deviceStopped', this._onDeviceStopped.bind(this));
this.controller.on('deviceStreaming', this._onDeviceStreaming.bind(this));
this.controller.on('disconnect', this._onDisconnect.bind(this));
this.controller.on('focus', this._onFocus.bind(this));
this.controller.on('frame', this._onFrame.bind(this));
this.controller.on('frameEnd', this._onFrameEnd.bind(this));
this.controller.on('gesture', this._onGesture.bind(this));
this.controller.on('protocol', this._onProtocol.bind(this));
this.controller.on('streamingStarted', this._onStreamingStarted.bind(this));
this.controller.on('streamingStopped', this._onStreamingStopped.bind(this));
}
},
_onBlur: function (frame) {
this.isFocus = false;
this.fire('blur');
},
_onConnect: function () {
this.connected = this.controller.connected();
this.fire('connect');
},
_onDeviceAttached: function (deviceInfo) {
this.lastDeviceInfo = deviceInfo;
this.isDeviceAttached = true;
this.fire('device-attached', deviceInfo);
},
_onDeviceConnected: function (deviceInfo) {
this.lastDeviceInfo = deviceInfo;
this.isDeviceConnected = true;
this.fire('device-connected', deviceInfo);
},
_onDeviceDisconnected: function (deviceInfo) {
this.lastDeviceInfo = deviceInfo;
this.isDeviceConnected = false;
this.fire('device-disconnected', deviceInfo);
},
_onDeviceRemoved: function (deviceInfo) {
this.lastDeviceInfo = deviceInfo;
this.isDeviceAttached = false;
this.fire('device-removed', deviceInfo);
},
_onDeviceStopped: function (deviceInfo) {
this.lastDeviceInfo = deviceInfo;
this.isDeviceStreaming = false;
this.fire('device-stopped', deviceInfo);
},
_onDeviceStreaming: function (deviceInfo) {
this.lastDeviceInfo = deviceInfo;
this.isDeviceStreaming = true;
this.fire('device-streaming', deviceInfo);
},
_onDisconnect: function () {
this.connected = this.controller.connected();
this.fire('disconnect');
},
_onFocus: function () {
this.isFocus = true;
this.fire('focus');
},
_onFrame: function (frame) {
this.lastFrame = frame;
this.fire('frame', frame);
},
_onFrameEnd: function (frameEnd) {
this.lastFrameEnd = frameEnd;
this.fire('frameEnd', frameEnd);
},
_onGesture: function (gesture, frame) {
this.lastGesture = gesture;
this.fire('gesture', {
gesture: gesture,
frame: frame
});
},
_onProtocol: function (protocol, frame) {
this.lastProtocol = protocol;
this.fire('protocol', {
protocol: protocol,
frame: frame
});
},
_onStreamingStarted: function (deviceInfo) {
this.streaming = this.controller.streaming();
this.lastDeviceInfo = deviceInfo;
this.fire('streaming-started', deviceInfo);
},
_onStreamingStopped: function (deviceInfo) {
this.streaming = this.controller.streaming();
this.lastDeviceInfo = deviceInfo;
this.fire('streaming-stopped', deviceInfo);
},
_configOptionsChanged: function () {
if (this.auto) {
this.generateController();
this._bindEvents();
this.connect();
}
},
_backgroundChange: function () {
if (this.controller) {
this.controller.setBackground(this.background);
}
},
_optimizeHmdChange: function () {
if (this.controller) {
this.controller.setOptimizeHMD(this.optimizeHmd);
}
}
});