Skip to content

Instantly share code, notes, and snippets.

@polidog
Last active December 20, 2015 22:19
Show Gist options
  • Save polidog/6204147 to your computer and use it in GitHub Desktop.
Save polidog/6204147 to your computer and use it in GitHub Desktop.
leapMotionでスワイプがleftかrightかupかdownかを判定するためのjQueryプラグイン
<!DOCTYPE html>
<html lang="ja">
<head>
<title>example</title>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
</head>
<body>
<div id="name">
</div>
<script src="//js.leapmotion.com/0.2.0/leap.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="./jquery.leapSwipeHelper.js"></script>
<script>
Leap.loop({enableGestures: true}, function(frame){
$(frame.gestures).leapSwipeHelper(function(event){
$("#name").html("").html("<div class='alert'>"+event.type+"</div>");
});
});
</script>
</body>
</html>
(function($) {
var getGestureType = function(gesture, distanse) {
// horizon
var ret = horizonCheck(gesture, distanse, ["right","left"]);
if (ret !== false) return ret;
return verticalCheck(gesture, distanse, ["up","down"]);
}
var horizonCheck = function(gesture,distanse,names) {
var _distanse = Math.abs(gesture.position[0] - gesture.startPosition[0]);
if (_distanse < distanse) return false;
if (gesture.position[0] > 0) {
return names[0];
} else {
return names[1];
}
}
var verticalCheck = function(gesture,distanse,names) {
var _distanse = Math.abs(gesture.position[1] - gesture.startPosition[1]);
if (_distanse < distanse) return false;
if (gesture.startPosition[1] < gesture.position[1]) {
return names[0];
} else {
return names[1];
}
}
$.fn.leapSwipeHelper = function(callback,options) {
options = $.extend({
distance: 50
});
var type = false;
this.each(function(){
if (this.state == "stop" && this.type == "swipe") {
console.log(this.state);
type = getGestureType(this,options.distance);
}
});
if (typeof callback === "function" && type !== false) {
$.proxy(callback,this,{type:type})();
}
return type;
}
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment