Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>
  D3 Map Projections
</h1>

<p>
  geoOrthographic vs.
  <select id="projectionChoice">
    <option value="geoAiry">geoAiry</option>
    <option value="geoAirocean">geoAirocean</option>
    <option value="geoAitoff">geoAitoff</option>
    <option value="geoAlbers">geoAlbers</option>
    <option value="geoArmadillo">geoArmadillo</option>
    <option value="geoAugust">geoAugust</option>
    <option value="geoAzimuthalEqualArea">geoAzimuthalEqualArea</option>
    <option value="geoAzimuthalEquidistant">geoAzimuthalEquidistant</option>
    <option value="geoBaker">geoBaker</option>
    <option value="geoBerghaus">geoBerghaus</option>
    <option value="geoBertin1953">geoBertin1953</option>
    <option value="geoBoggs">geoBoggs</option>
    <option value="geoBonne">geoBonne</option>
    <option value="geoBottomley">geoBottomley</option>
    <option value="geoBromley">geoBromley</option>
    <option value="geoCahillKeyes">geoCahillKeyes</option>
    <option value="geoCollignon">geoCollignon</option>
    <option value="geoComplexLog">geoComplexLog</option>
    <option value="geoConicEqualArea">geoConicEqualArea</option>
    <option value="geoConicEquidistant">geoConicEquidistant</option>
    <option value="geoCox">geoCox</option>
    <option value="geoCraig">geoCraig</option>
    <option value="geoCraster">geoCraster</option>
    <option value="geoCubic">geoCubic</option>
    <option value="geoCylindricalEqualArea">geoCylindricalEqualArea</option>
    <option value="geoCylindricalStereographic">geoCylindricalStereographic</option>
    <option value="geoDodecahedral">geoDodecahedral</option>
    <option value="geoEckert1">geoEckert1</option>
    <option value="geoEckert2">geoEckert2</option>
    <option value="geoEckert3">geoEckert3</option>
    <option value="geoEckert4">geoEckert4</option>
    <option value="geoEckert5">geoEckert5</option>
    <option value="geoEckert6">geoEckert6</option>
    <option value="geoEisenlohr">geoEisenlohr</option>
    <option value="geoEqualEarth">geoEqualEarth</option>
    <option value="geoEquirectangular">geoEquirectangular</option>
    <option value="geoFahey">geoFahey</option>
    <option value="geoFoucaut">geoFoucaut</option>
    <option value="geoFoucautSinusoidal">geoFoucautSinusoidal</option>
    <option value="geoGilbert">geoGilbert</option>
    <option value="geoGingery">geoGingery</option>
    <option value="geoGinzburg4">geoGinzburg4</option>
    <option value="geoGinzburg5">geoGinzburg5</option>
    <option value="geoGinzburg6">geoGinzburg6</option>
    <option value="geoGinzburg8">geoGinzburg8</option>
    <option value="geoGinzburg9">geoGinzburg9</option>
    <option value="geoGnomonic">geoGnomonic</option>
    <option value="geoGringorten">geoGringorten</option>
    <option value="geoGringortenQuincuncial">geoGringortenQuincuncial</option>
    <option value="geoGuyou">geoGuyou</option>
    <option value="geoHammer">geoHammer</option>
    <option value="geoHammerRetroazimuthal">geoHammerRetroazimuthal</option>
    <option value="geoHealpix">geoHealpix</option>
    <option value="geoHill">geoHill</option>
    <option value="geoHomolosine">geoHomolosine</option>
    <option value="geoHufnagel">geoHufnagel</option>
    <option value="geoHyperelliptical">geoHyperelliptical</option>
    <option value="geoIcosahedral">geoIcosahedral</option>
    <option value="geoImago">geoImago</option>
    <option value="geoInterruptedBoggs">geoInterruptedBoggs</option>
    <option value="geoInterruptedHomolosine">geoInterruptedHomolosine</option>
    <option value="geoInterruptedMollweide">geoInterruptedMollweide</option>
    <option value="geoInterruptedMollweideHemispheres">geoInterruptedMollweideHemispheres</option>
    <option value="geoInterruptedQuarticAuthalic">geoInterruptedQuarticAuthalic</option>
    <option value="geoInterruptedSinuMollweide">geoInterruptedSinuMollweide</option>
    <option value="geoInterruptedSinusoidal">geoInterruptedSinusoidal</option>
    <option value="geoKavrayskiy7">geoKavrayskiy7</option>
    <option value="geoLagrange">geoLagrange</option>
    <option value="geoLarrivee">geoLarrivee</option>
    <option value="geoLaskowski">geoLaskowski</option>
    <option value="geoLoximuthal">geoLoximuthal</option>
    <option value="geoMercator" selected>geoMercator</option>
    <option value="geoMiller">geoMiller</option>
    <option value="geoMollweide">geoMollweide</option>
    <option value="geoMtFlatPolarParabolic">geoMtFlatPolarParabolic</option>
    <option value="geoMtFlatPolarQuartic">geoMtFlatPolarQuartic</option>
    <option value="geoMtFlatPolarSinusoidal">geoMtFlatPolarSinusoidal</option>
    <option value="geoNaturalEarth1">geoNaturalEarth1</option>
    <option value="geoNaturalEarth2">geoNaturalEarth2</option>
    <option value="geoNellHammer">geoNellHammer</option>
    <option value="geoNicolosi">geoNicolosi</option>
    <option value="geoOrthographic">geoOrthographic</option>
    <option value="geoPatterson">geoPatterson</option>
    <option value="geoPeirceQuincuncial">geoPeirceQuincuncial</option>
    <option value="geoPolyconic">geoPolyconic</option>
    <option value="geoPolyhedralButterfly">geoPolyhedralButterfly</option>
    <option value="geoPolyhedralCollignon">geoPolyhedralCollignon</option>
    <option value="geoPolyhedralWaterman">geoPolyhedralWaterman</option>
    <option value="geoPolyhedralVoronoi">geoPolyhedralVoronoi</option>
    <option value="geoRectangularPolyconic">geoRectangularPolyconic</option>
    <option value="geoRobinson">geoRobinson</option>
    <option value="geoSatellite">geoSatellite</option>
    <option value="geoSinuMollweide">geoSinuMollweide</option>
    <option value="geoSinusoidal">geoSinusoidal</option>
    <option value="geoStereographic">geoStereographic</option>
    <option value="geoTetrahedralLee">geoTetrahedralLee</option>
    <option value="geoTimes">geoTimes</option>
    <option value="geoTransverseMercator">geoTransverseMercator</option>
    <option value="geoVanDerGrinten">geoVanDerGrinten</option>
    <option value="geoVanDerGrinten2">geoVanDerGrinten2</option>
    <option value="geoVanDerGrinten3">geoVanDerGrinten3</option>
    <option value="geoVanDerGrinten4">geoVanDerGrinten4</option>
    <option value="geoWagner">geoWagner</option>
    <option value="geoWagner4">geoWagner4</option>
    <option value="geoWagner6">geoWagner6</option>
    <option value="geoWagner7">geoWagner7</option>
    <option value="geoWiechel">geoWiechel</option>
    <option value="geoWinkel3">geoWinkel3</option>
  </select>
