-
Notifications
You must be signed in to change notification settings - Fork 146
/
index.html
executable file
·93 lines (80 loc) · 3.12 KB
/
index.html
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
<head>
<style> body { margin: 0; } </style>
<script type="importmap">{ "imports": {
"three": "//unpkg.com/three/build/three.module.js",
"three/addons/": "//unpkg.com/three/examples/jsm/"
}}</script>
<script type="module">
import * as THREE from 'three';
window.THREE = THREE;
</script>
<script src="//unpkg.com/three-globe" defer></script>
<!-- <script src="../../dist/three-globe.js" defer></script>-->
</head>
<body>
<div id="globeViz"></div>
<script type="module">
import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
import { CSS2DRenderer } from 'three/addons/renderers/CSS2DRenderer.js';
const markerSvg = `<svg viewBox="-4 0 36 36">
<path fill="currentColor" d="M14,0 C21.732,0 28,5.641 28,12.6 C28,23.963 14,36 14,36 C14,36 0,24.064 0,12.6 C0,5.641 6.268,0 14,0 Z"></path>
<circle fill="black" cx="14" cy="14" r="7"></circle>
</svg>`;
// Gen random data
const N = 30;
const gData = [...Array(N).keys()].map(() => ({
lat: (Math.random() - 0.5) * 180,
lng: (Math.random() - 0.5) * 360,
size: 7 + Math.random() * 30,
color: ['red', 'white', 'blue', 'green'][Math.round(Math.random() * 3)]
}));
const Globe = new ThreeGlobe()
.globeImageUrl('//unpkg.com/three-globe/example/img/earth-dark.jpg')
.bumpImageUrl('//unpkg.com/three-globe/example/img/earth-topology.png')
.htmlElementsData(gData)
.htmlElement(d => {
const el = document.createElement('div');
el.innerHTML = markerSvg;
el.style.color = d.color;
el.style.width = `${d.size}px`;
return el;
});
// Setup renderers
const renderers = [new THREE.WebGLRenderer(), new CSS2DRenderer()];
renderers.forEach((r, idx) => {
r.setSize(window.innerWidth, window.innerHeight);
if (idx > 0) {
// overlay additional on top of main renderer
r.domElement.style.position = 'absolute';
r.domElement.style.top = '0px';
r.domElement.style.pointerEvents = 'none';
}
document.getElementById('globeViz').appendChild(r.domElement);
});
// Setup scene
const scene = new THREE.Scene();
scene.add(Globe);
scene.add(new THREE.AmbientLight(0xcccccc, Math.PI));
scene.add(new THREE.DirectionalLight(0xffffff, 0.6 * Math.PI));
// Setup camera
const camera = new THREE.PerspectiveCamera();
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
camera.position.z = 500;
// Add camera controls
const tbControls = new TrackballControls(camera, renderers[0].domElement);
tbControls.minDistance = 101;
tbControls.rotateSpeed = 5;
tbControls.zoomSpeed = 0.8;
// Update pov when camera moves
Globe.setPointOfView(camera.position, Globe.position);
tbControls.addEventListener('change', () => Globe.setPointOfView(camera.position, Globe.position));
// Kick-off renderers
(function animate() { // IIFE
// Frame cycle
tbControls.update();
renderers.forEach(r => r.render(scene, camera));
requestAnimationFrame(animate);
})();
</script>
</body>