This repository was archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththree_materials_texture_canvas.html
More file actions
159 lines (117 loc) · 3.84 KB
/
three_materials_texture_canvas.html
File metadata and controls
159 lines (117 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js css3d - 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">
<style>
body {
margin: 0px;
background-color: #050505;
color: #fff;
overflow: hidden;
}
a { color: #e00 }
#info {
position: absolute;
top: 0px; width: 100%;
color: #ffffff;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: center;
z-index: 1000;
}
#drawing-canvas {
position: absolute;
background-color: #000000;
top: 0px;
right: 0px;
z-index: 3000;
cursor: crosshair;
}
</style>
</head>
<body>
<div id="info">
<a href="http://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 src="js/libs/three.min.js"></script>
<script src="build/C3R.standalone.js"></script>
<script src="js/renderers/CSS3DRenderer.js"></script>
<script>
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.CSS3DRenderer();
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.Texture( drawingCanvas );
// need to flag the map as needing updating.
material.map.needsUpdate = true;
// 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( e ) {
paint = false;
} );
drawingCanvas.addEventListener( 'mouseleave', function( e ) {
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>