align-html-to-3d.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  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: 100%;
  21. height: 100%;
  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: large;
  36. user-select: none; /* don't let the text get selected */
  37. text-shadow: /* create a black outline */
  38. -1px -1px 0 #000,
  39. 0 -1px 0 #000,
  40. 1px -1px 0 #000,
  41. 1px 0 0 #000,
  42. 1px 1px 0 #000,
  43. 0 1px 0 #000,
  44. -1px 1px 0 #000,
  45. -1px 0 0 #000;
  46. }
  47. #labels>div:hover {
  48. color: red;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <div id="container">
  54. <canvas id="c"></canvas>
  55. <div id="labels"></div>
  56. </div>
  57. </body>
  58. <!-- Import maps polyfill -->
  59. <!-- Remove this when import maps will be widely supported -->
  60. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  61. <script type="importmap">
  62. {
  63. "imports": {
  64. "three": "../../build/three.module.js",
  65. "three/addons/": "../../examples/jsm/"
  66. }
  67. }
  68. </script>
  69. <script type="module">
  70. import * as THREE from 'three';
  71. import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
  72. function main() {
  73. const canvas = document.querySelector('#c');
  74. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  75. const fov = 75;
  76. const aspect = 2; // the canvas default
  77. const near = 1.1;
  78. const far = 50;
  79. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  80. camera.position.z = 7;
  81. const controls = new OrbitControls(camera, canvas);
  82. controls.target.set(0, 0, 0);
  83. controls.update();
  84. const scene = new THREE.Scene();
  85. {
  86. const color = 0xFFFFFF;
  87. const intensity = 1;
  88. const light = new THREE.DirectionalLight(color, intensity);
  89. light.position.set(-1, 2, 4);
  90. scene.add(light);
  91. }
  92. const boxWidth = 1;
  93. const boxHeight = 1;
  94. const boxDepth = 1;
  95. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  96. const labelContainerElem = document.querySelector('#labels');
  97. function makeInstance(geometry, color, x, name) {
  98. const material = new THREE.MeshPhongMaterial({color});
  99. const cube = new THREE.Mesh(geometry, material);
  100. scene.add(cube);
  101. cube.position.x = x;
  102. const elem = document.createElement('div');
  103. elem.textContent = name;
  104. labelContainerElem.appendChild(elem);
  105. return {cube, elem};
  106. }
  107. const cubes = [
  108. makeInstance(geometry, 0x44aa88, 0, 'Aqua'),
  109. makeInstance(geometry, 0x8844aa, -2, 'Purple'),
  110. makeInstance(geometry, 0xaa8844, 2, 'Gold'),
  111. ];
  112. function resizeRendererToDisplaySize(renderer) {
  113. const canvas = renderer.domElement;
  114. const width = canvas.clientWidth;
  115. const height = canvas.clientHeight;
  116. const needResize = canvas.width !== width || canvas.height !== height;
  117. if (needResize) {
  118. renderer.setSize(width, height, false);
  119. }
  120. return needResize;
  121. }
  122. const tempV = new THREE.Vector3();
  123. function render(time) {
  124. time *= 0.001;
  125. if (resizeRendererToDisplaySize(renderer)) {
  126. const canvas = renderer.domElement;
  127. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  128. camera.updateProjectionMatrix();
  129. }
  130. cubes.forEach((cubeInfo, ndx) => {
  131. const {cube, elem} = cubeInfo;
  132. const speed = 1 + ndx * .1;
  133. const rot = time * speed;
  134. cube.rotation.x = rot;
  135. cube.rotation.y = rot;
  136. // get the position of the center of the cube
  137. cube.updateWorldMatrix(true, false);
  138. cube.getWorldPosition(tempV);
  139. // get the normalized screen coordinate of that position
  140. // x and y will be in the -1 to +1 range with x = -1 being
  141. // on the left and y = -1 being on the bottom
  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. renderer.render(scene, camera);
  150. requestAnimationFrame(render);
  151. }
  152. requestAnimationFrame(render);
  153. }
  154. main();
  155. </script>
  156. </html>