align-html-elements-to-3d-globe-too-many-labels.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Align HTML Elements to 3D Globe</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. font-family: sans-serif;
  13. }
  14. #c {
  15. width: 100%; /* let our container decide our size */
  16. height: 100%;
  17. display: block;
  18. }
  19. #container {
  20. position: relative; /* makes this the origin of its children */
  21. width: 100%;
  22. height: 100%;
  23. overflow: hidden;
  24. }
  25. #labels {
  26. position: absolute; /* let us position ourself inside the container */
  27. z-index: 0; /* make a new stacking context so children don't sort with rest of page */
  28. left: 0; /* make our position the top left of the container */
  29. top: 0;
  30. color: white;
  31. }
  32. #labels>div {
  33. position: absolute; /* let us position them inside the container */
  34. left: 0; /* make their default position the top left of the container */
  35. top: 0;
  36. cursor: pointer; /* change the cursor to a hand when over us */
  37. font-size: small;
  38. user-select: none; /* don't let the text get selected */
  39. pointer-events: none; /* make us invisible to the pointer */
  40. text-shadow: /* create a black outline */
  41. -1px -1px 0 #000,
  42. 0 -1px 0 #000,
  43. 1px -1px 0 #000,
  44. 1px 0 0 #000,
  45. 1px 1px 0 #000,
  46. 0 1px 0 #000,
  47. -1px 1px 0 #000,
  48. -1px 0 0 #000;
  49. }
  50. #labels>div:hover {
  51. color: red;
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <div id="container">
  57. <canvas id="c"></canvas>
  58. <div id="labels"></div>
  59. </div>
  60. </body>
  61. <script type="module">
  62. import * as THREE from '../../build/three.module.js';
  63. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  64. function main() {
  65. const canvas = document.querySelector('#c');
  66. const renderer = new THREE.WebGLRenderer({canvas});
  67. const fov = 60;
  68. const aspect = 2; // the canvas default
  69. const near = 0.1;
  70. const far = 10;
  71. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  72. camera.position.z = 2.5;
  73. const controls = new OrbitControls(camera, canvas);
  74. controls.enableDamping = true;
  75. controls.enablePan = false;
  76. controls.minDistance = 1.2;
  77. controls.maxDistance = 4;
  78. controls.update();
  79. const scene = new THREE.Scene();
  80. scene.background = new THREE.Color('#236');
  81. {
  82. const loader = new THREE.TextureLoader();
  83. const texture = loader.load('resources/data/world/country-outlines-4k.png', render);
  84. const geometry = new THREE.SphereGeometry(1, 64, 32);
  85. const material = new THREE.MeshBasicMaterial({map: texture});
  86. scene.add(new THREE.Mesh(geometry, material));
  87. }
  88. async function loadJSON(url) {
  89. const req = await fetch(url);
  90. return req.json();
  91. }
  92. let countryInfos;
  93. async function loadCountryData() {
  94. countryInfos = await loadJSON('resources/data/world/country-info.json'); /* threejs.org: url */
  95. const lonFudge = Math.PI * 1.5;
  96. const latFudge = Math.PI;
  97. // these helpers will make it easy to position the boxes
  98. // We can rotate the lon helper on its Y axis to the longitude
  99. const lonHelper = new THREE.Object3D();
  100. // We rotate the latHelper on its X axis to the latitude
  101. const latHelper = new THREE.Object3D();
  102. lonHelper.add(latHelper);
  103. // The position helper moves the object to the edge of the sphere
  104. const positionHelper = new THREE.Object3D();
  105. positionHelper.position.z = 1;
  106. latHelper.add(positionHelper);
  107. const labelParentElem = document.querySelector('#labels');
  108. for (const countryInfo of countryInfos) {
  109. const {lat, lon, name} = countryInfo;
  110. // adjust the helpers to point to the latitude and longitude
  111. lonHelper.rotation.y = THREE.MathUtils.degToRad(lon) + lonFudge;
  112. latHelper.rotation.x = THREE.MathUtils.degToRad(lat) + latFudge;
  113. // get the position of the lat/lon
  114. positionHelper.updateWorldMatrix(true, false);
  115. const position = new THREE.Vector3();
  116. positionHelper.getWorldPosition(position);
  117. countryInfo.position = position;
  118. // add an element for each country
  119. const elem = document.createElement('div');
  120. elem.textContent = name;
  121. labelParentElem.appendChild(elem);
  122. countryInfo.elem = elem;
  123. }
  124. requestRenderIfNotRequested();
  125. }
  126. loadCountryData();
  127. const tempV = new THREE.Vector3();
  128. function updateLabels() {
  129. // exit if we have not yet loaded the JSON file
  130. if (!countryInfos) {
  131. return;
  132. }
  133. for (const countryInfo of countryInfos) {
  134. const {position, elem} = countryInfo;
  135. // get the normalized screen coordinate of that position
  136. // x and y will be in the -1 to +1 range with x = -1 being
  137. // on the left and y = -1 being on the bottom
  138. tempV.copy(position);
  139. tempV.project(camera);
  140. // convert the normalized position to CSS coordinates
  141. const x = (tempV.x * .5 + .5) * canvas.clientWidth;
  142. const y = (tempV.y * -.5 + .5) * canvas.clientHeight;
  143. // move the elem to that position
  144. elem.style.transform = `translate(-50%, -50%) translate(${x}px,${y}px)`;
  145. // set the zIndex for sorting
  146. elem.style.zIndex = (-tempV.z * .5 + .5) * 100000 | 0;
  147. }
  148. }
  149. function resizeRendererToDisplaySize(renderer) {
  150. const canvas = renderer.domElement;
  151. const width = canvas.clientWidth;
  152. const height = canvas.clientHeight;
  153. const needResize = canvas.width !== width || canvas.height !== height;
  154. if (needResize) {
  155. renderer.setSize(width, height, false);
  156. }
  157. return needResize;
  158. }
  159. let renderRequested = false;
  160. function render() {
  161. renderRequested = undefined;
  162. if (resizeRendererToDisplaySize(renderer)) {
  163. const canvas = renderer.domElement;
  164. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  165. camera.updateProjectionMatrix();
  166. }
  167. controls.update();
  168. updateLabels();
  169. renderer.render(scene, camera);
  170. }
  171. render();
  172. function requestRenderIfNotRequested() {
  173. if (!renderRequested) {
  174. renderRequested = true;
  175. requestAnimationFrame(render);
  176. }
  177. }
  178. controls.addEventListener('change', requestRenderIfNotRequested);
  179. window.addEventListener('resize', requestRenderIfNotRequested);
  180. }
  181. main();
  182. </script>
  183. </html>