tips-transparent-canvas.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. antialias: true
  69. } );
  70. renderer.useLegacyLights = false;
  71. const fov = 75;
  72. const aspect = 2; // the canvas default
  73. const near = 0.1;
  74. const far = 5;
  75. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  76. camera.position.z = 2;
  77. const scene = new THREE.Scene();
  78. {
  79. const color = 0xFFFFFF;
  80. const intensity = 3;
  81. const light = new THREE.DirectionalLight( color, intensity );
  82. light.position.set( - 1, 2, 4 );
  83. scene.add( light );
  84. }
  85. const boxWidth = 1;
  86. const boxHeight = 1;
  87. const boxDepth = 1;
  88. const geometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  89. function makeInstance( geometry, color, x ) {
  90. const material = new THREE.MeshPhongMaterial( {
  91. color,
  92. opacity: 0.5,
  93. } );
  94. const cube = new THREE.Mesh( geometry, material );
  95. scene.add( cube );
  96. cube.position.x = x;
  97. return cube;
  98. }
  99. const cubes = [
  100. makeInstance( geometry, 0x44aa88, 0 ),
  101. makeInstance( geometry, 0x8844aa, - 2 ),
  102. makeInstance( geometry, 0xaa8844, 2 ),
  103. ];
  104. function resizeRendererToDisplaySize( renderer ) {
  105. const canvas = renderer.domElement;
  106. const width = canvas.clientWidth;
  107. const height = canvas.clientHeight;
  108. const needResize = canvas.width !== width || canvas.height !== height;
  109. if ( needResize ) {
  110. renderer.setSize( width, height, false );
  111. }
  112. return needResize;
  113. }
  114. function render( time ) {
  115. time *= 0.001;
  116. if ( resizeRendererToDisplaySize( renderer ) ) {
  117. const canvas = renderer.domElement;
  118. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  119. camera.updateProjectionMatrix();
  120. }
  121. cubes.forEach( ( cube, ndx ) => {
  122. const speed = 1 + ndx * .1;
  123. const rot = time * speed;
  124. cube.rotation.x = rot;
  125. cube.rotation.y = rot;
  126. } );
  127. renderer.render( scene, camera );
  128. requestAnimationFrame( render );
  129. }
  130. requestAnimationFrame( render );
  131. }
  132. main();
  133. </script>
  134. </html>