canvas-textured-cube-qix.html 4.3 KB

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