background-css.html 2.6 KB

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