-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
257 lines (203 loc) · 6.44 KB
/
index.js
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
var GoogleMaps = require('google-maps')
, Noise = require('noisejs').Noise
, dat = require('exdat')
, glMatrix = require('gl-matrix')
, raf = require('raf')
, KeyListner = require('key-listener');
var vec2 = glMatrix.vec2
, mat2 = mat2 = glMatrix.mat2
// config
GoogleMaps.KEY = 'AIzaSyCuKjnJWCoUMRLbVFNEkJoFVD0I73u_xJo';
var _noise = new Noise(Math.random());
var _container = document.createElement('div')
, _map
, _gui
, _interval;
_container.id = 'map-container';
var _keyHandler = new KeyListner();
var _params = {
maxMovementDist : 0.01
, maxMovementDeg: 10
, movementDelay: 100
// , flipped: false
, hideLogos: false
, wandering: true
, flyingCursor: true
, bigCursor: false
, cursorXOffset: 0
}
var _position = vec2.create();
var _movementVec = vec2.create();
var _scaledMovement = vec2.create();
var _cursorImg = new Image();
_cursorImg.src = "assets/cursor.png";
_cursorImg.id = "cursor";
// set initial movement vector
vec2.set(_movementVec, 1, 0);
// Normalizes the coords that tiles repeat across the x axis (horizontally)
// like the standard Google map tiles.
function getNormalizedCoord(coord, zoom) {
var y = coord.y;
var x = coord.x;
// tile range in one direction range is dependent on zoom level
// 0 = 1 tile, 1 = 2 tiles, 2 = 4 tiles, 3 = 8 tiles, etc
var tileRange = 1 << zoom;
// don't repeat across y-axis (vertically)
if (y < 0 || y >= tileRange) {
return null;
}
// repeat across x-axis
if (x < 0 || x >= tileRange) {
x = (x % tileRange + tileRange) % tileRange;
}
return {x: x, y: y};
}
GoogleMaps.load(function(google) {
// add container
document.body.appendChild(_container);
document.body.appendChild(_cursorImg);
// create moon map type
var moonMapType = new google.maps.ImageMapType({
getTileUrl: function(coord, zoom) {
var normalizedCoord = getNormalizedCoord(coord, zoom);
if (!normalizedCoord) {
return null;
}
var bound = Math.pow(2, zoom);
return '//mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw' +
'/' + zoom + '/' + normalizedCoord.x + '/' +
(bound - normalizedCoord.y - 1) + '.jpg';
},
tileSize: new google.maps.Size(256, 256),
maxZoom: 9,
minZoom: 0,
radius: 1738000,
name: 'Moon'
});
// create map
_map = new google.maps.Map(_container, {
center: {lat: 0, lng: 0}
, zoom: 9
, disableDefaultUI: true
, mapTypeControlOptions: {
mapTypeIds: ['moon']
}
});
_map.mapTypes.set('moon', moonMapType);
_map.setMapTypeId('moon');
// var maxMovementDist = 500;
var latLngRadius = .1;
function randomPan() {
var center = _map.getCenter();
console.log('current position', center.lat(), center.lng());
_map.panBy(randomRadius(center.lat(), center.lng()), randomRadius(center.lng(), center.lat()));
}
function randomCenter() {
var center = _map.getCenter();
_map.setCenter({
lat: randomLatLng(center.lat()),
lng: randomLatLng(center.lng())
});
}
function randomRadius(x, y) {
return (1 - (Math.random()*2)) * _params.maxMovementDist ;
// return _noise.simplex2(x, y, (new Date()).getTime()) * maxMovementDist ;
}
function randomLatLng(x) {
// return (1 - (Math.random()*2)) * maxMovementDist ;
return (_noise.simplex2(x, (new Date()).getTime()) * latLngRadius) + x;
}
// ok, here's what i think needs to happen. there needs to be a single vector
// for movement. every tick that movement vector is rotated slightly.
// then the map center position (LatLng) is transformed into a vector, and added to
// the movement vector. next the pos vector is transformed back into LatLng
// and then set as the new center
// also: use requestAnimationFrame for movement rather than setTimeout
function vectorMovement() {
// vec2.add(_position, _position, createRotatedVector(_position, deg2Rad(5)));
var rotationDeg = (Math.random()*(_params.maxMovementDeg*2)) - _params.maxMovementDeg;
rotateVec2(_movementVec, _movementVec, deg2Rad(rotationDeg));
vec2.scale(_scaledMovement, _movementVec, _params.maxMovementDist);
vec2.add(_position, _position, _scaledMovement);
if(_params.flyingCursor) {
var rotation = (Math.PI/2) - Math.atan2(_movementVec[0], _movementVec[1]);
setCssTransform(_cursorImg, "rotate("+rotation+"rad)");
_cursorImg.style['margin-left'] = _params.cursorXOffset + 'px';
}
// console.log('movement vec', vec2.len(_position), vec2.len(_scaledMovement), vec2.len(_movementVec));
_map.setCenter(vec2ToLatLng(_position));
}
function rotateVec2(out, v, rad) {
// console.log('createRotatedVector', v, rad);
var m = mat2.create()
, v2 = vec2.create();
// radians
mat2.rotate(m, m, rad);
vec2.transformMat2(out, v, m);
return out;
}
function deg2Rad(deg) {
return deg * Math.PI / 180;
}
function getRandomVector(min, max) {
var v = vec2.create();
vec2.set(
v
, min + (Math.random()*(max-min))
, min + (Math.random()*(max-min))
);
return v;
}
function vec2ToLatLng(v) {
return new google.maps.LatLng(v[0], v[1]);
}
function setCssTransform(el, val) {
var props = [
'webkitTransform'
, 'transform'
]
props.forEach(function(p){
el.style[p] = val;
});
return el;
}
_gui = new dat.GUI();
_gui.add(_params, 'maxMovementDist', 0, 1);
_gui.add(_params, 'maxMovementDeg', 0, 180);
_gui.add(_params, 'movementDelay', 0, 5000).onChange(function(v) {
stop();
start();
});
// _gui.add(_params, 'flipped').onChange(function(v) {
// _container.classList.toggle('flipped');
// });
_gui.add(_params, 'hideLogos').onChange(function(v) {
_container.classList.toggle('hideLogos');
});
_gui.add(_params, 'wandering').onChange(function(v) {
if(v) {
start();
}
});
_gui.add(_params, 'flyingCursor').onChange(function(v) {
_cursorImg.style.display = v ? 'block' : 'none';
});
_gui.add(_params, 'bigCursor').onChange(function(v) {
_cursorImg.src = 'assets/' + (v ? 'cursor2x.png' : 'cursor.png');
});
_gui.add(_params, 'cursorXOffset', -200, 200);
_gui.domElement.classList.toggle('hidden');
_keyHandler.addListener(document, 'g', function() {
// console.log('g key!', _gui.domElement);
_gui.domElement.classList.toggle('hidden');
});
function start() {
raf(function tick() {
vectorMovement();
if(_params.wandering) raf(tick);
});
}
if(_params.wandering) {
start();
}
});