tips-transparent-canvas.html 3.4 KB

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