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