threejs-responsive-hd-dpi.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 HD-DPI</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/r102/three.min.js"></script>
  24. <script>
  25. 'use strict';
  26. /* global THREE */
  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 near = 0.1;
  33. const far = 5;
  34. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  35. camera.position.z = 2;
  36. const scene = new THREE.Scene();
  37. {
  38. const color = 0xFFFFFF;
  39. const intensity = 1;
  40. const light = new THREE.DirectionalLight(color, intensity);
  41. light.position.set(-1, 2, 4);
  42. scene.add(light);
  43. }
  44. const boxWidth = 1;
  45. const boxHeight = 1;
  46. const boxDepth = 1;
  47. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  48. function makeInstance(geometry, color, x) {
  49. const material = new THREE.MeshPhongMaterial({color});
  50. const cube = new THREE.Mesh(geometry, material);
  51. scene.add(cube);
  52. cube.position.x = x;
  53. return cube;
  54. }
  55. const cubes = [
  56. makeInstance(geometry, 0x44aa88, 0),
  57. makeInstance(geometry, 0x8844aa, -2),
  58. makeInstance(geometry, 0xaa8844, 2),
  59. ];
  60. function resizeRendererToDisplaySize(renderer) {
  61. const canvas = renderer.domElement;
  62. const pixelRatio = window.devicePixelRatio;
  63. const width = canvas.clientWidth * pixelRatio;
  64. const height = canvas.clientHeight * pixelRatio;
  65. const needResize = canvas.width !== width || canvas.height !== height;
  66. if (needResize) {
  67. renderer.setSize(width, height, false);
  68. }
  69. return needResize;
  70. }
  71. function render(time) {
  72. time *= 0.001;
  73. if (resizeRendererToDisplaySize(renderer)) {
  74. const canvas = renderer.domElement;
  75. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  76. camera.updateProjectionMatrix();
  77. }
  78. cubes.forEach((cube, ndx) => {
  79. const speed = 1 + ndx * .1;
  80. const rot = time * speed;
  81. cube.rotation.x = rot;
  82. cube.rotation.y = rot;
  83. });
  84. renderer.render(scene, camera);
  85. requestAnimationFrame(render);
  86. }
  87. requestAnimationFrame(render);
  88. }
  89. main();
  90. </script>
  91. </html>