Skip to content

Instantly share code, notes, and snippets.

@flyinactor91
Last active February 2, 2016 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flyinactor91/80b1a540aa9d7a55f3b8 to your computer and use it in GitHub Desktop.
Save flyinactor91/80b1a540aa9d7a55f3b8 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
fill: none;
stroke-linejoin: round;
}
.sphere,
.graticule {
stroke: #aaa;
}
.orbit {
stroke: red;
stroke-width: 2px;
}
.equator {
stroke: blue;
stroke-width: 2px;
stroke-dasharray: 15;
}
.start {
stroke: black;
fill: #0a0;
stroke-width: 1px;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = interpolatedProjection(
d3.geo.orthographic()
.rotate([30, -10])
.center([-30, 10])
.scale(240)
.translate([width / 2, height / 2]),
d3.geo.equirectangular()
.scale(145)
.translate([width / 2, height / 2]));
var path = d3.geo.path()
.projection(projection);
var graticule = d3.geo.graticule();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("path")
.datum({type: "Sphere"})
.attr("class", "sphere")
.attr("d", path);
svg.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path);
//The launch latitude
var start = 30;
//The launch direction
var degree = 60;
//The degree as a percentage from vertical
var degPerc = Math.abs(degree) / 90;
//Whether the launch is oriented North or South
var direction = (degree >= 0) ? 1 : -1;
//The latitude encountered after a quarter orbit
var mid = (start + ((90-start) * 2 * degPerc)) * degPerc * direction;
console.log(mid, degPerc, direction);
//Draw the orbital path
svg.append("path")
.datum({type: "LineString", coordinates: [[-180, -start], [-90, -mid], [0, start], [90, mid], [180, -start]]})
.attr("class", "orbit")
.attr("d", path);
//Draw the equator
svg.append("path")
.datum({type: "LineString", coordinates: [[-180, 0], [-90, 0], [0, 0], [90, 0], [180, 0]]})
.attr("class", "equator")
.attr("d", path);
//Draw the longitude line of the starting point
svg.append("path")
.datum({type: "LineString", coordinates: [[0, -90], [0, 0], [0, 90]]})
.attr("class", "equator")
.attr("d", path);
//Draw the starting point
svg.append("path")
.datum({type: "MultiPoint", coordinates: [[0, start]]})
.attr("class", "start")
.attr("d", path);
var feature = svg.selectAll("path");
animation();
function animation() {
svg.transition()
.duration(7500)
.tween("projection", function() {
return function(_) {
projection.alpha(_);
feature.attr("d", path);
};
})
.transition()
.duration(2500)
//.each("end", animation);
}
function interpolatedProjection(a, b) {
var projection = d3.geo.projection(raw).scale(1),
center = projection.center,
translate = projection.translate,
α;
function raw(λ, φ) {
var pa = a([λ *= 180 / Math.PI, φ *= 180 / Math.PI]), pb = b([λ, φ]);
return [(1 - α) * pa[0] + α * pb[0], (α - 1) * pa[1] - α * pb[1]];
}
projection.alpha = function(_) {
if (!arguments.length) return α;
α = +_;
var ca = a.center(), cb = b.center(),
ta = a.translate(), tb = b.translate();
center([(1 - α) * ca[0] + α * cb[0], (1 - α) * ca[1] + α * cb[1]]);
translate([(1 - α) * ta[0] + α * tb[0], (1 - α) * ta[1] + α * tb[1]]);
return projection;
};
delete projection.scale;
delete projection.translate;
delete projection.center;
return projection.alpha(0);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment