-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathglobe.js
108 lines (87 loc) · 3.1 KB
/
globe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var globe = planetaryjs.planet();
// Load our custom `autorotate` plugin; see below.
globe.loadPlugin(autorotate(1));
// The `earth` plugin draws the oceans and the land
globe.loadPlugin(planetaryjs.plugins.earth({
topojson: { file: './world-110m.json' },
oceans: { fill: '#94b6e4' },
land: { fill: '#aae57e' },
borders: { stroke: '#aae57e' }
}));
// The `pings` plugin draws animated pings on the globe.
globe.loadPlugin(planetaryjs.plugins.pings());
// The `drag` plugin enables manipulating the globe with the mouse
globe.loadPlugin(planetaryjs.plugins.drag({
// Dragging the globe should pause the automatic rotation until we release the mouse
onDragStart: function () {
this.plugins.autorotate.pause();
},
onDragEnd: function () {
this.plugins.autorotate.resume();
}
}));
// Set initial globe rotation
globe.projection.rotate([-20, -20, 0]);
// Zeichne die Welt!
var canvas = document.getElementById('rotatingGlobe');
globe.draw(canvas);
var dpr = window.devicePixelRatio;
var width = window.innerWidth;
var height = window.innerHeight;
// Set initial globe size on page load
document.addEventListener("DOMContentLoaded", function() {
// Grab new window dimensions
width = window.innerWidth;
height = window.innerHeight;
// Update canvas dimensions
canvas.width = width * dpr;
canvas.height = height * dpr;
canvas.style.width = width + "px";
canvas.style.height = height + "px";
var context = canvas.getContext('2d');
context.scale(dpr, dpr);
// Update globe's translation and scale
globe.projection.translate([width / 2, height / 2]);
globe.projection.scale((width * dpr) / 2.4);
}, false);
// Listen for window resize events
// Same code as function above
window.addEventListener('resize', () => {
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width * dpr;
canvas.height = height * dpr;
canvas.style.width = width + "px";
canvas.style.height = height + "px";
var context = canvas.getContext('2d');
context.scale(dpr, dpr);
globe.projection.translate([width / 2, height / 2]);
globe.projection.scale((width * dpr) / 2.4);
}, {passive: false});
// This plugin will automatically rotate the globe around its vertical axis
function autorotate(degPerSec) {
// Planetary.js plugins are functions that take a `planet` instance as an argument...
return function (planet) {
var lastTick = null;
var paused = false;
planet.plugins.autorotate = {
pause: function () { paused = true; },
resume: function () { paused = false; }
};
// ...and configure hooks into certain pieces of its lifecycle
planet.onDraw(function () {
if (paused || !lastTick) {
lastTick = new Date();
} else {
var now = new Date();
var delta = now - lastTick;
// This plugin uses the built-in projection (provided by D3) to rotate the globe each time we draw iy
var rotation = planet.projection.rotate();
rotation[0] += degPerSec * delta / 1000;
if (rotation[0] >= 180) rotation[0] -= 360;
planet.projection.rotate(rotation);
lastTick = now;
}
});
};
};