block by mbostock 1de35c45f8718c678712

Multiple Rotations

Full Screen

This globe rotates along φ (latitude) and then along λ (longtitude). Normally, the axes are rotated in the opposite order. D3’s new geometry pipeline allows multiple rotations to be applied sequentially, allowing you to specify the desired order.

index.html

<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="d3.js"></script>
<script>

var width = 960,
    height = 960,
    scale = width / 2 - 4;

var canvas = d3.select("body").append("canvas")
    .attr("width", width)
    .attr("height", height);

var context = canvas.node().getContext("2d");

var sphere = {type: "Sphere"},
    graticule = d3.geo.graticule()();

d3.timer(function(elapsed) {
  var render = d3.geo.pipeline()
      .source(d3.geo.jsonSource)
      .pipe(d3.geo.rotate, 0, -.5, 0)
      .pipe(d3.geo.rotate, elapsed / 3000, 0, 0)
      .pipe(d3.geo.clipCircle, Math.PI / 2)
      .pipe(d3.geo.project, d3.geo.orthographic, .3 / scale)
      .sink(d3.geom.contextSink, context);

  context.clearRect(0, 0, width, height);
  context.save();
  context.translate(width / 2, height / 2);
  context.scale(scale, -scale);

  context.beginPath();
  render(graticule);
  context.lineWidth = 1 / scale;
  context.stroke();

  context.beginPath();
  render(sphere);
  context.lineWidth = 2.5 / scale;
  context.stroke();

  context.restore();
});

d3.select(self.frameElement).style("height", height + "px");

</script>