forked from bloomtime/d3map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
568 lines (503 loc) · 24.2 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Map Tiles in D3</title>
<!-- An exercise in learning D3 for DOM manipulation and transitions.
Uses CSS (3D) transforms where available, falls back to normal CSS if not.
Coordinate and tile positioning logic cribbed from Modest Maps.
A two day hack... Doubtless still glitches to be ironed out!
No projections, no layers, no double click, no touching, no overlays.
Queueing img requests seems overkill but smooths things out a lot.
(C) 2011 Tom Carden, released under the same BSD license as D3 itself:
https://github.com/mbostock/d3/blob/master/LICENSE
Forks welcome! -->
<script src="http://mbostock.github.com/d3/d3.min.js"></script>
<script type="text/javascript">
// borrowed from Modest Maps, inspired by LeafletJS
var transformProperty = (function(props) {
var style = document.documentElement.style;
for (var i = 0; i < props.length; i++) {
if (props[i] in style) {
return props[i];
}
}
return false;
})(['transform', '-webkit-transform', '-o-transform', '-moz-transform', '-ms-transform']);
var matrixString = (function() {
if (('WebKitCSSMatrix' in window) && ('m11' in new WebKitCSSMatrix())) {
return function(scale,x,y,cx,cy) {
scale = scale || 1;
return 'translate3d(' + [ x, y, '0px' ].join('px,') + ') scale3d(' + [ scale,scale,1 ].join(',') + ')';
/* return 'matrix3d(' +
[ scale, '0,0,0,0',
scale, '0,0,0,0,1,0',
(x + ((cx * scale) - cx)).toFixed(4),
(y + ((cy * scale) - cy)).toFixed(4),
'0,1'].join(',') + ')'; */
}
} else {
return function(scale,x,y,cx,cy) {
var unit = (transformProperty == 'MozTransform') ? 'px' : '';
return 'matrix(' +
[(scale || '1'), 0, 0,
(scale || '1'),
(x + ((cx * scale) - cx)) + unit,
(y + ((cy * scale) - cy)) + unit
].join(',') + ')';
}
}
})();
// make a tile provider that knows how to wrap tiles around the world
function provider(tile) {
var c = {r: tile.r, c: tile.c, z: tile.z};
var minCol = 0,
maxCol = Math.pow(2,tile.z);
while (c.c < minCol) c.c += maxCol;
while (c.c >= maxCol) c.c -= maxCol;
var z = c.z, x = c.c, y = c.r;
return 'http://otile1.mqcdn.com/tiles/1.0.0/osm/'+z+'/'+x+'/'+y+'.jpg';
}
/**
* Closure for a single queue.
*
* loadedTiles is an object: [src] --> msec when load completed.
*
* Returns an object with addTo, removeFrom, removeOpen,
* and process functions attached.
*/
function Queue(coord, loadedTiles)
{
var queue = [],
queueById = {},
numOpenRequests = 0,
requestById = {};
/**
* Called with selection.each(), this == <img>.
*/
function addTo(tile, i)
{
var src = provider(tile);
if (src in loadedTiles) {
// if we've seen it this session the browser cache probably has it
d3.select(this).attr("src", src);
} else {
var item = { id: this.id, img: this };
queue.push(item);
queueById[item.id] = item;
}
}
/**
* Called with selection.each(), this == <img>.
*/
function removeFrom(tile, i)
{
// attempt to cancel loading for incomplete tiles
// and prevent very large/tiny tiles from being scaled
// (remove these immediately so they don't slow down positioning)
if (!this.complete || (coord.z - tile.z > 5) || (tile.z - coord.z > 2)) {
this.src = null;
d3.select(this).remove();
}
// also clear the open request
removeOpen(this);
// and mark the image null in the queue so it will be skipped
var item = queueById[this.id];
if (item) {
item.img = null;
delete queueById[this.id];
}
}
/**
* Called when tiles are complete /or/ canceled.
*/
function removeOpen(img)
{
var request = requestById[img.id];
if (request) {
request.img = null;
delete requestById[img.id];
numOpenRequests--;
}
}
/**
* Request up to 8 things from the queue, skipping blank items.
*/
function process()
{
while (numOpenRequests < 8 && queue.length > 0) {
var request = queue.shift();
if (request.img && request.img.parentNode) {
// luckily there's a magic mapping inside d3
// that knows how to pass the correct data to provider()
d3.select(request.img)
.attr("src", provider);
requestById[request.id] = request;
numOpenRequests++;
}
delete queueById[request.id];
}
}
return {addTo: addTo, removeFrom: removeFrom, removeOpen: removeOpen, process: process};
};
// ----- Tile Positioning Functions
// ----- end Tile Positioning Functions
/**
* Collection of static functions for operating on coordinates.
*
* Coordinates are simple objects with three properties:
* "r" (row), "c" (column), and "z" (zoom).
*/
var Coordinates = {
/**
* Return a new coordinate, zoomed by the given amount.
*/
zoomedBy: function(c, dz)
{
var power = Math.pow(2, dz);
return {c: c.c * power, r: c.r * power, z: c.z + dz};
},
/**
* Return a new coordinate, offset by the given coordinate.
*/
offsetBy: function(c, o)
{
return {c: c.c + o.c, r: c.r + o.r, z: c.z + o.z};
},
/**
* Return a new round-number coordinate containing the given coordinate.
*/
container: function(c)
{
c = Coordinates.zoomedBy(c, Math.round(c.z) - c.z);
return {c: Math.floor(c.c), r: Math.floor(c.r), z: c.z};
}
};
function Grid(parent)
{
var coord = {c: 2, r: 2, z: 2}, // col, row, zoom
roundCoord = null, // coord at an integer zoom level
tileSize = {w: 256, h: 256},// px
loadedTiles = {}, // [src] --> millis when load completed
w = window.innerWidth,
h = window.innerHeight,
center = {x: w/2, y: h/2 }; // center of map in pixels
var map = d3.select(parent),
queue = Queue(coord, loadedTiles);
d3.timer(redraw);
map.on('mousedown.map', onMouseDown)
.on('mousewheel.map', onWheel)
.on('DOMMouseScroll.map', onWheel);
d3.select(window).on('resize.map', onResize);
/**
* Return CSS left property value for a tile.
*
* Remove Math.round() for greater accuracy but visible seams
*/
function left(tile)
{
var scale = Math.pow(2, coord.z - tile.z),
power = Math.pow(2, tile.z - roundCoord.z),
centerCol = roundCoord.c * power;
return Math.round(center.x + (tile.c - centerCol) * tileSize.w * scale) + 'px';
}
/**
* Return CSS top property value for a tile.
*
* Remove Math.round() for greater accuracy but visible seams
*/
function top(tile)
{
var scale = Math.pow(2, coord.z - tile.z),
power = Math.pow(2, tile.z - roundCoord.z),
centerRow = roundCoord.r * power;
return Math.round(center.y + (tile.r - centerRow) * tileSize.h * scale) + 'px';
}
/**
* Return CSS width property value for a tile.
*
* Remove Math.ceil() for greater accuracy but visible seams
*/
function width(tile)
{
var scale = Math.pow(2, coord.z - tile.z);
return Math.ceil(scale * tileSize.w)+'px';
}
/**
* Return CSS height property value for a tile.
*
* Remove Math.ceil() for greater accuracy but visible seams
*/
function height(tile)
{
var scale = Math.pow(2, coord.z - tile.z);
return Math.ceil(scale * tileSize.h)+'px';
}
/**
* Return CSS transform property value for a tile.
*
* For 3D webkit mode
*/
function transform(tile)
{
var scale = Math.pow(2, coord.z - tile.z);
// adjust to nearest whole pixel scale (thx @tmcw)
if (scale * tileSize.w % 1) {
scale += (1 - scale * tileSize.w % 1) / tileSize.w;
}
var zoomedCoord = Coordinates.zoomedBy(roundCoord, tile.z - roundCoord.z),
x = Math.round(center.x + (tile.c - zoomedCoord.c) * tileSize.w * scale),
y = Math.round(center.y + (tile.r - zoomedCoord.r) * tileSize.h * scale);
return matrixString(scale, x, y, tileSize.w/2.0, tileSize.h/2.0);
}
/**
* Called with selection.on(), this == <img>.
*/
function onTileLoaded(tile)
{
loadedTiles[this.src] = Date.now();
d3.select(this)
.style("display", "block")
.style("opacity", 0.0)
.transition()
.duration(250)
.style("opacity", 1.0);
queue.removeOpen(this);
// request redraw (which will also check queue)
d3.timer(redraw,50); // TODO: only remove compensation tiles for this tile instead of a full redraw
}
// don't show above/below the poles
function validCoordinateFilter(tile)
{
var minRow = 0,
maxRow = Math.pow(2, tile.z);
return minRow <= tile.r && tile.r < maxRow;
}
function loadedLongAgo()
{
return (this.src in loadedTiles) && (Date.now() - loadedTiles[this.src] > 500);
}
function redraw()
{
// apply coord limits
if (coord.z > 18) {
coord = Coordinates.zoomedBy(coord, 18-coord.z);
} else if (coord.z < 0) {
coord = Coordinates.zoomedBy(coord, -coord.z);
}
// find coordinate extents of map
var tl = Coordinates.offsetBy(coord, {c: -center.x / tileSize.w, r: -center.y / tileSize.h, z: 0}),
br = Coordinates.offsetBy(coord, {c: center.x / tileSize.w, r: center.y / tileSize.h, z: 0});
// round coords to "best" zoom level
roundCoord = Coordinates.zoomedBy(coord, Math.round(coord.z)-coord.z);
tl = Coordinates.zoomedBy(tl, Math.round(tl.z)-tl.z);
br = Coordinates.zoomedBy(br, Math.round(br.z)-br.z);
// generate visible tile coords
var padding = 0;
var cols = d3.range( Math.floor(tl.c) - padding, Math.ceil(br.c) + padding),
rows = d3.range( Math.floor(tl.r) - padding, Math.ceil(br.r) + padding),
visibleCoords = [];
rows.forEach(function(row) {
cols.forEach(function(col) {
visibleCoords.push({c: col, r: row, z: roundCoord.z});
});
});
visibleCoords = visibleCoords.filter(validCoordinateFilter);
// explicitly preserve parent tiles for tiles we haven't already loaded
// not strictly necessary but helps with continuity on slow connections
var compensationCoords = [];
uniqueCompensations = {};
function addParentIfNeeded(tile)
{
if (tile.z > 0 && tile.z > coord.z - 3) {
var src = provider(tile);
if ((tile.z > coord.z - 1) || !(src in loadedTiles) || (Date.now() - loadedTiles[src] < 250)) {
tile = Coordinates.container(Coordinates.zoomedBy(tile, -1));
src = provider(tile);
if (src in loadedTiles && !(src in uniqueCompensations)) {
uniqueCompensations[src] = true;
compensationCoords.push(tile);
}
// better continuity if we loop, but slower (needs tuning)
addParentIfNeeded(tile);
}
}
}
visibleCoords.forEach(addParentIfNeeded);
visibleCoords = compensationCoords.concat(visibleCoords);
// Takes the place of just String on coordinates below
function coordString(c) { return [c.c, c.r, c.z].toString() };
var tiles = map.selectAll('img.tile')
.data(visibleCoords, coordString);
// setup new things
tiles.enter().append('img')
.attr("id", coordString)
.attr("class", "tile")
.style("opacity", 0.0)
.style("display", "none") // opacity doesn't seem to "take" until load event fires
.style("z-index", function(tile) { return 100 * tile.z })
.on('load', onTileLoaded)
.each(queue.addTo) // sets img src 8 at a time using provider()
// TODO: on('error')?
// ensure updating tiles are at opacity 1.0 (if old enough)
tiles.filter(loadedLongAgo).transition().duration(250).style("opacity", 1.0);
// clean up old things
tiles.exit()
.each(queue.removeFrom)
.transition()
.duration(250)
.style("opacity", 0.0)
.delay(250)
.remove()
.each('end',function() {
// prevents blank tiles if zooming confuses transitions
d3.timer(redraw,50);
});
// update all positions, enter/update/exit alike
if (transformProperty) {
map.selectAll('img.tile')
.style(transformProperty, transform);
} else {
map.selectAll('img.tile')
.style("left", left)
.style("top", top)
.style("width", width)
.style("height", height);
}
// see what's new
queue.process();
return true;
}
function onMouseDown()
{
var prevMouse = {x: d3.event.pageX, y: d3.event.pageY};
d3.select(window)
.on('mousemove.map', onMouseMove)
.on('mouseup.map', onMouseUp)
d3.event.preventDefault();
d3.event.stopPropagation();
function onMouseMove()
{
var mouse = prevMouse;
prevMouse = {x: d3.event.pageX, y: d3.event.pageY};
coord = Coordinates.offsetBy(coord, {
c: -((prevMouse.x - mouse.x) / tileSize.w),
r: -((prevMouse.y - mouse.y) / tileSize.h),
z: 0
});
d3.event.preventDefault();
d3.event.stopPropagation();
d3.timer(redraw);
}
function onMouseUp()
{
prevMouse = null;
d3.select(window)
.on('mousemove.map',null)
.on('mouseup.map',null);
}
}
function onWheel() {
// 18 = max zoom, 0 = min zoom
var delta = Math.min(18-coord.z,Math.max(0-coord.z,d3_behavior_zoomDelta()));
if (delta != 0) {
var mouse = {x: d3.event.pageX, y: d3.event.pageY};
coord = Coordinates.offsetBy(coord, {
c: ((mouse.x-center.x) / tileSize.w),
r: ((mouse.y-center.y) / tileSize.h),
z: 0
});
coord = Coordinates.zoomedBy(coord, delta);
coord = Coordinates.offsetBy(coord, {
c: -((mouse.x-center.x) / tileSize.w),
r: -((mouse.y-center.y) / tileSize.h),
z: 0
});
d3.timer(redraw);
}
d3.event.preventDefault();
d3.event.stopPropagation();
}
function onResize()
{
center = {x: window.innerWidth/2, y: window.innerHeight/2}
d3.timer(redraw);
}
};
window.onload = function() {
Grid('#map');
}
// expose this so our own mousewheel handler can use it
var d3_behavior_zoomDiv = null;
// detect the pixels that would be scrolled by this wheel event
function d3_behavior_zoomDelta() {
// mousewheel events are totally broken!
// https://bugs.webkit.org/show_bug.cgi?id=40441
// not only that, but Chrome and Safari differ in re. to acceleration!
if (!d3_behavior_zoomDiv) {
d3_behavior_zoomDiv = d3.select("body").append("div")
.style("visibility", "hidden")
.style("top", 0)
.style("height", 0)
.style("width", 0)
.style("overflow-y", "scroll")
.append("div")
.style("height", "2000px")
.node().parentNode;
}
var e = d3.event, delta;
try {
d3_behavior_zoomDiv.scrollTop = 250;
d3_behavior_zoomDiv.dispatchEvent(e);
delta = 250 - d3_behavior_zoomDiv.scrollTop;
} catch (error) {
delta = e.wheelDelta || (-e.detail * 5);
}
return delta * .005;
}
</script>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
border: 0;
}
#map {
position: absolute;
overflow: hidden;
margin: 0;
padding: 0;
border: 0;
}
img.tile {
display: block;
position: absolute;
margin: 0;
padding: 0;
border: 0;
-webkit-transform-origin: 0px 0px;
}
p {
font: bold 12px sans-serif;
position: absolute;
display: block;
right: 5px;
bottom: 5px;
color: white;
text-shadow: 1px 1px 4px rgba(0,0,0,0.75);
z-index: 250;
margin: 0;
padding: 5px;
}
p a {
color: white;
}
</style>
</head>
<body>
<div id="map" style="width: 100%; height: 100%;"></div>
<p>Tiles © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, CC-BY-SA. Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png"></p>
</body>
</html>