webgl_materials_texture_canvas.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. <style>
  8. body {
  9. margin: 0px;
  10. background-color: #050505;
  11. color: #fff;
  12. overflow: hidden;
  13. }
  14. a { color: #e00 }
  15. #info {
  16. position: absolute;
  17. top: 0px; width: 100%;
  18. color: #ffffff;
  19. padding: 5px;
  20. font-family: Monospace;
  21. font-size: 13px;
  22. text-align: center;
  23. z-index: 1000;
  24. }
  25. #drawing-canvas {
  26. position: absolute;
  27. background-color: #000000;
  28. top: 0px;
  29. right: 0px;
  30. z-index: 3000;
  31. cursor: crosshair;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <div id="info">
  37. <a href="http://threejs.org" target="_blank">three.js</a> - webgl - canvas as a texture
  38. <div>click and draw in the white box</div>
  39. </div>
  40. <canvas id="drawing-canvas" height="128" width="128"></canvas>
  41. <script src="../build/three.js"></script>
  42. <script src="js/Detector.js"></script>
  43. <script src="js/libs/stats.min.js"></script>
  44. <script>
  45. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  46. var camera, scene, renderer, mesh, material;
  47. var drawStartPos = { x: 0, y: 0 };
  48. init();
  49. setupCanvasDrawing();
  50. animate();
  51. function init() {
  52. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
  53. camera.position.z = 400;
  54. scene = new THREE.Scene();
  55. geometry = new THREE.BoxGeometry( 200, 200, 200 );
  56. material = new THREE.MeshBasicMaterial();
  57. mesh = new THREE.Mesh( geometry, material );
  58. scene.add( mesh );
  59. renderer = new THREE.WebGLRenderer( { antialias: true } );
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. document.body.appendChild( renderer.domElement );
  63. stats = new Stats();
  64. document.body.appendChild( stats.dom );
  65. window.addEventListener( 'resize', onWindowResize, false );
  66. }
  67. // Sets up the drawing canvas and adds it as the material map.
  68. function setupCanvasDrawing() {
  69. // get canvas and context
  70. var drawingCanvas = document.getElementById( 'drawing-canvas' );
  71. var drawingContext = drawingCanvas.getContext( '2d' );
  72. // draw white background
  73. drawingContext.fillStyle = "#FFFFFF";
  74. drawingContext.fillRect( 0, 0, 128, 128 );
  75. // set canvas as material.map (this could be done to any map, bump, displacement etc.)
  76. material.map = new THREE.Texture( drawingCanvas );
  77. // need to flag the map as needing updating.
  78. material.map.needsUpdate = true;
  79. // set the variable to keep track of when to draw
  80. var paint = false;
  81. // add canvas event listeners
  82. drawingCanvas.addEventListener( 'mousedown', function( e ) {
  83. paint = true;
  84. drawStartPos = { x: e.offsetX, y: e.offsetY };
  85. } );
  86. drawingCanvas.addEventListener( 'mousemove', function( e ) {
  87. if(paint){
  88. draw( drawingContext, e.offsetX, e.offsetY );
  89. }
  90. } );
  91. drawingCanvas.addEventListener( 'mouseup', function( e ) {
  92. paint = false;
  93. } );
  94. drawingCanvas.addEventListener( 'mouseleave', function( e ) {
  95. paint = false;
  96. } );
  97. }
  98. function draw( drawContext, x, y ) {
  99. drawContext.moveTo( drawStartPos.x, drawStartPos.y );
  100. drawContext.strokeStyle = '#000000';
  101. drawContext.lineTo( x, y );
  102. drawContext.stroke();
  103. // reset drawing start position to current position.
  104. drawStartPos = { x: x, y: y };
  105. // need to flag the map as needing updating.
  106. material.map.needsUpdate = true;
  107. }
  108. function onWindowResize() {
  109. camera.aspect = window.innerWidth / window.innerHeight;
  110. camera.updateProjectionMatrix();
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. }
  113. function animate() {
  114. requestAnimationFrame( animate );
  115. mesh.rotation.x += 0.01;
  116. mesh.rotation.y += 0.01;
  117. renderer.render( scene, camera );
  118. stats.update();
  119. }
  120. </script>
  121. </body>
  122. </html>