debug-js-html-elements.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 - Debug - HTML Elements</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. #debug {
  19. position: absolute;
  20. left: 1em;
  21. top: 1em;
  22. padding: 1em;
  23. background: rgba(0, 0, 0, 0.9);
  24. color: white;
  25. font-family: monospace;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <canvas id="c"></canvas>
  31. <div id="debug">
  32. <div>x:<span id="x"></span></div>
  33. <div>y:<span id="y"></span></div>
  34. <div>z:<span id="z"></span></div>
  35. </div>
  36. </body>
  37. <script type="importmap">
  38. {
  39. "imports": {
  40. "three": "../../build/three.module.js"
  41. }
  42. }
  43. </script>
  44. <script type="module">
  45. import * as THREE from 'three';
  46. function main() {
  47. const canvas = document.querySelector( '#c' );
  48. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  49. const fov = 75;
  50. const aspect = 2; // the canvas default
  51. const near = 0.1;
  52. const far = 50;
  53. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  54. camera.position.z = 20;
  55. const scene = new THREE.Scene();
  56. scene.background = new THREE.Color( 'red' );
  57. const geometry = new THREE.SphereGeometry();
  58. const material = new THREE.MeshBasicMaterial( { color: 'yellow' } );
  59. const mesh = new THREE.Mesh( geometry, material );
  60. scene.add( mesh );
  61. function resizeRendererToDisplaySize( renderer ) {
  62. const canvas = renderer.domElement;
  63. const width = canvas.clientWidth;
  64. const height = canvas.clientHeight;
  65. const needResize = canvas.width !== width || canvas.height !== height;
  66. if ( needResize ) {
  67. renderer.setSize( width, height, false );
  68. }
  69. return needResize;
  70. }
  71. const xElem = document.querySelector( '#x' );
  72. const yElem = document.querySelector( '#y' );
  73. const zElem = document.querySelector( '#z' );
  74. function render( time ) {
  75. time *= 0.001; // convert to seconds
  76. if ( resizeRendererToDisplaySize( renderer ) ) {
  77. const canvas = renderer.domElement;
  78. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  79. camera.updateProjectionMatrix();
  80. }
  81. mesh.position.set(
  82. Math.sin( time * 1.2 ) * 5,
  83. Math.sin( time * 1.1 ) * 5,
  84. Math.sin( time * 1.3 ) * 10,
  85. );
  86. xElem.textContent = mesh.position.x.toFixed( 3 );
  87. yElem.textContent = mesh.position.y.toFixed( 3 );
  88. zElem.textContent = mesh.position.z.toFixed( 3 );
  89. renderer.render( scene, camera );
  90. requestAnimationFrame( render );
  91. }
  92. requestAnimationFrame( render );
  93. }
  94. main();
  95. </script>
  96. </html>