</p>

<p>
  <label>
    <input id="axialTilt" type="checkbox" checked/>
    Keep it real! Use Earth's axial tilt of ~23.4°.
  </label>
</p>

<canvas id="ortho" width="960" height="600"></canvas>
<canvas id="any" width="960" height="600"></canvas>

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

<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://unpkg.com/d3-geo-projection@2.9.0/dist/d3-geo-projection.min.js"></script>
<script src="https://unpkg.com/d3-geo-polygon@1.11.0/dist/d3-geo-polygon.min.js"></script>
<script src="https://unpkg.com/topojson-client@3.1.0/dist/topojson-client.min.js"></script>
<script src="https://unpkg.com/topojson-simplify@3.0.3/dist/topojson-simplify.min.js"></script>

              
            
!

CSS

              
                body {
  margin: 5px;
  text-align: center;
  font-family: sans-serif;
}

h1 {
  font-size: 1.25rem;
  margin: 7px 0;
}

p {
  font-size: 0.8rem;
}

p:first-of-type {
  font-weight: bold;
}

select#projectionChoice {
  max-width: 50vw;
  font-weight: bold;
  cursor: pointer;
}

label {
  cursor: pointer;
}

input#axialTilt {
  vertical-align: middle;
}

canvas {
  max-width: calc(50vw - 20px);
}

              
            
!

JS

              
                var canvasAny = d3.select("canvas#any"),
  ctxAny = canvasAny.node().getContext("2d"),
  canvasAnyWidth = +canvasAny.attr("width"),
  canvasAnyHeight = +canvasAny.attr("height");

var projectionAny = d3
  .geoMercator()
  .rotate([-160, 0, -23.4])
  .fitSize([canvasAnyWidth, canvasAnyHeight], {
    type: "Sphere"
  });

var geoPathGeneratorAny = d3
  .geoPath()
  .projection(projectionAny)
  .context(ctxAny);

