index.html
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<style>
html, body {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
}
svg {
width: 99%;
height: 99%;
}
</style>
</head>
<body>
<svg></svg>
<script src="//unpkg.com/babel-standalone@6.26.0/babel.min.js"></script>
<script src="//unpkg.com/d3@4.12.2/build/d3.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/topojson/3.0.0/topojson.min.js"></script>
<script type="text/babel">
const stage = d3.select("svg");
const seaLayer = stage.append("g");
const landLayer = stage.append("g");
const projection = d3.geoOrthographic()
const path = d3.geoPath()
d3.json("conuntries.topojson", function(json){
const data = topojson.feature(json, json.objects.conuntries).features;
projection
.rotate([0,0,0])
.clipAngle(90);
path.projection(projection);
seaLayer.append("path")
.datum({type: "Sphere"})
.attr("fill", "blue")
landLayer
.selectAll("path")
.data(data)
.enter()
.append("path")
.style("background-color", "blue")
.attr("fill", "green")
.attr("stroke", "#222")
.on("mouseover", function(){
d3.select(this).attr("fill", "red");
})
.on("mouseout", function(){
d3.select(this).transition().duration(500).attr("fill", "green");
})
});
const draw = () => {
let i = 0;
return () => {
const w = stage.node().clientWidth;
const h = stage.node().clientHeight;
i = i+0.5;
projection
.rotate([i,0,0])
.scale(d3.min([w, h]) / 2)
.translate([w/2, h/2])
path.projection(projection);
seaLayer.select("path").attr("d", path);
landLayer.selectAll("path").attr("d", path);
}
}
setInterval(draw(), 100);
</script>
</body>
</html>