webgl_materials_texture_canvas.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - canvas texture</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. #drawing-canvas {
  10. position: absolute;
  11. background-color: #000000;
  12. top: 0px;
  13. right: 0px;
  14. z-index: 3000;
  15. cursor: crosshair;
  16. touch-action: none;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="info">
  22. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - canvas as a texture
  23. <div>click and draw in the white box</div>
  24. </div>
  25. <canvas id="drawing-canvas" height="128" width="128"></canvas>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../build/three.module.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. let camera, scene, renderer, mesh, material;
  37. const drawStartPos = new THREE.Vector2();
  38. init();
  39. setupCanvasDrawing();
  40. function init() {
  41. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
  42. camera.position.z = 500;
  43. scene = new THREE.Scene();
  44. material = new THREE.MeshBasicMaterial();
  45. mesh = new THREE.Mesh( new THREE.BoxGeometry( 200, 200, 200 ), material );
  46. scene.add( mesh );
  47. renderer = new THREE.WebGLRenderer( { antialias: true } );
  48. renderer.setPixelRatio( window.devicePixelRatio );
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. renderer.setAnimationLoop( animate );
  51. document.body.appendChild( renderer.domElement );
  52. window.addEventListener( 'resize', onWindowResize );
  53. }
  54. // Sets up the drawing canvas and adds it as the material map
  55. function setupCanvasDrawing() {
  56. // get canvas and context
  57. const drawingCanvas = document.getElementById( 'drawing-canvas' );
  58. const drawingContext = drawingCanvas.getContext( '2d' );
  59. // draw white background
  60. drawingContext.fillStyle = '#FFFFFF';
  61. drawingContext.fillRect( 0, 0, 128, 128 );
  62. // set canvas as material.map (this could be done to any map, bump, displacement etc.)
  63. material.map = new THREE.CanvasTexture( drawingCanvas );
  64. // set the variable to keep track of when to draw
  65. let paint = false;
  66. // add canvas event listeners
  67. drawingCanvas.addEventListener( 'pointerdown', function ( e ) {
  68. paint = true;
  69. drawStartPos.set( e.offsetX, e.offsetY );
  70. } );
  71. drawingCanvas.addEventListener( 'pointermove', function ( e ) {
  72. if ( paint ) draw( drawingContext, e.offsetX, e.offsetY );
  73. } );
  74. drawingCanvas.addEventListener( 'pointerup', function () {
  75. paint = false;
  76. } );
  77. drawingCanvas.addEventListener( 'pointerleave', function () {
  78. paint = false;
  79. } );
  80. }
  81. function draw( drawContext, x, y ) {
  82. drawContext.moveTo( drawStartPos.x, drawStartPos.y );
  83. drawContext.strokeStyle = '#000000';
  84. drawContext.lineTo( x, y );
  85. drawContext.stroke();
  86. // reset drawing start position to current position.
  87. drawStartPos.set( x, y );
  88. // need to flag the map as needing updating.
  89. material.map.needsUpdate = true;
  90. }
  91. function onWindowResize() {
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. }
  96. function animate() {
  97. mesh.rotation.x += 0.01;
  98. mesh.rotation.y += 0.01;
  99. renderer.render( scene, camera );
  100. }
  101. </script>
  102. </body>
  103. </html>