Created
October 24, 2014 12:37
-
-
Save popenkomaksim/5220f4e73198e9d09e25 to your computer and use it in GitHub Desktop.
test d3 page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
margin: 0; | |
} | |
path { | |
fill: none; | |
stroke: red; | |
stroke-linejoin: round; | |
stroke-width: 1.5px; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://d3js.org/d3.geo.tile.v0.min.js"></script> | |
<script src="http://d3js.org/topojson.v1.min.js"></script> | |
<script> | |
var width = Math.max(960, window.innerWidth), | |
height = Math.max(500, window.innerHeight); | |
var tile = d3.geo.tile() | |
.size([width, height]); | |
var projection = d3.geo.mercator() | |
.scale((1 << 12) / 2 / Math.PI) | |
.translate([width / 2, height / 2]); | |
var center = projection([-100, 40]); | |
var path = d3.geo.path() | |
.projection(projection); | |
var zoom = d3.behavior.zoom() | |
.scale(projection.scale() * 2 * Math.PI) | |
.scaleExtent([1 << 11, 1 << 14]) | |
.translate([width - center[0], height - center[1]]) | |
.on("zoom", zoomed); | |
// With the center computed, now adjust the projection such that | |
// it uses the zoom behavior’s translate and scale. | |
projection | |
.scale(1 / 2 / Math.PI) | |
.translate([0, 0]); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var raster = svg.append("g"); | |
var vector = svg.append("path"); | |
d3.json("/d/4090846/us.json", function(error, us) { | |
svg.call(zoom); | |
vector.attr("d", path(topojson.mesh(us, us.objects.counties))); | |
zoomed(); | |
}); | |
function zoomed() { | |
var tiles = tile | |
.scale(zoom.scale()) | |
.translate(zoom.translate()) | |
(); | |
vector | |
.attr("transform", "translate(" + zoom.translate() + ")scale(" + zoom.scale() + ")") | |
.style("stroke-width", 1 / zoom.scale()); | |
var image = raster | |
.attr("transform", "scale(" + tiles.scale + ")translate(" + tiles.translate + ")") | |
.selectAll("image") | |
.data(tiles, function(d) { return d; }); | |
image.exit() | |
.remove(); | |
image.enter().append("image") | |
.attr("xlink:href", function(d) { return "http://" + ["a", "b", "c", "d"][Math.random() * 4 | 0] + ".tiles.mapbox.com/v3/examples.map-i86nkdio/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; }) | |
.attr("width", 1) | |
.attr("height", 1) | |
.attr("x", function(d) { return d[0]; }) | |
.attr("y", function(d) { return d[1]; }); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment