fundamentals-with-animation.html 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 - Fundamentals with animation</title>
  8. </head>
  9. <body>
  10. <canvas id="c"></canvas>
  11. </body>
  12. <script type="module">
  13. import * as THREE from '../../build/three.module.js';
  14. function main() {
  15. const canvas = document.querySelector('#c');
  16. const renderer = new THREE.WebGLRenderer({canvas});
  17. const fov = 75;
  18. const aspect = 2; // the canvas default
  19. const near = 0.1;
  20. const far = 5;
  21. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  22. camera.position.z = 2;
  23. const scene = new THREE.Scene();
  24. const boxWidth = 1;
  25. const boxHeight = 1;
  26. const boxDepth = 1;
  27. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  28. const material = new THREE.MeshBasicMaterial({color: 0x44aa88}); // greenish blue
  29. const cube = new THREE.Mesh(geometry, material);
  30. scene.add(cube);
  31. function render(time) {
  32. time *= 0.001; // convert time to seconds
  33. cube.rotation.x = time;
  34. cube.rotation.y = time;
  35. renderer.render(scene, camera);
  36. requestAnimationFrame(render);
  37. }
  38. requestAnimationFrame(render);
  39. }
  40. main();
  41. </script>
  42. </html>