Skip to content

Instantly share code, notes, and snippets.

@vitohuang
Last active January 4, 2017 09:25
Show Gist options
  • Save vitohuang/a10e82eba30b357d6d4b4c58ba57f209 to your computer and use it in GitHub Desktop.
Save vitohuang/a10e82eba30b357d6d4b4c58ba57f209 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>route</title>
<script src="//d3js.org/queue.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
</head>
<body>
<canvas id="globe" width='960' height='600'/>
<script>
var width = 960,
height = 600;
var radius = height / 2 - 5,
scale = radius,
sens = 0.25;
var projection = d3.geo.orthographic()
.translate([width / 2, height / 2])
.scale(scale)
.clipAngle(90);
var canvas = document.getElementById('globe');
var context = canvas.getContext("2d");
var path = d3.geo.path()
.projection(projection)
.context(context);
d3.select(canvas)
.call(d3.behavior.drag()
.origin(function() {
var r = projection.rotate();
return {
x: r[0] / sens,
y: -r[1] / sens
};
})
.on('drag', function() {
var lng = -d3.event.x * sens;
var lat = d3.event.y * sens;
// Disallow rotation beyond the poles.
lat = lat > 89 ? 89 : lat < -89 ? -89 : lat;
projection.rotate([-lng, -lat]);
})
);
//var route = {"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"LineString","coordinates":[[2.548828125,50.28933925329178],[4.482421875,47.989921667414194],[7.294921874999999,46.92025531537451],[11.25,46.73986059969267],[15.380859374999998,46.37725420510028],[17.578125,44.653024159812],[18.544921875,43.068887774169625],[19.951171875,41.11246878918088],[22.587890625,41.57436130598913],[26.015625,41.44272637767212],[27.773437499999996,39.842286020743394],[31.289062500000004,39.90973623453719],[33.134765625,40.58058466412761],[34.892578125,41.244772343082076],[37.44140625,40.3130432088809],[41.30859375,40.111688665595956],[43.681640625,41.31082388091818],[46.845703125,40.58058466412761],[47.8125,38.75408327579141],[48.603515625,40.58058466412761],[55.107421875,44.653024159812],[57.216796875,44.84029065139799],[58.97460937499999,43.004647127794435],[61.34765625,41.902277040963696],[63.6328125,40.91351257612758],[66.005859375,39.436192999314095],[68.203125,39.842286020743394],[69.2578125,40.97989806962013],[71.71875,40.91351257612758],[72.421875,40.04443758460856],[74.794921875,40.713955826286046],[75.41015624999999,39.70718665682654],[78.3984375,38.685509760012],[83.3203125,38.54816542304656],[88.24218749999999,38.34165619279595],[90.966796875,38.41055825094609],[91.0546875,42.94033923363181],[94.39453125,42.87596410238256],[96.591796875,41.902277040963696],[97.734375,40.44694705960048],[97.998046875,38.47939467327645],[99.31640625,36.527294814546245],[100.72265625,34.95799531086792],[104.150390625,30.44867367928756],[108.6328125,27.059125784374068],[114.873046875,22.350075806124867]]}}]};
// Get data
queue()
.defer(d3.json, "/mbostock/raw/4090846/world-50m.json")
.defer(d3.json, "/vitohuang/raw/a10e82eba30b357d6d4b4c58ba57f209/9d29ac67ad884987df09797a68ecb35ea47404e1/route.geojson")
.await(ready);
function ready(error, world, route) {
if (error) throw error;
var land = topojson.feature(world, world.objects.land);
d3.timer(function(elapsed) {
context.clearRect(0, 0, width, height);
// Draw the land
context.fillStyle = '#ccc';
context.strokeStyle = '#ccc';
context.beginPath();
path(land);
context.fill();
// the route
context.fillStyle = '#000';
context.strokeStyle = '#000';
context.beginPath();
path(route);
context.fill();
context.beginPath();
context.arc(width / 2, height / 2, radius, 0, 2 * Math.PI, true);
context.lineWidth = 2.5;
context.stroke();
});
};
projection.rotate([-50, -30]);
d3.select(self.frameElement).style("height", height + "px");
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment