-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathText2D.ts
125 lines (101 loc) · 3.07 KB
/
Text2D.ts
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
import THREE = require("three");
import { textAlign } from "./utils";
import { CanvasText } from "./CanvasText";
export interface TextOptions {
font?: string;
fillStyle?: string;
align?: THREE.Vector2;
side?: number;
antialias?: boolean;
shadowColor?: string;
shadowBlur?: number;
shadowOffsetX?: number;
shadowOffsetY?: number;
lineHeight?: number;
backgroundColor?: string;
horizontalPadding?: number;
verticalPadding?: number;
}
export abstract class Text2D extends THREE.Object3D {
public side: number;
public antialias: boolean;
public texture: THREE.Texture;
public material: THREE.MeshBasicMaterial | THREE.SpriteMaterial;
protected _align: THREE.Vector2;
protected _font: string;
protected _fillStyle: string;
protected _text: string;
protected _shadowColor: string;
protected _shadowBlur: number;
protected _shadowOffsetX: number;
protected _shadowOffsetY: number;
protected _lineHeight: number;
protected _backgroundColor: string;
protected _horizontalPadding: number;
protected _verticalPadding: number;
protected canvas: CanvasText;
constructor(text = '', options: TextOptions = {}) {
super();
this._align = new THREE.Vector2();
this._font = options.font || '30px Arial';
this._fillStyle = options.fillStyle || '#FFFFFF';
this._shadowColor = options.shadowColor || 'rgba(0, 0, 0, 0)';
this._shadowBlur = options.shadowBlur || 0;
this._shadowOffsetX = options.shadowOffsetX || 0;
this._shadowOffsetY = options.shadowOffsetY || 0;
this._lineHeight = options.lineHeight || 1.2;
this._backgroundColor = options.backgroundColor || 'transparent';
this._horizontalPadding = options.horizontalPadding || 0;
this._verticalPadding = options.verticalPadding || 0;
this.canvas = new CanvasText()
this.align = options.align || textAlign.center
this.side = options.side || THREE.DoubleSide
// this.anchor = Label.fontAlignAnchor[ this._textAlign ]
this.antialias = (typeof options.antialias === "undefined") ? true : options.antialias
this.text = text;
}
abstract raycast(): void;
abstract updateText(): void;
get width () { return this.canvas.textWidth }
get height () { return this.canvas.textHeight }
get text(): string { return this._text; }
set text(value) {
if (this._text !== value) {
this._text = value;
this.updateText();
}
}
get font(): string { return this._font; }
set font(value) {
if (this._font !== value) {
this._font = value;
this.updateText();
}
}
get fillStyle() {
return this._fillStyle;
}
set fillStyle(value) {
if (this._fillStyle !== value) {
this._fillStyle = value;
this.updateText();
}
}
get align() {
return this._align;
}
set align(value: THREE.Vector2) {
this._align.copy(value);
}
cleanUp () {
if (this.texture) {
this.texture.dispose()
}
}
applyAntiAlias () {
if (this.antialias === false) {
this.texture.magFilter = THREE.NearestFilter
this.texture.minFilter = THREE.LinearMipMapLinearFilter
}
}
}