tips-transparent-canvas.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 - Transparent Canvas</title>
  8. <style>
  9. html {
  10. box-sizing: border-box;
  11. }
  12. *, *:before, *:after {
  13. box-sizing: inherit;
  14. }
  15. html, body {
  16. height: 100%;
  17. margin: 0;
  18. }
  19. #c {
  20. width: 100%;
  21. height: 100%;
  22. display: block;
  23. position: fixed;
  24. left: 0;
  25. top: 0;
  26. z-index: 2;
  27. pointer-events: none;
  28. }
  29. #content {
  30. font-size: 7vw;
  31. font-family: sans-serif;
  32. text-align: center;
  33. width: 100%;
  34. height: 100%;
  35. display: flex;
  36. justify-content: center;
  37. align-items: center;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <canvas id="c"></canvas>
  43. <div id="content">
  44. <div>
  45. <h1>Cubes-R-Us!</h1>
  46. <p>We make the best cubes!</p>
  47. </div>
  48. </div>
  49. </body>
  50. <script type="module">
  51. import * as THREE from '../../build/three.module.js';
  52. function main() {
  53. const canvas = document.querySelector('#c');
  54. const renderer = new THREE.WebGLRenderer({
  55. canvas,
  56. alpha: true,
  57. premultipliedAlpha: false,
  58. });
  59. const fov = 75;
  60. const aspect = 2; // the canvas default
  61. const near = 0.1;
  62. const far = 5;
  63. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  64. camera.position.z = 2;
  65. const scene = new THREE.Scene();
  66. {
  67. const color = 0xFFFFFF;
  68. const intensity = 1;
  69. const light = new THREE.DirectionalLight(color, intensity);
  70. light.position.set(-1, 2, 4);
  71. scene.add(light);
  72. }
  73. const boxWidth = 1;
  74. const boxHeight = 1;
  75. const boxDepth = 1;
  76. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  77. function makeInstance(geometry, color, x) {
  78. const material = new THREE.MeshPhongMaterial({
  79. color,
  80. opacity: 0.5,
  81. });
  82. const cube = new THREE.Mesh(geometry, material);
  83. scene.add(cube);
  84. cube.position.x = x;
  85. return cube;
  86. }
  87. const cubes = [
  88. makeInstance(geometry, 0x44aa88, 0),
  89. makeInstance(geometry, 0x8844aa, -2),
  90. makeInstance(geometry, 0xaa8844, 2),
  91. ];
  92. function resizeRendererToDisplaySize(renderer) {
  93. const canvas = renderer.domElement;
  94. const width = canvas.clientWidth;
  95. const height = canvas.clientHeight;
  96. const needResize = canvas.width !== width || canvas.height !== height;
  97. if (needResize) {
  98. renderer.setSize(width, height, false);
  99. }
  100. return needResize;
  101. }
  102. function render(time) {
  103. time *= 0.001;
  104. if (resizeRendererToDisplaySize(renderer)) {
  105. const canvas = renderer.domElement;
  106. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  107. camera.updateProjectionMatrix();
  108. }
  109. cubes.forEach((cube, ndx) => {
  110. const speed = 1 + ndx * .1;
  111. const rot = time * speed;
  112. cube.rotation.x = rot;
  113. cube.rotation.y = rot;
  114. });
  115. renderer.render(scene, camera);
  116. requestAnimationFrame(render);
  117. }
  118. requestAnimationFrame(render);
  119. }
  120. main();
  121. </script>
  122. </html>