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

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