Чтобы узнать об интеграции Leap с веб-приложением, я декомпилировал New York Times Reader для Leap Motion.
Что касается его реализации (построенный.js), может помочь следующий код. (на основе Backbone.js)
var LeapController = новый Leap.Controller({enableGestures: true});
window.L = LeapController;
LeapController.on('deviceConnected', function () {
console.log('deviceConnected', arguments);
// in the example code, this fires when the app starts.
// in our app, it only fires when the device is reconnected after having been connected when the app was started.
dispatch.trigger('LeapControl:reconnect');
});
LeapController.on('ready', function () {
// this fires when the app is ready.
dispatch.trigger('LeapControl:reconnect');
});
LeapController.on('connect', function () {
console.log('device is connected');
// this fires when no device is plugged in. wtf.
});
LeapController.connection.on('deviceConnect', function () {
console.log('deviceConnect');
// this fires when the device is changes state from plugged in to ungplugged and vice versa.
});
LeapController.on('deviceDisconnected', function () {
console.log('deviceDisconnected', arguments);
dispatch.trigger('LeapControl:disconnect');
});
Очевидно, разработчики NYTimes Reader уже обнаружили, что было бы непросто определить, был ли Leap Controller уже подключен до загрузки приложения. ("Втф", ха-ха....)
И часть кода, определяющая поведение события LeapControl:disconnect/reconnect, понятно:
newNews.views.Disconnected = Backbone.View.extend({
el: $('#disconnection-box'),
initialize: function () {
_.bindAll(this);
},
open: function () {
this.listenTo(dispatch, 'LeapControl:disconnect', this.show);
this.listenTo(dispatch, 'LeapControl:reconnect', this.hide);
return this;
}, ........
Таким образом, при запуске LeapControl:reconnect всплывающее окно с сообщением о том, что «контроллер движения прыжка не обнаружен», скрывалось.
При отладке, когда скачкообразное движение уже было подключено до запуска приложения, события будут запускаться в следующем порядке, что обеспечивает правильное обнаружение:
- LeapController.on('connect', function () { console.log('устройство подключено'); });
- LeapController.on('готово', function () { dispatch.trigger('LeapControl:reconnect'); });
Одновременно, если не было подключено заранее, будет запущено это и только это:
- LeapController.on('connect', function () { console.log('устройство подключено'); });
В заключение, мы можем использовать событие «готово» для работы с этим сценарием. ХТН
person
LuoboTixS
schedule
28.09.2013