threejs-canvas-textured-cube-qix.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 - Canvas Textured Cube</title>
  8. <style>
  9. body {
  10. margin: 0;
  11. }
  12. #c {
  13. width: 100vw;
  14. height: 100vh;
  15. display: block;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <canvas id="c"></canvas>
  21. </body>
  22. <script type="module">
  23. import * as THREE from './resources/threejs/r114/build/three.module.js';
  24. function main() {
  25. const canvas = document.querySelector('#c');
  26. const renderer = new THREE.WebGLRenderer({canvas});
  27. const fov = 75;
  28. const aspect = 2; // the canvas default
  29. const near = 0.1;
  30. const far = 5;
  31. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  32. camera.position.z = 2;
  33. const scene = new THREE.Scene();
  34. const boxWidth = 1;
  35. const boxHeight = 1;
  36. const boxDepth = 1;
  37. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  38. const cubes = []; // just an array we can use to rotate the cubes
  39. const ctx = document.createElement('canvas').getContext('2d');
  40. ctx.canvas.width = 256;
  41. ctx.canvas.height = 256;
  42. ctx.fillStyle = '#FFF';
  43. ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  44. const texture = new THREE.CanvasTexture(ctx.canvas);
  45. const material = new THREE.MeshBasicMaterial({
  46. map: texture,
  47. });
  48. const cube = new THREE.Mesh(geometry, material);
  49. scene.add(cube);
  50. cubes.push(cube); // add to our list of cubes to rotate
  51. function resizeRendererToDisplaySize(renderer) {
  52. const canvas = renderer.domElement;
  53. const width = canvas.clientWidth;
  54. const height = canvas.clientHeight;
  55. const needResize = canvas.width !== width || canvas.height !== height;
  56. if (needResize) {
  57. renderer.setSize(width, height, false);
  58. }
  59. return needResize;
  60. }
  61. function rand(min, max) {
  62. if (max === undefined) {
  63. max = min;
  64. min = 0;
  65. }
  66. return Math.random() * (max - min) + min;
  67. }
  68. function randVelocity() {
  69. return rand(2, 4) * (rand(2) < 1 ? -1 : 1);
  70. }
  71. const maxLines = 60;
  72. const points = [
  73. { position: [rand(256), rand(256)], direction: [randVelocity(), randVelocity()]},
  74. { position: [rand(256), rand(256)], direction: [randVelocity(), randVelocity()]},
  75. ];
  76. const lineHistory = [];
  77. let lineCursor = 0;
  78. function drawCurrentLine() {
  79. const line = lineHistory[lineCursor];
  80. ctx.beginPath();
  81. ctx.moveTo(...line[0]);
  82. ctx.lineTo(...line[1]);
  83. ctx.stroke();
  84. }
  85. function drawLines(time) {
  86. points.forEach((point) => {
  87. point.position.forEach((position, ndx) => {
  88. const newPosition = position + point.direction[ndx];
  89. if (newPosition > 255) {
  90. point.direction[ndx] = rand(-2, -4);
  91. } else if (newPosition < 0) {
  92. point.direction[ndx] = rand( 2, 4);
  93. }
  94. point.position[ndx] = newPosition;
  95. });
  96. });
  97. if (lineHistory.length === maxLines) {
  98. ctx.lineWidth = 3;
  99. ctx.strokeStyle = '#FFF';
  100. drawCurrentLine();
  101. }
  102. lineHistory[lineCursor] = points.map(point => point.position.slice());
  103. ctx.lineWidth = 1;
  104. ctx.strokeStyle = hsl(time, 1, .5);
  105. drawCurrentLine();
  106. lineCursor = (lineCursor + 1) % maxLines;
  107. }
  108. function hsl(h, s, l) {
  109. return `hsl(${h * 360 | 0},${s * 100 | 0}%,${l * 100 | 0}%)`;
  110. }
  111. function render(time) {
  112. time *= 0.001;
  113. if (resizeRendererToDisplaySize(renderer)) {
  114. const canvas = renderer.domElement;
  115. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  116. camera.updateProjectionMatrix();
  117. }
  118. drawLines(time * 0.1);
  119. texture.needsUpdate = true;
  120. cubes.forEach((cube, ndx) => {
  121. const speed = .2 + 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>