canvas-textured-cube-qix.html 4.0 KB

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