Skip to content

Instantly share code, notes, and snippets.

@martgnz
Last active October 28, 2017 15:09
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 martgnz/bf93d31bd25c57617841decc8b1470e3 to your computer and use it in GitHub Desktop.
Save martgnz/bf93d31bd25c57617841decc8b1470e3 to your computer and use it in GitHub Desktop.
World Hover
license: mit
border: none

Hover over a country to get its name.

<!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>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment