123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - canvas texture</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- #drawing-canvas {
- position: absolute;
- background-color: #000000;
- top: 0px;
- right: 0px;
- z-index: 3000;
- cursor: crosshair;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - canvas as a texture
- <div>click and draw in the white box</div>
- </div>
- <canvas id="drawing-canvas" height="128" width="128"></canvas>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- var camera, scene, renderer, mesh, material;
- var drawStartPos = new THREE.Vector2();
- init();
- setupCanvasDrawing();
- animate();
- function init() {
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
- camera.position.z = 500;
- scene = new THREE.Scene();
- material = new THREE.MeshBasicMaterial();
- mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 200, 200, 200 ), material );
- scene.add( mesh );
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- window.addEventListener( 'resize', onWindowResize, false );
- }
- // Sets up the drawing canvas and adds it as the material map
- function setupCanvasDrawing() {
- // get canvas and context
- var drawingCanvas = document.getElementById( 'drawing-canvas' );
- var drawingContext = drawingCanvas.getContext( '2d' );
- // draw white background
- drawingContext.fillStyle = '#FFFFFF';
- drawingContext.fillRect( 0, 0, 128, 128 );
- // set canvas as material.map (this could be done to any map, bump, displacement etc.)
- material.map = new THREE.CanvasTexture( drawingCanvas );
- // set the variable to keep track of when to draw
- var paint = false;
- // add canvas event listeners
- drawingCanvas.addEventListener( 'mousedown', function ( e ) {
- paint = true;
- drawStartPos.set( e.offsetX, e.offsetY );
- } );
- drawingCanvas.addEventListener( 'mousemove', function ( e ) {
- if ( paint ) draw( drawingContext, e.offsetX, e.offsetY );
- } );
- drawingCanvas.addEventListener( 'mouseup', function () {
- paint = false;
- } );
- drawingCanvas.addEventListener( 'mouseleave', function () {
- paint = false;
- } );
- }
- function draw( drawContext, x, y ) {
- drawContext.moveTo( drawStartPos.x, drawStartPos.y );
- drawContext.strokeStyle = '#000000';
- drawContext.lineTo( x, y );
- drawContext.stroke();
- // reset drawing start position to current position.
- drawStartPos.set( x, y );
- // need to flag the map as needing updating.
- material.map.needsUpdate = true;
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- mesh.rotation.x += 0.01;
- mesh.rotation.y += 0.01;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|