Hover over a country to get its name.
Last active
October 28, 2017 15:09
-
-
Save martgnz/bf93d31bd25c57617841decc8b1470e3 to your computer and use it in GitHub Desktop.
World Hover
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
license: mit | |
border: none |
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> | |
canvas { | |
cursor: crosshair | |
} | |
</style> | |
<body> | |
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<script src="https://d3js.org/topojson.v1.min.js"></script> | |
<script src="https://unpkg.com/rbush@1.4.3/rbush.js"></script> | |
<script src="https://unpkg.com/d3-geo-projection@0.2.16/d3.geo.projection.min.js"></script> | |
<script src="https://unpkg.com/spamjs@1.1.0/spam.min.js"></script> | |
<script type='text/javascript'> | |
var hover = null; | |
var width = 960; | |
var height = 500; | |
var graticule = d3.geo.graticule(); | |
d3.json("world.json", function(error, d) { | |
topojson.presimplify(d); | |
var map = new StaticCanvasMap({ | |
element: "body", | |
width: width, | |
projection: d3.geo.robinson(), | |
data: [ | |
{ | |
features: topojson.feature(d, d.objects["countries"]), | |
static: { | |
prepaint: function(parameters) { | |
parameters.context.beginPath(); | |
parameters.path(graticule()); | |
parameters.context.lineWidth = 0.5; | |
parameters.context.strokeStyle = "rgb(200,200,200)"; | |
parameters.context.stroke(); | |
parameters.context.beginPath(); | |
parameters.path({ type: "Sphere" }); | |
parameters.context.lineWidth = 1; | |
parameters.context.strokeStyle = "rgb(30,30,30)"; | |
parameters.context.stroke(); | |
}, | |
paintfeature: function(parameters, d) { | |
parameters.context.lineWidth = 0.5; | |
parameters.context.strokeStyle = "rgb(30,30,30)"; | |
parameters.context.stroke(); | |
parameters.context.fillStyle = "rgb(192, 224, 186)"; | |
parameters.context.fill(); | |
} | |
}, | |
dynamic: { | |
postpaint: function(parameters) { | |
if (!hover) return; | |
parameters.context.beginPath(); | |
parameters.context.lineWidth = 1.5 / parameters.scale; | |
parameters.path(hover); | |
parameters.context.stroke(); | |
parameters.context.beginPath(); | |
parameters.context.textAlign = "center"; | |
parameters.context.font = "bold 50px sans-serif"; | |
parameters.context.fillStyle = "rgba(70,70,70,0.5)"; | |
parameters.context.fillText(hover.id, width / 2, height / 2); | |
parameters.context.fill(); | |
} | |
}, | |
events: { | |
hover: function(parameters, d) { | |
hover = d; | |
parameters.map.paint(); | |
} | |
} | |
} | |
] | |
}); | |
map.init(); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment