threejs-tips-transparent-canvas.html 3.1 KB

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