cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <p>
  <span>Centering (left)</span> vs.
  <span>Rotating (right)</span> in D3
</p>

<p id="info">
  Mercator (top) and Orthographic (bottom)
</p>

<canvas width="900" height="900"></canvas>

<p>
  Blog post:
  <a href="https://petrichor.studio/2018/06/29/map-projections-in-the-web/" target="_blank">
    "Map Projections in the Web"
  </a>
</p>

<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://unpkg.com/topojson-client@3.0.0/dist/topojson-client.min.js"></script>
<script src="https://unpkg.com/topojson-simplify@3.0.2/dist/topojson-simplify.min.js"></script>
            
          
!
            
              body {
  margin: 10px;
  text-align: center;
  font-family: sans-serif;
}

canvas {
  max-width: calc(100vw - 40px);
  max-height: 82.5vh;
  outline: 1px solid black;
}

span:first-of-type {
  color: #7570b3;
  font-weight: bold;
}

span:last-of-type {
  color: #1b9e77;
  font-weight: bold;
}

p {
  margin: 7px 0;
  font-size: 0.9rem;
}


            
          
!
            
              var canvas = d3.select("canvas");
var w = +canvas.attr("width");
var h = +canvas.attr("height");

var ctx = canvas.node().getContext("2d");
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.fillStyle = "white";
ctx.lineWidth = 2;

var geoJsonSphere = {
  type: "Sphere"
};

var geoPathGeneratorTL = d3
  .geoPath()
  .projection(
    d3
      .geoMercator()
      .fitExtent([[10, 10], [w / 2 - 10, h / 2 - 10]], geoJsonSphere)
  )
  .context(ctx);

var geoPathGeneratorTR = d3
  .geoPath()
  .projection(
    d3
      .geoMercator()
      .fitExtent([[w / 2 + 10, 10], [w - 10, h / 2 - 10]], geoJsonSphere)
  )
  .context(ctx);

var geoPathGeneratorBL = d3
  .geoPath()
  .projection(
    d3
      .geoOrthographic()
      .fitExtent([[10, h / 2 + 10], [w / 2 - 10, h - 10]], geoJsonSphere)
  )
  .context(ctx);

var geoPathGeneratorBR = d3
  .geoPath()
  .projection(
    d3
      .geoOrthographic()
      .fitExtent([[w / 2 + 10, h / 2 + 10], [w - 10, h - 10]], geoJsonSphere)
  )
  .context(ctx);

var stops = [
  [40, 0, 0],
  [-40, 0, 0],
  [0, 0, 0],
  [0, -60, 0],
  [0, 60, 0],
  [0, 0, 0],
  [0, 0, 180],
  [0, 0, 0]
];
var stopsCounter = 0;
var animationTimer;
var infoContent = d3.select("p#info");
var originalInfoContent = infoContent.html();

var geoJson;
d3
  .json("https://unpkg.com/world-atlas@1.1.4/world/110m.json")
  .then(function(loadedTopoJson) {
    var topology = topojson.presimplify(loadedTopoJson);
    topology = topojson.simplify(topology, 1.5);
    geoJson = topojson.mesh(topology, topology.objects.land);

    startAnimationTimer();
    visitStops(stops);
  });

function startAnimationTimer() {
  animationTimer = d3.timer(function() {
    ctx.clearRect(0, 0, w, h);

    // top left and bottom left
    ctx.strokeStyle = "#7570b3";

    ctx.beginPath();
    geoPathGeneratorTL(geoJsonSphere);
    ctx.fill();
    ctx.stroke();
    ctx.closePath();

    ctx.beginPath();
    geoPathGeneratorTL(geoJson);
    ctx.stroke();
    ctx.closePath();

    ctx.beginPath();
    geoPathGeneratorBL(geoJsonSphere);
    ctx.fill();
    ctx.stroke();
    ctx.closePath();

    ctx.beginPath();
    geoPathGeneratorBL(geoJson);
    ctx.stroke();
    ctx.closePath();

    // top right and bottom right
    ctx.strokeStyle = "#1b9e77";

    ctx.beginPath();
    geoPathGeneratorTR(geoJsonSphere);
    geoPathGeneratorBR(geoJsonSphere);
    ctx.fill();
    ctx.stroke();
    ctx.closePath();

    ctx.beginPath();
    geoPathGeneratorTR(geoJson);
    geoPathGeneratorBR(geoJson);
    ctx.stroke();
    ctx.closePath();
  });
}

function visitStops(stops) {
  d3
    .select({})
    .transition()
    .duration(3000)
    .tween(null, function() {
      var interpolatorTL = d3.interpolate(
        geoPathGeneratorTL.projection().center(),
        stops[0]
      );

      var interpolatorTR = d3.interpolate(
        geoPathGeneratorTR.projection().rotate(),
        [-stops[0][0], -stops[0][1], -stops[0][2]]
      );

      var interpolatorBL = d3.interpolate(
        geoPathGeneratorBL.projection().center(),
        stops[0]
      );

      var interpolatorBR = d3.interpolate(
        geoPathGeneratorBR.projection().rotate(),
        [-stops[0][0], -stops[0][1], -stops[0][2]]
      );

      return function(t) {
        geoPathGeneratorTL.projection().center(interpolatorTL(t));
        geoPathGeneratorTR.projection().rotate(interpolatorTR(t));
        geoPathGeneratorBL.projection().center(interpolatorBL(t));
        geoPathGeneratorBR.projection().rotate(interpolatorBR(t));
      };
    })
    .on("end", function() {
      stops.push(stops.shift());
      stopsCounter++;

      if (stopsCounter >= stops.length) {
        infoContent.html("<strong>click below to replay demo</strong>");
        animationTimer.stop();

        // attach click listener
        d3
          .select("canvas")
          .style("cursor", "pointer")
          .on("click", function() {
            // remove click listener (to be like a one-time listener)
            d3
              .select(this)
              .style("cursor", "unset")
              .on("click", null);

            startAnimationTimer();
            stopsCounter = 0;
            visitStops(stops);
            infoContent.html(originalInfoContent);
          });

        return;
      }

      visitStops(stops);
    });
}

            
          
!
999px
Loading ..................

Console