threejs-responsive-no-resize.html 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 - Responsive no resize</title>
  8. <style>
  9. html, body {
  10. margin: 0;
  11. height: 100%;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script src="resources/threejs/r93/three.min.js"></script>
  24. <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 -->
  25. <script>
  26. 'use strict';
  27. function main() {
  28. const canvas = document.querySelector('#c');
  29. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  30. const fov = 75;
  31. const aspect = 2; // the canvas default
  32. const zNear = 0.1;
  33. const zFar = 5;
  34. const camera = new THREE.PerspectiveCamera(fov, aspect, zNear, zFar);
  35. camera.position.z = 2;
  36. const scene = new THREE.Scene();
  37. const light = new THREE.DirectionalLight(0xffffff, 1);
  38. light.position.set(-1, 2, 4);
  39. scene.add(light);
  40. const boxWidth = 1;
  41. const boxHeight = 1;
  42. const boxDepth = 1;
  43. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  44. function makeInstance(geometry, color, x) {
  45. const material = new THREE.MeshPhongMaterial({color});
  46. const cube = new THREE.Mesh(geometry, material);
  47. scene.add(cube);
  48. cube.position.x = x;
  49. return cube;
  50. }
  51. const cubes = [
  52. makeInstance(geometry, 0x44aa88, 0),
  53. makeInstance(geometry, 0x8844aa, -2),
  54. makeInstance(geometry, 0xaa8844, 2),
  55. ];
  56. function render(time) {
  57. time *= 0.001;
  58. cubes.forEach((cube, ndx) => {
  59. const speed = 1 + ndx * .1;
  60. const rot = time * speed;
  61. cube.rotation.x = rot;
  62. cube.rotation.y = rot;
  63. });
  64. renderer.render(scene, camera);
  65. requestAnimationFrame(render);
  66. }
  67. requestAnimationFrame(render);
  68. }
  69. main();
  70. </script>
  71. </html>