Morphing map projections with D3

There's no way to flatten a globe without stretching it somewhere, so every world map picks what to give up. Mercator keeps shapes true but blows up the land toward the poles. Equal-area maps keep sizes true but squash shapes. Interrupted maps cut the world open, through the oceans or along the edges of a folded-up solid, so that less of it has to stretch.

This map morphs through ten projections, from everyday ones to some of the stranger ones mapmakers have come up with. The orange circles are all the same size on the ground, 1,000 km across, so you can watch where each projection stretches and squashes them. Pause it, or pick a projection to morph straight to it.

1,000 km across
Land: Natural Earth

Each frame partway through a morph mixes where the two projections put every place on Earth, a little more of the second each frame, the way Jason Davies' projection transitions do. That works smoothly as long as neither projection cuts the world open. Goode's map is cut along four lines through the oceans, the butterflies and Airocean along the edges of their unfolded solids, and every flat map along the far side, so before each morph the land, ocean, and circles are cut along both projections' lines with d3-geo-polygon. A cut that only one of them has stays shut at that end and opens up along the way, so the continents tear apart and close up instead of jumping. The globe can't be mixed directly, since it only shows half the world, so it opens out through the azimuthal equal-area projection, which shows all of it in a disk, and its far side folds away out of sight.

Build it yourself

A page that morphs between plain projections, ones that only cut the world open along the far side, takes only D3. It's the map above without the interrupted maps, the globe, or the buttons. It was tested with d3-geo 3.1 and d3-geo-projection 4.0 in Chromium.

What you need

  • A web server to serve the page. D3 and the land need no key.

Get the land

Make a folder for the map, and in it download the land:

Terminal
curl -O https://mapricot.com/gallery/land.json

It's Natural Earth's land at 1:110 million as GeoJSON, rounded to about 100 meters, with its outer rings running clockwise, the way D3 expects.

Make the page

Next to it, make index.html:

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Morphing projections</title>
    <script type="importmap">
      {
        "imports": {
          "d3-geo": "https://cdn.jsdelivr.net/npm/d3-geo@3.1.1/+esm",
          "d3-geo-projection": "https://cdn.jsdelivr.net/npm/d3-geo-projection@4.0.0/+esm"
        }
      }
    </script>
  </head>
  <body style="margin: 0">
    <canvas style="display: block; width: 100vw; height: 100vh"></canvas>
    <script type="module">
      import { geoCircle, geoEqualEarthRaw, geoMercatorRaw, geoNaturalEarth1Raw, geoPath, geoProjection } from "d3-geo";
      import { geoMollweideRaw, geoRobinsonRaw } from "d3-geo-projection";

      const mercatorRaw = (x, y) => geoMercatorRaw(x, Math.max(-1.484, Math.min(1.484, y)));
      const projections = [geoEqualEarthRaw, mercatorRaw, geoRobinsonRaw, geoMollweideRaw, geoNaturalEarth1Raw];

      const land = await (await fetch("land.json")).json();
      const circles = { type: "MultiPolygon", coordinates: [] };
      for (let y = -60; y <= 60; y += 30) {
        for (let x = -150; x <= 150; x += 30) circles.coordinates.push(geoCircle().center([x, y]).radius(4.5)().coordinates);
      }

      const canvas = document.querySelector("canvas");
      const context = canvas.getContext("2d");

      function draw(a, b, t) {
        const raw = (x, y) => {
          const p = a(x, y);
          const q = b(x, y);
          return [p[0] + (q[0] - p[0]) * t, p[1] + (q[1] - p[1]) * t];
        };
        const width = canvas.clientWidth;
        const height = canvas.clientHeight;
        canvas.width = width * devicePixelRatio;
        canvas.height = height * devicePixelRatio;
        context.scale(devicePixelRatio, devicePixelRatio);
        const path = geoPath(geoProjection(raw).fitExtent([[20, 20], [width - 20, height - 20]], { type: "Sphere" }), context);
        const shape = (feature, fill, stroke) => {
          context.beginPath();
          path(feature);
          context.fillStyle = fill;
          context.fill();
          context.strokeStyle = stroke;
          context.stroke();
        };
        shape({ type: "Sphere" }, "#d3e4f1", "#d3e4f1");
        shape(land, "#f3e8cf", "#b9a379");
        shape(circles, "#f59f3a", "#c2620a");
      }

      let i = 0;
      let start = performance.now();
      requestAnimationFrame(function frame(now) {
        const t = Math.min(1, Math.max(0, (now - start - 2500) / 2000));
        draw(projections[i], projections[(i + 1) % projections.length], t * t * (3 - 2 * t));
        if (t === 1) {
          i = (i + 1) % projections.length;
          start = now;
        }
        requestAnimationFrame(frame);
      });
    </script>
  </body>
</html>
  • Each projection is a raw projection: a function from longitude and latitude, in radians, to x and y, before D3 scales, moves, and clips it. draw makes a new one that's a mix of two, t of the way from the first to the second, and geoProjection turns it into a projection like any other.
  • All five cut the world open only along the far side, 180° from the middle of the map, so D3's own clipping along that line works for every mix of them.
  • Mercator's poles are infinitely far away, so mercatorRaw stops at about 85°, where the map is square, and squeezes the rest into its top and bottom edges.
  • fitExtent scales each frame to fill the window, since some mixes come out wider or taller than either projection.
  • Each projection holds still for 2.5 seconds, then morphs to the next for 2, easing in and out.

Open it

Serve the folder with npx serve or python3 -m http.server, and open the address it prints. Opened straight from the file, the page stays blank, because the browser won't fetch land.json from a file. You should see the world on Equal Earth with rows of orange circles, morphing to a square Mercator map with the circles swelling toward the poles, then on through Robinson, Mollweide, and Natural Earth.

To add a projection that's cut open elsewhere, such as Goode's or a polyhedral one, the land has to be cut along its lines too, as the map above does in its own code.

Data