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

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