webgl_materials_texture_canvas.html 3.4 KB

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