threejs-responsive.html 2.5 KB

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