A spinning globe.
Last active
March 17, 2019 18:07
-
-
Save sarah37/6296052a62ea8ac03140982ffd42b117 to your computer and use it in GitHub Desktop.
Spinning globe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| redirect: https://observablehq.com/@sarah37/spinning-globe |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // width and height | |
| var w = window.innerWidth; | |
| var h = window.innerHeight-60; | |
| var scl = Math.min(w, h)/2.5; // scale globe | |
| var tRotation = 30000; //30s per rotation | |
| // pause button | |
| var rotationOn = true; | |
| d3.select("#pauseButton").on("click", function() { | |
| rotationOn = !rotationOn; | |
| d3.select("#pauseButton").text(rotationOn ? "pause" : "play") | |
| }); | |
| // map projection | |
| var projection = d3.geoOrthographic() | |
| .scale(scl) | |
| .translate([ w/2, h/2 ]); | |
| // path generator | |
| var path = d3.geoPath() | |
| .projection(projection); | |
| // create SVG element | |
| var svg = d3.select("#svgDiv") | |
| .append("svg") | |
| .attr("width", w) | |
| .attr("height", h) | |
| // add circle for background | |
| var bgCircle = svg.append("circle") | |
| .attr("cx", w/2) | |
| .attr("cy", h/2) | |
| .attr("r", projection.scale()) | |
| .classed("ocean", true); | |
| // load TopoJSON data | |
| d3.json("https://gist.githubusercontent.com/sarah37/dcca42b936545d9ee9f0bc8052e03dbd/raw/550cfee8177df10e515d82f7eb80bce4f72c52de/world-110m.json", function(json) { | |
| // bind data, create one path per TopoJSON feature | |
| svg.append("path") | |
| .datum(topojson.feature(json, json.objects.countries)) | |
| .attr("d", path) | |
| .classed("land", true); | |
| }); | |
| // vars for timer | |
| var tNew, dt, steps, pos, tOld, oldPos; | |
| tOld = 0; | |
| oldPos = 0; | |
| // start timer | |
| d3.timer(myTimer); | |
| // function that rotates the earth | |
| function myTimer(now) { | |
| if (rotationOn) { | |
| tNew = now; | |
| dt = tOld - tNew; | |
| steps = dt * 360 / tRotation; | |
| pos = oldPos - steps //the earth rotates towards the east | |
| if (pos <= -180) {pos = pos+360} | |
| projection.rotate([pos, 0]); | |
| svg.selectAll("path").attr("d", path) | |
| tOld = tNew; | |
| oldPos = pos; | |
| } | |
| else { | |
| tOld = now; | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Spinning Globe</title> | |
| <link rel="stylesheet" type="text/css" href="style.css"> | |
| <script src="https://d3js.org/d3.v4.min.js"></script> | |
| <script src="https://d3js.org/topojson.v1.min.js"></script> | |
| </head> | |
| <body> | |
| <div id="svgDiv"></div> | |
| <a id="pauseButton">pause</a> | |
| <script type="text/javascript" src="globe.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| body, html { | |
| text-align: center; | |
| margin: 0; | |
| padding: 0; | |
| overflow: hidden; | |
| } | |
| #svgDiv { | |
| display: inline-block; | |
| overflow: hidden; | |
| } | |
| .ocean { | |
| fill: #bfd7e4; | |
| } | |
| .land { | |
| fill: #eaeaea; | |
| stroke: #ccc; | |
| stroke-width: 0.3px; | |
| } | |
| /*button*/ | |
| #pauseButton { | |
| text-transform: uppercase; | |
| font-family: sans-serif; | |
| line-height: 60px; | |
| color: #000; | |
| display: inline-block; | |
| border: none; | |
| cursor: pointer; | |
| } | |
| #pauseButton:hover { | |
| transition: text-decoration 0.2s; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment