threejs-tips-transparent-canvas.html 3.1 KB

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