-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.html
279 lines (220 loc) · 8.48 KB
/
index.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script src="//js.leapmotion.com/leap-0.6.0.min.js"></script>
<script src="//js.leapmotion.com/leap-plugins-0.1.6.js"></script>
<script src="//js.leapmotion.com/leap.rigged-hand-0.1.4.min.js"></script>
<script>
var map;
var leftHandPrev;
var separationStart;
var MAX_ZOOM = 22;
var SEPARATION_SCALING = 1.25;
var LEFT_HAND = 0, RIGHT_HAND = 1;
var X = 0, Y = 1, Z = 2;
function move(frame) {
// Look for any circle gestures and process the zoom
// TODO: filter out multiple circle gestures per frame
if(frame.valid && frame.gestures.length > 0){
frame.gestures.forEach(function(gesture){
filterGesture("circle", zoom)(frame, gesture);
});
}
markHands(frame);
// if there is one hand grabbing...
if(frame.hands.length > 0 && isGripped(frame.hands[LEFT_HAND])) {
var leftHand = frame.hands[LEFT_HAND];
var rightHand = frame.hands.length > 1 ? frame.hands[RIGHT_HAND] : undefined;
var separation;
// If there was no previous closed position, capture it and exit
if(leftHandPrev == null) {
leftHandPrev = leftHand;
return;
}
// if there is a right hand and its gripped...
if(rightHand) {
if(isGripped(rightHand)) {
separation = Math.sqrt(
Math.pow(rightHand.stabilizedPalmPosition[X] - leftHand.stabilizedPalmPosition[X], 2) +
Math.pow(rightHand.stabilizedPalmPosition[Y] - leftHand.stabilizedPalmPosition[Y], 2)
);
// console.log("separation = " + separation + " ("+separationStart+")");
// ...and no previous separation, capture and exit
if(separationStart == null) {
separationStart = separation;
return;
}
// Calculate if we need to change the zoom level
var currentZoom = map.getZoom();
if(currentZoom > 1 && separation < (separationStart / SEPARATION_SCALING) ) {
map.setZoom( currentZoom - 1 );
separationStart = separation;
} else if( currentZoom < MAX_ZOOM && separation > (SEPARATION_SCALING * separationStart) ) {
map.setZoom( currentZoom + 1 );
separationStart = separation;
}
// If the right hand is not gripped...
} else if(separationStart != null) {
separationStart = null;
}
}
// Calculate how much the hand moved
var dX = leftHandPrev.stabilizedPalmPosition[X] - leftHand.stabilizedPalmPosition[X];
var dY = leftHandPrev.stabilizedPalmPosition[Y] - leftHand.stabilizedPalmPosition[Y];
// console.log("Movement: " + dX + ","+dY);
var center = map.getCenter();
var scaling = 4.0 / Math.pow(2, map.getZoom()-1);
var newLat = center.lat() + dY * scaling;
var newLng = center.lng() + dX * scaling;
var newCenter = new google.maps.LatLng(newLat, newLng);
// console.log(newCenter)
map.setCenter(newCenter);
leftHandPrev = leftHand;
} else {
// If the left hand is not in a grab position, clear the last hand position
if(frame.hands.length > LEFT_HAND && !isGripped(frame.hands[LEFT_HAND]) && leftHandPrev != null) {
leftHandPrev = null;
}
// if the right hand is not in a grab position, clear the separation
if(frame.hands.length > RIGHT_HAND && !isGripped(frame.hands[RIGHT_HAND]) && separationStart != null) {
separationStart = null;
}
// console.log("Clearing lastHand");
}
}
var handMarkers = [];
var HEIGHT_OFFSET = 150;
var BASE_MARKER_SIZE_GRIPPED = 350000, BASE_MARKER_SIZE_UNGRIPPED = 500000;
function markHands(frame) {
var scaling = (4.0 / Math.pow(2, map.getZoom()-1));
var bounds = map.getBounds();
// FIXME: Sometimes this gets run too early, just exit if its too early.
if(!bounds) { return; }
var origin = new google.maps.LatLng(bounds.getSouthWest().lat(), bounds.getCenter().lng());
var hands = frame.hands;
for(var i in hands) {
if(hands.hasOwnProperty(i)) {
// Limit this to 2 hands for now
if(i > RIGHT_HAND) {
return;
}
var hand = hands[i];
newCenter = new google.maps.LatLng(origin.lat() + ((hand.stabilizedPalmPosition[1] - HEIGHT_OFFSET) * scaling), origin.lng() + (hand.stabilizedPalmPosition[0] * scaling));
// console.log(center.lat() + "," + center.lng());
// console.log(newCenter.lat() + "," + newCenter.lng());
var gripped = isGripped(hand);
var baseRadius = gripped ? BASE_MARKER_SIZE_GRIPPED : BASE_MARKER_SIZE_UNGRIPPED;
var handColor = getHandColor(hand);
var handMarker = handMarkers[i];
if(!handMarker) {
handMarker = new google.maps.Circle();
handMarkers[i] = handMarker;
}
handMarker.setOptions({
strokeColor: handColor,
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: handColor,
fillOpacity: 0.35,
map: map,
center: newCenter,
radius: baseRadius * scaling
});
}
}
}
var zoomLevelAtCircleStart;
var INDEX_FINGER = 1;
function zoom(frame, circleGesture) {
// Only zoom based on one index finger
if(circleGesture.pointableIds.length == 1 &&
frame.pointable(circleGesture.pointableIds[0]).type == INDEX_FINGER) {
switch(circleGesture.state) {
case "start":
zoomLevelAtCircleStart = map.getZoom();
// fall through on purpose...
case "update":
// figure out if we need to change the zoom level;
var zoomChange = Math.floor(circleGesture.progress);
var currentZoom = map.getZoom();
var zoomDirection = isClockwise(frame, circleGesture) ? zoomChange : -zoomChange;
if(zoomLevelAtCircleStart + zoomDirection != currentZoom) {
var newZoom = zoomLevelAtCircleStart + zoomDirection;
if(newZoom >= 0 && newZoom <= MAX_ZOOM) {
map.setZoom(newZoom);
}
}
break;
case "stop":
zoomLevelAtCircleStart = null;
break;
}
}
}
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControlOptions: {
position: google.maps.ControlPosition.TOP_LEFT
}
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// listen to Leap Motion
Leap.loop({enableGestures: true}, move);
}
// ==== utility functions =====
/** Returns the truth that a Leap Motion API Hand object is currently in a gripped or "grabbed" state.
*/
function isGripped(hand) {
return hand.grabStrength == 1.0;
}
function getHandColor(hand) {
if(isGripped(hand)) {
return "rgb(0,119,0)";
} else {
var tint = Math.round((1.0 - hand.grabStrength) * 119);
tint = "rgb(119," + tint + "," + tint + ")";
return tint;
}
}
function filterGesture(gestureType, callback) {
return function(frame, gesture) {
if(gesture.type == gestureType) {
callback(frame, gesture);
}
}
}
function isClockwise(frame, gesture) {
var clockwise = false;
var pointableID = gesture.pointableIds[0];
var direction = frame.pointable(pointableID).direction;
var dotProduct = Leap.vec3.dot(direction, gesture.normal);
if (dotProduct > 0) clockwise = true;
return clockwise;
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<a href="https://github.com/jaxzin/leap-map"><img
id="fork-ribbon"
style="position: absolute; top: 0; right: 0; border: 0; z-index: 1000;"
src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"
alt="Fork me on GitHub"/></a>
<div id="map-canvas"></div>
</body>
</html>