d3-geo-zoom
Version:
Zoom and Pan D3 Geo projections
54 lines (40 loc) • 1.33 kB
HTML
<head>
<script src="//cdn.jsdelivr.net/npm/d3"></script>
<script src="//cdn.jsdelivr.net/npm/topojson"></script>
<script src="//cdn.jsdelivr.net/npm/d3-geo-zoom"></script>
<!--<script src="../../dist/d3-geo-zoom.js"></script>-->
<style>body { margin: 0 }</style>
</head>
<body>
<div id="world"></div>
<script>
const MARGIN = 5;
const width = window.innerWidth;
const height = window.innerHeight;
const svg = d3.select('#world').append('svg')
.attr('width', width)
.attr('height', height);
const projection = d3.geoOrthographic()
.scale((Math.min(width, height)) / 2 - MARGIN)
.translate([width / 2, height / 2]);
const path = d3.geoPath()
.projection(projection);
d3.geoZoom()
.projection(projection)
.onMove(render)
(svg.node());
fetch('//unpkg.com/world-atlas@1/world/110m.json').then(r => r.json()).then(world => {
svg.append('path').attr('class', 'geo-feature')
.datum({ type: 'Sphere' })
.attr('fill', 'aqua');
svg.append('path').attr('class', 'geo-feature')
.datum(topojson.feature(world, world.objects.land))
.attr('fill', 'maroon');
render();
});
//
function render() {
svg.selectAll('path.geo-feature').attr('d', path);
}
</script>
</body>