tips-transparent-canvas.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. <!-- Import maps polyfill -->
  51. <!-- Remove this when import maps will be widely supported -->
  52. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  53. <script type="importmap">
  54. {
  55. "imports": {
  56. "three": "../../build/three.module.js"
  57. }
  58. }
  59. </script>
  60. <script type="module">
  61. import * as THREE from 'three';
  62. function main() {
  63. const canvas = document.querySelector('#c');
  64. const renderer = new THREE.WebGLRenderer({
  65. canvas,
  66. alpha: true,
  67. premultipliedAlpha: false,
  68. });
  69. const fov = 75;
  70. const aspect = 2; // the canvas default
  71. const near = 0.1;
  72. const far = 5;
  73. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  74. camera.position.z = 2;
  75. const scene = new THREE.Scene();
  76. {
  77. const color = 0xFFFFFF;
  78. const intensity = 1;
  79. const light = new THREE.DirectionalLight(color, intensity);
  80. light.position.set(-1, 2, 4);
  81. scene.add(light);
  82. }
  83. const boxWidth = 1;
  84. const boxHeight = 1;
  85. const boxDepth = 1;
  86. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  87. function makeInstance(geometry, color, x) {
  88. const material = new THREE.MeshPhongMaterial({
  89. color,
  90. opacity: 0.5,
  91. });
  92. const cube = new THREE.Mesh(geometry, material);
  93. scene.add(cube);
  94. cube.position.x = x;
  95. return cube;
  96. }
  97. const cubes = [
  98. makeInstance(geometry, 0x44aa88, 0),
  99. makeInstance(geometry, 0x8844aa, -2),
  100. makeInstance(geometry, 0xaa8844, 2),
  101. ];
  102. function resizeRendererToDisplaySize(renderer) {
  103. const canvas = renderer.domElement;
  104. const width = canvas.clientWidth;
  105. const height = canvas.clientHeight;
  106. const needResize = canvas.width !== width || canvas.height !== height;
  107. if (needResize) {
  108. renderer.setSize(width, height, false);
  109. }
  110. return needResize;
  111. }
  112. function render(time) {
  113. time *= 0.001;
  114. if (resizeRendererToDisplaySize(renderer)) {
  115. const canvas = renderer.domElement;
  116. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  117. camera.updateProjectionMatrix();
  118. }
  119. cubes.forEach((cube, ndx) => {
  120. const speed = 1 + ndx * .1;
  121. const rot = time * speed;
  122. cube.rotation.x = rot;
  123. cube.rotation.y = rot;
  124. });
  125. renderer.render(scene, camera);
  126. requestAnimationFrame(render);
  127. }
  128. requestAnimationFrame(render);
  129. }
  130. main();
  131. </script>
  132. </html>