webgl_materials_texture_canvas.html 3.8 KB

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