Skip to content

Instantly share code, notes, and snippets.

@wboykinm
Forked from mbostock/.block
Last active December 27, 2015 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wboykinm/7388233 to your computer and use it in GitHub Desktop.
Save wboykinm/7388233 to your computer and use it in GitHub Desktop.
Vector Tiles in D3, darkened
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
margin: 0;
background:#031536;
}
.map {
position: absolute;
overflow: hidden;
top:0px;
bottom:0px;
}
.layer {
position: absolute;
}
.tile {
position: absolute;
width: 256px;
height: 256px;
}
.tile path {
fill: none;
stroke: #000;
stroke-linejoin: round;
stroke-linecap: round;
}
.tile .major_road { stroke: url(#grad2); stroke-width:1.8;}
.tile .minor_road { stroke: url(#grad3); stroke-width:1.2;}
.tile .highway { stroke: url(#grad1); stroke-width: 3px; stroke-linecap: round; stroke-linejoin: round; }
.tile .rail { stroke: url(#grad4); stroke-dasharray:4; stroke-dashoffset:4; stroke-width:1; }
.tile .path { stroke: url(#grad5); stroke-dasharray:2; stroke-dashoffset:4; stroke-width:0.8; }
.info {
position: absolute;
bottom: 10px;
left: 10px;
color:#fff;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/d3.geo.tile.v0.min.js"></script>
<svg>
<defs>
<linearGradient id='grad1'>
<stop offset='0%' stop-color='#E8DDC9'/>
<stop offset='100%' stop-color='#E8DDC9' stop-opacity='0' />
</linearGradient>
<linearGradient id='grad2'>
<stop offset='0%' stop-color='#CCB479'/>
<stop offset='100%' stop-color='#CCB479' stop-opacity='0' />
</linearGradient>
<linearGradient id='grad3'>
<stop offset='0%' stop-color='#3C80A9'/>
<stop offset='100%' stop-color='#3C80A9' stop-opacity='0' />
</linearGradient>
<linearGradient id='grad4'>
<stop offset='0%' stop-color='#7de'/>
<stop offset='100%' stop-color='#7de' stop-opacity='0' />
</linearGradient>
<linearGradient id='grad5'>
<stop offset='0%' stop-color='#74DC9B'/>
<stop offset='100%' stop-color='#74DC9B' stop-opacity='0' />
</linearGradient>
</defs>
</svg>
<script>
var width = Math.max(960, window.innerWidth),
height = Math.max(500, window.innerHeight),
prefix = prefixMatch(["webkit", "ms", "Moz", "O"]);
var tile = d3.geo.tile()
.size([width, height]);
var projection = d3.geo.mercator()
.scale((1 << 23) / 2 / Math.PI)
.translate([-width / 2, -height / 2]); // just temporary
var tileProjection = d3.geo.mercator();
var tilePath = d3.geo.path()
.projection(tileProjection);
var zoom = d3.behavior.zoom()
.scale(projection.scale() * 2 * Math.PI)
.scaleExtent([1 << 20, 1 << 23])
.translate(projection([-77.045446166, 38.89370499]).map(function(x) { return -x; }))
.on("zoom", zoomed);
var map = d3.select("body").append("div")
.attr("class", "map")
.style("width", width + "px")
.style("height", height + "px")
.call(zoom)
.on("mousemove", mousemoved);
var layer = map.append("div")
.attr("class", "layer");
var info = map.append("div")
.attr("class", "info");
zoomed();
function zoomed() {
var tiles = tile
.scale(zoom.scale())
.translate(zoom.translate())
();
projection
.scale(zoom.scale() / 2 / Math.PI)
.translate(zoom.translate());
var image = layer
.style(prefix + "transform", matrix3d(tiles.scale, tiles.translate))
.selectAll(".tile")
.data(tiles, function(d) { return d; });
image.exit()
.each(function(d) { this._xhr.abort(); })
.remove();
image.enter().append("svg")
.attr("class", "tile")
.style("left", function(d) { return d[0] * 256 + "px"; })
.style("top", function(d) { return d[1] * 256 + "px"; })
.each(function(d) {
var svg = d3.select(this);
this._xhr = d3.json("http://" + ["a", "b", "c"][(d[0] * 31 + d[1]) % 3] + ".tile.openstreetmap.us/vectiles-highroad/" + d[2] + "/" + d[0] + "/" + d[1] + ".json", function(error, json) {
var k = Math.pow(2, d[2]) * 256; // size of the world in pixels
tilePath.projection()
.translate([k / 2 - d[0] * 256, k / 2 - d[1] * 256]) // [0°,0°] in pixels
.scale(k / 2 / Math.PI);
svg.selectAll("path")
.data(json.features.sort(function(a, b) { return a.properties.sort_key - b.properties.sort_key; }))
.enter().append("path")
.attr("class", function(d) { return d.properties.kind; })
.attr("d", tilePath);
});
});
}
function mousemoved() {
info.text(formatLocation(projection.invert(d3.mouse(this)), zoom.scale()));
}
function matrix3d(scale, translate) {
var k = scale / 256, r = scale % 1 ? Number : Math.round;
return "matrix3d(" + [k, 0, 0, 0, 0, k, 0, 0, 0, 0, k, 0, r(translate[0] * scale), r(translate[1] * scale), 0, 1 ] + ")";
}
function prefixMatch(p) {
var i = -1, n = p.length, s = document.body.style;
while (++i < n) if (p[i] + "Transform" in s) return "-" + p[i].toLowerCase() + "-";
return "";
}
function formatLocation(p, k) {
var format = d3.format("." + Math.floor(Math.log(k) / 2 - 2) + "f");
return (p[1] < 0 ? format(-p[1]) + "°S" : format(p[1]) + "°N") + " "
+ (p[0] < 0 ? format(-p[0]) + "°W" : format(p[0]) + "°E");
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment