Render PNG markers on a rotating canvas globe with d3.
forked from mbostock's block: This Is a Globe
forked from Fil's block: This Is a Globe in d3.v4
forked from Fil's block: Why Reykjavík?
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<div class="image" style="display:none;">
<img id="source" src="https://s3-us-west-2.amazonaws.com/canopyplanet/assets/canopy_map_marker.png" width="40" height="40">
</div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script>
var width = 960,
height = 600;
var radius = height / 2 - 5,
scale = radius,
velocity = .02;
var projection = d3.geoOrthographic()
.translate([width / 2, height / 2])
.scale(scale)
.clipAngle(90);
var canvas = d3.select("body").append("canvas")
.attr("width", width)
.attr("height", height);
var context = canvas.node().getContext("2d");
var path = d3.geoPath()
.projection(projection)
.context(context);
var zoom = d3
.zoom()
.on("zoom start end", zoomed)
.scaleExtent([0.7, 5])
.translateExtent([[0,0], [width, height]]);
canvas.call(zoom);
var init_scale = projection.scale(),
init_translate = projection.translate();
function zoomed() {
var t = d3.event.transform;
projection.scale(init_scale * t.k)
.translate(t.apply(init_translate));
}
d3.json("https://gist.githubusercontent.com/mbostock/4090846/raw/d534aba169207548a8a3d670c9c2cc719ff05c47/world-110m.json", function(error, world) {
if (error) throw error;
var land = topojson.feature(world, world.objects.land);
var places = {
"type": "FeatureCollection",
"bbox": [-126.507568, 3.590178, 98.217773, 51.481383],
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [98.217773, 3.590178]
},
"properties": {
"name": "Leuser Ecosystem"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-76.025391, 50.303376]
},
"properties": {
"name": "Broadback Forest"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-126.507568, 51.481383]
},
"properties": {
"name": "Great Bear Rainforest"
}
}]
};
var a = places.features[0].geometry.coordinates;
var b = places.features[1].geometry.coordinates;
var c = places.features[2].geometry.coordinates;
var image = document.getElementById('source');
d3.timer(function(elapsed) {
context.clearRect(0, 0, width, height);
projection.rotate([velocity * elapsed, -40]);
context.beginPath();
path(land);
context.fillStyle = '#333';
context.fill();
context.strokeStyle = '#333';
context.beginPath();
path({type:"Sphere"});
context.lineWidth = 2.5;
context.stroke();
a = path.centroid(places.features[0]);
b = path.centroid(places.features[1]);
c = path.centroid(places.features[2]);
context.drawImage(image, a[0] - 20, a[1] - 40, 40, 40);
context.drawImage(image, b[0] - 20, b[1] - 40, 40, 40);
context.drawImage(image, c[0] - 20, c[1] - 40, 40, 40);
});
});
d3.select(self.frameElement).style("height", height + "px");
</script>
</body>
https://d3js.org/d3.v4.min.js
https://d3js.org/d3.v4.min.js
https://d3js.org/topojson.v2.min.js