This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeapCubeExperiment.html
108 lines (94 loc) · 3.23 KB
/
LeapCubeExperiment.html
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
<!DOCTYPE html>
<html>
<head>
<title>Leap</title>
<style>canvas { width: 50%; height: 100% }</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
var ws;
// Support both the WebSocket and MozWebSocket objects
if ((typeof(WebSocket) == 'undefined') &&
(typeof(MozWebSocket) != 'undefined')) {
WebSocket = MozWebSocket;
}
// Create the socket with event handlers
function initLeapMotion() {
//Create and open the socket
ws = new WebSocket("ws://localhost:6437/");
var doc = document;
// On successful connection
ws.onopen = function(event) {
doc.getElementById("main").style.visibility = "visible";
doc.getElementById("connection").innerHTML = "WebSocket connection open!";
};
// On message received
ws.onmessage = function(event) {
var leap = JSON.parse(event.data);
setVelocity(leap);
var str = JSON.stringify(leap, undefined, 2);
doc.getElementById("output").innerHTML = '<pre>' + str + '</pre>';
};
// On socket close
ws.onclose = function(event) {
ws = null;
doc.getElementById("main").style.visibility = "hidden";
doc.getElementById("connection").innerHTML = "WebSocket connection closed";
}
//On socket error
ws.onerror = function(event) {
alert("Received error");
};
}
</script>
</head>
<body onload="initLeapMotion();">
<script src="https://raw.github.com/mrdoob/three.js/master/build/three.js"></script>
<script>
var velocity = [0,1,0];
function setVelocity(leap){
if (leap !== undefined){
if (leap.hands !== undefined && leap.hands.length >= 1){
if (leap.hands[0].fingers !== undefined && leap.hands[0].fingers.length >= 1){
velocity[0] = leap.hands[0].fingers[0].tip.velocity[0];
velocity[1] = leap.hands[0].fingers[0].tip.velocity[1];
velocity[2] = leap.hands[0].fingers[0].tip.velocity[2];
}
}
}
}
var pause = false;
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
var clock = new THREE.Clock();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
renderer.setClearColorHex(0xEEEEEE, 1.0);
renderer.clear();
var geometry = new THREE.CubeGeometry(1,1,1);
var material = new THREE.MeshBasicMaterial({color: 0x1f1ff1});
var cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;
function render() {
requestAnimationFrame(render);
if (!pause){
var delta = clock.getDelta();
console.log (velocity);
cube.rotation.x += delta * velocity[0] * .001;
cube.rotation.y += delta * velocity[1] * .001;
cube.rotation.y += delta * velocity[2] * .001;
renderer.render(scene, camera);
}
}
render();
</script>
<button onclick="pause = !pause;">Pause </button>
<h1>Leap JavaScript/WebSocket Sample</h1>
<div id="connection">WebSocket not connected</div>
<div id="main" style="visibility:hidden">
<p>JSON Frame data:</p>
<div id="output"></div>
</div>
</body>
</html>