tips-transparent-canvas.html 3.2 KB

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