Skip to content

Instantly share code, notes, and snippets.

@apratt2003
Last active October 30, 2017 19:08
Show Gist options
  • Save apratt2003/be9acb2fd1f8254befd503bd70801bd3 to your computer and use it in GitHub Desktop.
Save apratt2003/be9acb2fd1f8254befd503bd70801bd3 to your computer and use it in GitHub Desktop.
Orthographic Globe
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
</head>
<body>
<svg width="960" height="500"></svg>
<script>
const svg = d3.select('svg');
const path = svg.append('path');
const projection = d3.geoOrthographic();
const initialScale = projection.scale();
const geoPath = d3.geoPath().projection(projection);
const sensitivity = 58;
d3.json('world-110m.json', (error, world) => {
const land = topojson.feature(world, world.objects.land);
const render = () => path.attr('d', geoPath(land));
render();
svg
.call(d3.drag().on('drag', () => {
const rotate = projection.rotate();
const k = sensitivity / projection.scale();
projection.rotate([
rotate[0] + d3.event.dx * k,
rotate[1] - d3.event.dy * k
])
render();
}))
.call(d3.zoom().on('zoom', () => {
projection.scale(initialScale * d3.event.transform.k);
render();
}));
});
</script>
</body>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment