An attempt at panning and zooming with Orthographic projection.
forked from curran's block: Orthographic Zoom
license: mit |
An attempt at panning and zooming with Orthographic projection.
forked from curran's block: Orthographic Zoom
<!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> |