webgl_materials_texture_canvas.html 3.7 KB

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