threejs-canvas-textured-cube.html 2.9 KB

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