var canvasOrtho = d3.select("canvas#ortho"),
  ctxOrtho = canvasOrtho.node().getContext("2d"),
  canvasOrthoWidth = +canvasOrtho.attr("width"),
  canvasOrthoHeight = +canvasOrtho.attr("height");

var projectionOrtho = d3
  .geoOrthographic()
  .rotate([-160, 0, -23.4])
  .fitSize([canvasOrthoWidth, canvasOrthoHeight], {
    type: "Sphere"
  });

var geoPathGeneratorOrtho = d3
  .geoPath()
  .projection(projectionOrtho)
  .context(ctxOrtho);

var selectElement = document.querySelector("select#projectionChoice");
selectElement.addEventListener("change", function(evt) {
  changeProjection(evt.target.value);
});

var axialTilt = -23.4;

var axialTiltElement = document.querySelector("input#axialTilt");
axialTiltElement.addEventListener("change", function(evt) {
  changeAxialTilt(evt.target.checked);
});

var geoCircles = {
  type: "GeometryCollection",
  geometries: []
};

for (var longitude = -180; longitude < 180; longitude += 15) {
  for (var latitude = -90; latitude <= 90; latitude += 15) {
    geoCircles.geometries.push(
      d3
        .geoCircle()
        .center([longitude, latitude])
        .radius(1.333)()
    );
  }
}

drawSphereAndCircles(geoCircles);

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.1);
    var worldGeoJson = topojson.feature(
      topology,
      topology.objects.land
    );

    d3.timer(function(elapsed) {
      geoPathGeneratorAny
        .projection()
        .rotate([elapsed * 0.0075 - 160, 0, axialTilt]);

      geoPathGeneratorOrtho
        .projection()
        .rotate([elapsed * 0.0075 - 160, 0, axialTilt]);

      ctxOrtho.clearRect(0, 0, canvasOrthoWidth, canvasOrthoHeight);
      ctxAny.clearRect(0, 0, canvasAnyWidth, canvasAnyHeight);

      drawWorld(worldGeoJson);

      drawSphereAndCircles(geoCircles);
    });

    var totalOptions = selectElement.options.length;
    var intervalTimer = d3.interval(function() {
      if (selectElement.selectedIndex === totalOptions - 1) {
        selectElement.selectedIndex = 1;
      } else {
        selectElement.selectedIndex++;
      }
      selectElement.dispatchEvent(new Event("change"));
    }, 2500);

    selectElement.addEventListener("focus", function() {
      intervalTimer.stop();
    });
  });

function drawWorld(worldGeoJson) {
  ctxOrtho.beginPath();
  ctxOrtho.fillStyle = "#b2df8a";
  geoPathGeneratorOrtho(worldGeoJson);
  ctxOrtho.fill();
  ctxOrtho.closePath();

  ctxAny.beginPath();
  ctxAny.fillStyle = "#b2df8a";
  geoPathGeneratorAny(worldGeoJson);
  ctxAny.fill();
  ctxAny.closePath();
}

function drawSphereAndCircles(geoCircles) {
  ctxOrtho.beginPath();
  ctxOrtho.fillStyle = "#1f78b4";
  geoPathGeneratorOrtho(geoCircles);
  ctxOrtho.fill();
  ctxOrtho.closePath();

  ctxOrtho.beginPath();
  ctxOrtho.strokeStyle = "#1f78b4";
  geoPathGeneratorOrtho({ type: "Sphere" });
  ctxOrtho.stroke();
  ctxOrtho.closePath();

  ctxAny.beginPath();
  ctxAny.fillStyle = "#1f78b4";
  geoPathGeneratorAny(geoCircles);
  ctxAny.fill();
  ctxAny.closePath();

  ctxAny.beginPath();
  ctxAny.strokeStyle = "#1f78b4";
  geoPathGeneratorAny({ type: "Sphere" });
  ctxAny.stroke();
  ctxAny.closePath();
}

function changeProjection(d3ProjectionName) {
  var newProjection = d3[d3ProjectionName]().fitSize(
    [canvasAnyWidth, canvasAnyHeight],
    {
      type: "Sphere"
    }
  );

  geoPathGeneratorAny.projection(newProjection);
}

function changeAxialTilt(checked) {
  var newAxialTilt = checked ? -23.4 + 0 : 0;

  d3
    .select({})
    .transition()
    .duration(1250)
    .ease(d3.easeBackOut)
    .tween(null, function() {
      var interpolator = d3.interpolate(axialTilt, newAxialTilt);
      return function(t) {
        axialTilt = interpolator(t);
      };
    });
}

              
            
!
999px

Console