Skip to content

Instantly share code, notes, and snippets.

@barnabee
Created September 16, 2013 16:57
Show Gist options
  • Save barnabee/6583375 to your computer and use it in GitHub Desktop.
Save barnabee/6583375 to your computer and use it in GitHub Desktop.
Test simple Leap Motion gesture recognition with node.js
var _ = require('underscore')
var Leap = require('leapjs')
/*** Decompose X, Y, Z vector to simple direction */
var getDirection = function(vector) {
// Only do this if vector is defined and has length 3
if (vector && vector.length == 3) {
// Find out which axis has largest absolute value (e.g. major direction component)
var absVector = _.map(vector, Math.abs);
var axis = absVector.indexOf(Math.max.apply(null, absVector));
// Return a word based on the axis and the sign of the value in that axis
return [['left','right'],['down', 'up'],['in','out']][axis][vector[axis] > 0 ? 1 : 0]
}
};
var controller = new Leap.Controller({enableGestures: true});
// Handle a frame and print direction and state of a swipe gesture
controller.on('frame', function (f) {
if(f.gestures.length > 0) {
if(f.gestures[0].type == 'swipe') {
console.log('swipe: ' + getDirection(f.gestures[0].direction) + ' ' + f.gestures[0].state);
}
}
});
// Go!
controller.connect();
//EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment