Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active December 20, 2022 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mbostock/6619561 to your computer and use it in GitHub Desktop.
Save mbostock/6619561 to your computer and use it in GitHub Desktop.
Rotating Orthographic
license: gpl-3.0
redirect: https://observablehq.com/@d3/rotating-orthographic
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
background: #fcfcfa;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 720,
speed = 1e-2,
start = Date.now();
var sphere = {type: "Sphere"};
var projection = d3.geo.orthographic()
.scale(height / 2.1)
.translate([width / 2, height / 2])
.clipAngle(90)
.precision(.5);
var graticule = d3.geo.graticule();
var canvas = d3.select("body").append("canvas")
.attr("width", width)
.attr("height", height);
var context = canvas.node().getContext("2d");
var path = d3.geo.path()
.projection(projection)
.context(context);
d3.json("/mbostock/raw/4090846/world-110m.json", function(error, topo) {
if (error) throw error;
var land = topojson.feature(topo, topo.objects.land),
borders = topojson.mesh(topo, topo.objects.countries, function(a, b) { return a !== b; }),
grid = graticule();
d3.timer(function() {
projection.rotate([speed * (Date.now() - start), -15]);
context.clearRect(0, 0, width, height);
context.beginPath();
path(sphere);
context.lineWidth = 3;
context.strokeStyle = "#000";
context.stroke();
context.beginPath();
path(sphere);
context.fillStyle = "#fff";
context.fill();
context.beginPath();
path(land);
context.fillStyle = "#222";
context.fill();
context.beginPath();
path(borders);
context.lineWidth = .5;
context.strokeStyle = "#fff";
context.stroke();
context.beginPath();
path(grid);
context.lineWidth = .5;
context.strokeStyle = "rgba(119,119,119,.5)";
context.stroke();
});
});
d3.select(self.frameElement).style("height", height + "px");
</script>
@acifuentes00
Copy link

hello good, I'm new using the library and I'm not very familiar with the respective labels, my questions about this example are:

  1. Is it possible to stop the timer with timer.shop with a click on the canvas?
  2. Once the animation is stopped, is it possible to use .on ("click", function (d) {}) to select a country of the animation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment