canvas-textured-cube-qix.html 4.0 KB

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