-
Notifications
You must be signed in to change notification settings - Fork 0
/
boxes.html
195 lines (158 loc) · 4.86 KB
/
boxes.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!DOCTYPE html>
<!-- tested in chrome and safari -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Leap!</title>
<script src="leap.js"></script>
<script src="jquery-1.9.1.min.js"></script>
<style>
html {
height:100%;
}
body {
height:100%;
}
.box {
width:100px;
height:100px;
opacity:0.2;
position:absolute;
top:35%;
-webkit-transition-duration:.25s;
-webkit-transition-property:-webkit-transform;
}
.box.scaled {
-webkit-transform: scale(1.3);
}
.b1 {
background-color:#8b008b;
left:15%;
}
.b2 {
background-color:#008b8b;
left:45%;
}
.b3 {
background-color:#ff8c00;
left:75%;
}
</style>
<script>
// Store frame for motion functions
var paused = false;
var maxHandX = 150.0;
var maxHandY = 230.0;
var minHandY = 60.0;
var inKeyTap = false;
var inCircle = false;
var boxModel = {boxes:[], screen:{}, currentHit: null};
// Setup Leap loop with frame callback function
var controllerOptions = {enableGestures: true};
Leap.loop(controllerOptions, function(frame) {
if (paused) return;
if (frame.gestures && frame.gestures.length > 0) {
handleGesture(frame.gestures[0]);
}
$('.box').css('opacity', '.2');
boxModel.currentHit = null;
if (frame.hands.length > 0) {
var hand = frame.hands[0];
var handX = hand.palmPosition[0];
var handY = hand.palmPosition[1];
var $activeBox = detectHit(handX);
if ($activeBox) {
$activeBox.css('opacity', '.8');
}
boxModel.currentHit = $activeBox;
}
});
function pause(){
paused = true;
}
function waitForTransitionDuration($elem, callback) {
var transitionDuration = parseFloat($elem.css('-webkit-transition-duration')) * 1000;
transitionDuration += 50;
window.setTimeout(function() {
callback();
}, transitionDuration);
}
function handleKeyTap(gesture) {
if (inKeyTap) return;
inKeyTap = true;
window.setTimeout(function() {
inKeyTap = false;
}, 1000);
var $currentHit = boxModel.currentHit; //in case we move off before the event fires
waitForTransitionDuration($currentHit, function() {
$currentHit.removeClass('scaled');
});
$currentHit.addClass('scaled');
}
// function handleCircle(gesture) {
// console.log('handling circle', gesture);
// }
function handleGesture(gesture) {
switch(gesture.type)
{
case "keyTap":
if (!boxModel.currentHit) return;
handleKeyTap(gesture);
break;
// case "circle":
// handleCircle(gesture);
// break;
default:
//console.log('detected unhandled gesture of type', gesture.type, gesture);
}
}
function translateCoordinateX(x) {
return x * boxModel.screen.coordScaleX;
}
// function translateCoordinateY(y) {
// return (maxHandY - y) * boxModel.screen.coordScaleY; // - (y * boxModel.screen.coordScaleY);
// }
function detectHit(handOffsetX) {
var origHandOffsetX = handOffsetX;
handOffsetX = translateCoordinateX(handOffsetX);
// for example 0
var screenPosX = boxModel.screen.midX + handOffsetX;
for (var i= 0, l=boxModel.boxes.length; i<l; i++) {
var box = boxModel.boxes[i];
if ((screenPosX > (box.x - box.width)) && (screenPosX < (box.x + box.width*2))) {
return $('.b' + (i+1));
}
}
return null;
}
function readBoxModel($box) {
var bModel = {};
var pos = $box.position();
bModel.x = pos.left;
bModel.y = pos.top;
bModel.midX = pos.left + $box.width()/2.0;
bModel.midY = pos.top + $box.height()/2.0;
bModel.width = $box.width();
bModel.height = $box.height();
boxModel.boxes.push(bModel);
}
$(document).ready(function() {
readBoxModel($('.b1'));
readBoxModel($('.b2'));
readBoxModel($('.b3'));
boxModel.screen.midX = $('body').width()/2.0;
boxModel.screen.midY = $('body').height()/2.0;
boxModel.screen.width = $('body').width();
boxModel.screen.height = $('body').height();
boxModel.screen.coordScaleX = boxModel.screen.width/maxHandX;
boxModel.screen.coordScaleY = (boxModel.screen.height/(maxHandY));
});
</script>
</head>
<body>
<button class="pause" onclick="pause()">Pause</button>
<div class="box b1"></div>
<div class="box b2"></div>
<div class="box b3"></div>
</body>
</html>