Skip to content

Instantly share code, notes, and snippets.

@earthjs
Last active October 1, 2017 20:47
Show Gist options
  • Save earthjs/57a52c45ddcfb9bc284d729aa35fcc97 to your computer and use it in GitHub Desktop.
Save earthjs/57a52c45ddcfb9bc284d729aa35fcc97 to your computer and use it in GitHub Desktop.
Orthographic Zoom
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().filter(_ => {
const {touches} = d3.event;
return (touches===undefined || touches.length===1)
}).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
Loading
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