-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
/
Text2D.js
38 lines (32 loc) · 1.02 KB
/
Text2D.js
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
import { DoubleSide, Mesh, MeshBasicMaterial, PlaneGeometry, Texture } from 'three';
function createText( message, height ) {
const canvas = document.createElement( 'canvas' );
const context = canvas.getContext( '2d' );
let metrics = null;
const textHeight = 100;
context.font = 'normal ' + textHeight + 'px Arial';
metrics = context.measureText( message );
const textWidth = metrics.width;
canvas.width = textWidth;
canvas.height = textHeight;
context.font = 'normal ' + textHeight + 'px Arial';
context.textAlign = 'center';
context.textBaseline = 'middle';
context.fillStyle = '#ffffff';
context.fillText( message, textWidth / 2, textHeight / 2 );
const texture = new Texture( canvas );
texture.needsUpdate = true;
const material = new MeshBasicMaterial( {
color: 0xffffff,
side: DoubleSide,
map: texture,
transparent: true,
} );
const geometry = new PlaneGeometry(
( height * textWidth ) / textHeight,
height
);
const plane = new Mesh( geometry, material );
return plane;
}
export { createText };