Created
June 17, 2016 22:19
-
-
Save syuhei176/470fb03fc4c2ff21b0077e9c13558e3c to your computer and use it in GitHub Desktop.
leap motion screen touch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="leap">Leap Motion was not connected</div> | |
<p>Touched: <span id="touched">…</span></p> | |
<p>Position: <span id="position">…</span></p> | |
<script src="//js.leapmotion.com/leap-0.6.3.min.js"></script> | |
<script> | |
var leapConnectedDom = document.getElementById("leap"); | |
var touched = document.getElementById("touched"); | |
var position = document.getElementById("position"); | |
var div = document.createElement('div'); | |
div.style["background-color"] = "#000"; | |
div.style["position"] = "absolute"; | |
div.style["top"] = 0; | |
div.style["left"] = 0; | |
div.style["width"] = "60px"; | |
div.style["height"] = "60px"; | |
document.documentElement.appendChild(div); | |
var controller = new Leap.Controller(); | |
controller.on('frame', function(frame){ | |
if(frame.pointables.length > 0) { | |
var pointable = frame.pointables[0]; | |
var touchDistance = frame.pointables[0].touchDistance; | |
var interactionBox = frame.interactionBox; | |
var normalizedPosition = interactionBox.normalizePoint(pointable.tipPosition, true); | |
var tipPosition = pointable.tipPosition; | |
var x = normalizedPosition[0]*screen.width; | |
var y = screen.height-normalizedPosition[1]*screen.height; | |
div.style["left"] = x; | |
div.style["top"] = y; | |
position.textContent = "("+x+","+y+")"; | |
if(touchDistance <= 0) { | |
touched.textContent = "Clicked"; | |
div.style["background-color"] = "#28f"; | |
}else{ | |
touched.textContent = ""; | |
div.style["background-color"] = "#111"; | |
} | |
} | |
}); | |
controller.on('connect', function(){ | |
console.log('connected'); | |
}); | |
controller.on('deviceStreaming', function(){ | |
console.log('deviceStreaming'); | |
leapConnectedDom.textContent = "Leap Motion was connected"; | |
}); | |
controller.on('deviceStopped', function(){ | |
console.log('deviceStopped'); | |
leapConnectedDom.textContent = "Leap Motion was not connected"; | |
}); | |
controller.connect(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment