webgl_materials_texture_canvas.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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="module">
  27. import * as THREE from '../build/three.module.js';
  28. let camera, scene, renderer, mesh, material;
  29. const drawStartPos = new THREE.Vector2();
  30. init();
  31. setupCanvasDrawing();
  32. animate();
  33. function init() {
  34. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
  35. camera.position.z = 500;
  36. scene = new THREE.Scene();
  37. material = new THREE.MeshBasicMaterial();
  38. mesh = new THREE.Mesh( new THREE.BoxGeometry( 200, 200, 200 ), material );
  39. scene.add( mesh );
  40. renderer = new THREE.WebGLRenderer( { antialias: true } );
  41. renderer.setPixelRatio( window.devicePixelRatio );
  42. renderer.setSize( window.innerWidth, window.innerHeight );
  43. document.body.appendChild( renderer.domElement );
  44. window.addEventListener( 'resize', onWindowResize );
  45. }
  46. // Sets up the drawing canvas and adds it as the material map
  47. function setupCanvasDrawing() {
  48. // get canvas and context
  49. const drawingCanvas = document.getElementById( 'drawing-canvas' );
  50. const drawingContext = drawingCanvas.getContext( '2d' );
  51. // draw white background
  52. drawingContext.fillStyle = '#FFFFFF';
  53. drawingContext.fillRect( 0, 0, 128, 128 );
  54. // set canvas as material.map (this could be done to any map, bump, displacement etc.)
  55. material.map = new THREE.CanvasTexture( drawingCanvas );
  56. // set the variable to keep track of when to draw
  57. let paint = false;
  58. // add canvas event listeners
  59. drawingCanvas.addEventListener( 'pointerdown', function ( e ) {
  60. paint = true;
  61. drawStartPos.set( e.offsetX, e.offsetY );
  62. } );
  63. drawingCanvas.addEventListener( 'pointermove', function ( e ) {
  64. if ( paint ) draw( drawingContext, e.offsetX, e.offsetY );
  65. } );
  66. drawingCanvas.addEventListener( 'pointerup', function () {
  67. paint = false;
  68. } );
  69. drawingCanvas.addEventListener( 'pointerleave', function () {
  70. paint = false;
  71. } );
  72. }
  73. function draw( drawContext, x, y ) {
  74. drawContext.moveTo( drawStartPos.x, drawStartPos.y );
  75. drawContext.strokeStyle = '#000000';
  76. drawContext.lineTo( x, y );
  77. drawContext.stroke();
  78. // reset drawing start position to current position.
  79. drawStartPos.set( x, y );
  80. // need to flag the map as needing updating.
  81. material.map.needsUpdate = true;
  82. }
  83. function onWindowResize() {
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. }
  88. function animate() {
  89. requestAnimationFrame( animate );
  90. mesh.rotation.x += 0.01;
  91. mesh.rotation.y += 0.01;
  92. renderer.render( scene, camera );
  93. }
  94. </script>
  95. </body>
  96. </html>