canvas-textured-cube.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 randInt(min, max) {
  63. if (max === undefined) {
  64. max = min;
  65. min = 0;
  66. }
  67. return Math.random() * (max - min) + min | 0;
  68. }
  69. function drawRandomDot() {
  70. ctx.fillStyle = `#${randInt(0x1000000).toString(16).padStart(6, '0')}`;
  71. ctx.beginPath();
  72. const x = randInt(256);
  73. const y = randInt(256);
  74. const radius = randInt(10, 64);
  75. ctx.arc(x, y, radius, 0, Math.PI * 2);
  76. ctx.fill();
  77. }
  78. function render(time) {
  79. time *= 0.001;
  80. if (resizeRendererToDisplaySize(renderer)) {
  81. const canvas = renderer.domElement;
  82. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  83. camera.updateProjectionMatrix();
  84. }
  85. drawRandomDot();
  86. texture.needsUpdate = true;
  87. cubes.forEach((cube, ndx) => {
  88. const speed = .2 + ndx * .1;
  89. const rot = time * speed;
  90. cube.rotation.x = rot;
  91. cube.rotation.y = rot;
  92. });
  93. renderer.render(scene, camera);
  94. requestAnimationFrame(render);
  95. }
  96. requestAnimationFrame(render);
  97. }
  98. main();
  99. </script>
  100. </html>