threejs-canvas-textured-cube.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 type="module">
  23. import * as THREE from './resources/threejs/r114/build/three.module.js';
  24. function main() {
  25. const canvas = document.querySelector('#c');
  26. const renderer = new THREE.WebGLRenderer({canvas});
  27. const fov = 75;
  28. const aspect = 2; // the canvas default
  29. const near = 0.1;
  30. const far = 5;
  31. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  32. camera.position.z = 2;
  33. const scene = new THREE.Scene();
  34. const boxWidth = 1;
  35. const boxHeight = 1;
  36. const boxDepth = 1;
  37. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  38. const cubes = []; // just an array we can use to rotate the cubes
  39. const ctx = document.createElement('canvas').getContext('2d');
  40. ctx.canvas.width = 256;
  41. ctx.canvas.height = 256;
  42. ctx.fillStyle = '#FFF';
  43. ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  44. const texture = new THREE.CanvasTexture(ctx.canvas);
  45. const material = new THREE.MeshBasicMaterial({
  46. map: texture,
  47. });
  48. const cube = new THREE.Mesh(geometry, material);
  49. scene.add(cube);
  50. cubes.push(cube); // add to our list of cubes to rotate
  51. function resizeRendererToDisplaySize(renderer) {
  52. const canvas = renderer.domElement;
  53. const width = canvas.clientWidth;
  54. const height = canvas.clientHeight;
  55. const needResize = canvas.width !== width || canvas.height !== height;
  56. if (needResize) {
  57. renderer.setSize(width, height, false);
  58. }
  59. return needResize;
  60. }
  61. function randInt(min, max) {
  62. if (max === undefined) {
  63. max = min;
  64. min = 0;
  65. }
  66. return Math.random() * (max - min) + min | 0;
  67. }
  68. function drawRandomDot() {
  69. ctx.fillStyle = `#${randInt(0x1000000).toString(16).padStart(6, '0')}`;
  70. ctx.beginPath();
  71. const x = randInt(256);
  72. const y = randInt(256);
  73. const radius = randInt(10, 64);
  74. ctx.arc(x, y, radius, 0, Math.PI * 2);
  75. ctx.fill();
  76. }
  77. function render(time) {
  78. time *= 0.001;
  79. if (resizeRendererToDisplaySize(renderer)) {
  80. const canvas = renderer.domElement;
  81. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  82. camera.updateProjectionMatrix();
  83. }
  84. drawRandomDot();
  85. texture.needsUpdate = true;
  86. cubes.forEach((cube, ndx) => {
  87. const speed = .2 + ndx * .1;
  88. const rot = time * speed;
  89. cube.rotation.x = rot;
  90. cube.rotation.y = rot;
  91. });
  92. renderer.render(scene, camera);
  93. requestAnimationFrame(render);
  94. }
  95. requestAnimationFrame(render);
  96. }
  97. main();
  98. </script>
  99. </html>