-
-
Save nuernber/aeaac0e8edcf7ca93ade to your computer and use it in GitHub Desktop.
map
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
<html> | |
<head> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<style> | |
svg { border:1px solid black; } | |
</style> | |
</head> | |
<body> | |
<svg id="svg" width="640" height="480" xmlns="http://www.w3.org/2000/svg" version="1.1"> | |
<defs> | |
<clipPath id="rectClip"> | |
<rect x="150" y="25" width="400" height="400" style="stroke: gray; fill: none;"/> | |
</clipPath> | |
</defs> | |
<g id="outer_group"> | |
<circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red" /> | |
<g id="svg_map" style="clip-path: url(#rectClip);"> | |
</g> | |
</g> | |
</svg><br/> | |
<script type="text/javascript"> | |
var svg = d3.select("#svg_map"); | |
var mapGroup = svg.append("g"); | |
var projection = d3.geo.mercator(); | |
var path = d3.geo.path().projection(projection); | |
var zoom = d3.behavior.zoom() | |
.translate(projection.translate()) | |
.scale(projection.scale()) | |
.on("zoom", zoomed); | |
mapGroup.call(zoom); | |
var pan = d3.behavior.zoom() | |
.on("zoom", panned); | |
d3.select("svg").call(pan); | |
mapGroup.attr("transform", "translate(200,0) scale(2,2)"); | |
d3.json("map.json", function(collection) { | |
mapGroup.selectAll("path").data(collection.features) | |
.enter().append("path") | |
.attr("d", path) | |
.attr("id", function(d) { return d.properties.name.replace(/\s+/g, "")}) | |
.style("fill", "gray").style("stroke", "white").style("stroke-width",1); | |
} | |
); | |
function panned() { | |
var x = d3.event.translate[0]; | |
var y = d3.event.translate[1]; | |
d3.select("#outer_group").attr("transform", "translate("+x+","+y+") scale(" + d3.event.scale + ")"); | |
} | |
function zoomed() { | |
previousScale = d3.event.scale; | |
projection.translate(d3.event.translate).scale(d3.event.scale); | |
translationOffset = d3.event.translate; | |
mapGroup.selectAll("path").attr("d", path); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment