threejs-fundamentals-with-animation.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 src="resources/threejs/r93/three.min.js"></script>
  13. <script src="resources/threejs-lessons-helper.js"></script> <!-- you can and should delete this script. it is only used on the site to help with errors -->
  14. <script>
  15. 'use strict';
  16. function main() {
  17. const canvas = document.querySelector('#c');
  18. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  19. const fov = 75;
  20. const aspect = 2; // the canvas default
  21. const zNear = 0.1;
  22. const zFar = 5;
  23. const camera = new THREE.PerspectiveCamera(fov, aspect, zNear, zFar);
  24. camera.position.z = 2;
  25. const scene = new THREE.Scene();
  26. const boxWidth = 1;
  27. const boxHeight = 1;
  28. const boxDepth = 1;
  29. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  30. const material = new THREE.MeshBasicMaterial({color: 0x44aa88}); // greenish blue
  31. const cube = new THREE.Mesh(geometry, material);
  32. scene.add(cube);
  33. function render(time) {
  34. time *= 0.001; // convert time to seconds
  35. cube.rotation.x = time;
  36. cube.rotation.y = time;
  37. renderer.render(scene, camera);
  38. requestAnimationFrame(render);
  39. }
  40. requestAnimationFrame(render);
  41. }
  42. main();
  43. </script>
  44. </html>