threejs-align-html-to-3d.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. body {
  10. margin: 0;
  11. }
  12. #c {
  13. width: 100%; /* let our container decide our size */
  14. height: 100%;
  15. display: block;
  16. }
  17. #container {
  18. position: relative; /* makes this the origin of its children */
  19. width: 100vw;
  20. height: 100vh;
  21. overflow: hidden;
  22. }
  23. #labels {
  24. position: absolute; /* let us position ourself inside the container */
  25. left: 0; /* make our position the top left of the container */
  26. top: 0;
  27. color: white;
  28. }
  29. #labels>div {
  30. position: absolute; /* let us position them inside the container */
  31. left: 0; /* make their default position the top left of the container */
  32. top: 0;
  33. cursor: pointer; /* change the cursor to a hand when over us */
  34. font-size: large;
  35. user-select: none; /* don't let the text get selected */
  36. text-shadow: /* create a black outline */
  37. -1px -1px 0 #000,
  38. 0 -1px 0 #000,
  39. 1px -1px 0 #000,
  40. 1px 0 0 #000,
  41. 1px 1px 0 #000,
  42. 0 1px 0 #000,
  43. -1px 1px 0 #000,
  44. -1px 0 0 #000;
  45. }
  46. #labels>div:hover {
  47. color: red;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div id="container">
  53. <canvas id="c"></canvas>
  54. <div id="labels"></div>
  55. </div>
  56. </body>
  57. <script type="module">
  58. import * as THREE from './resources/threejs/r115/build/three.module.js';
  59. import {OrbitControls} from './resources/threejs/r115/examples/jsm/controls/OrbitControls.js';
  60. function main() {
  61. const canvas = document.querySelector('#c');
  62. const renderer = new THREE.WebGLRenderer({canvas});
  63. const fov = 75;
  64. const aspect = 2; // the canvas default
  65. const near = 1.1;
  66. const far = 50;
  67. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  68. camera.position.z = 7;
  69. const controls = new OrbitControls(camera, canvas);
  70. controls.target.set(0, 0, 0);
  71. controls.update();
  72. const scene = new THREE.Scene();
  73. {
  74. const color = 0xFFFFFF;
  75. const intensity = 1;
  76. const light = new THREE.DirectionalLight(color, intensity);
  77. light.position.set(-1, 2, 4);
  78. scene.add(light);
  79. }
  80. const boxWidth = 1;
  81. const boxHeight = 1;
  82. const boxDepth = 1;
  83. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  84. const labelContainerElem = document.querySelector('#labels');
  85. function makeInstance(geometry, color, x, name) {
  86. const material = new THREE.MeshPhongMaterial({color});
  87. const cube = new THREE.Mesh(geometry, material);
  88. scene.add(cube);
  89. cube.position.x = x;
  90. const elem = document.createElement('div');
  91. elem.textContent = name;
  92. labelContainerElem.appendChild(elem);
  93. return {cube, elem};
  94. }
  95. const cubes = [
  96. makeInstance(geometry, 0x44aa88, 0, 'Aqua'),
  97. makeInstance(geometry, 0x8844aa, -2, 'Purple'),
  98. makeInstance(geometry, 0xaa8844, 2, 'Gold'),
  99. ];
  100. function resizeRendererToDisplaySize(renderer) {
  101. const canvas = renderer.domElement;
  102. const width = canvas.clientWidth;
  103. const height = canvas.clientHeight;
  104. const needResize = canvas.width !== width || canvas.height !== height;
  105. if (needResize) {
  106. renderer.setSize(width, height, false);
  107. }
  108. return needResize;
  109. }
  110. const tempV = new THREE.Vector3();
  111. function render(time) {
  112. time *= 0.001;
  113. if (resizeRendererToDisplaySize(renderer)) {
  114. const canvas = renderer.domElement;
  115. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  116. camera.updateProjectionMatrix();
  117. }
  118. cubes.forEach((cubeInfo, ndx) => {
  119. const {cube, elem} = cubeInfo;
  120. const speed = 1 + ndx * .1;
  121. const rot = time * speed;
  122. cube.rotation.x = rot;
  123. cube.rotation.y = rot;
  124. // get the position of the center of the cube
  125. cube.updateWorldMatrix(true, false);
  126. cube.getWorldPosition(tempV);
  127. // get the normalized screen coordinate of that position
  128. // x and y will be in the -1 to +1 range with x = -1 being
  129. // on the left and y = -1 being on the bottom
  130. tempV.project(camera);
  131. // convert the normalized position to CSS coordinates
  132. const x = (tempV.x * .5 + .5) * canvas.clientWidth;
  133. const y = (tempV.y * -.5 + .5) * canvas.clientHeight;
  134. // move the elem to that position
  135. elem.style.transform = `translate(-50%, -50%) translate(${x}px,${y}px)`;
  136. });
  137. renderer.render(scene, camera);
  138. requestAnimationFrame(render);
  139. }
  140. requestAnimationFrame(render);
  141. }
  142. main();
  143. </script>
  144. </html>