-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
355 lines (297 loc) · 11.2 KB
/
index.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import {Actor, BaseAlign, Color, FontStyle, TextAlign, Vector} from "excalibur";
import * as excalibur from "excalibur";
import chain from "@softwareventures/chain";
import {mapFn, foldFn, concat, toArray} from "@softwareventures/iterable";
import {hasProperty} from "unknown";
export interface LabelOptions {
/** The text to draw. */
readonly text?: string;
/** The CSS font family string (e.g. `sans-serif`, `Droid Sans Pro`). Web
* fonts are supported, same as in CSS. */
readonly fontFamily?: string;
/** The font style for the label.
*
* @default FontStyle.Normal */
readonly fontStyle?: FontStyle;
/** True if the text is bold.
*
* @default false */
readonly bold?: boolean;
/** The font size in pixels.
*
* @default 10 */
readonly fontSize?: number;
/** Horizontal text alignment.
*
* @default TextAlign.Left */
readonly textAlign?: TextAlign;
/** Baseline alignment.
*
* @default BaseAlign.Alphabetic */
readonly baseAlign?: BaseAlign;
/** The height of each line of text in pixels, for multiline text.
*
* Set to `undefined` to use the font size as the line height.
*
* @default undefined */
readonly lineHeight?: number;
/** The maximum width of a line of text, in pixels, after which the text
* will wrap to the next line.
*
* Set to `Infinity` to disable text wrapping.
*
* @default Infinity */
readonly wrapWidth?: number;
/** The position of the text in pixels. */
readonly pos?: Vector;
/** The velocity of the text in pixels per second. */
readonly vel?: Vector;
/** The acceleration of the text in pixels per second per second. */
readonly acc?: Vector;
/** The rotation of the text in radians. */
readonly rotation?: number;
/** The rotational velocity of the text in radians per second. */
readonly rx?: number;
/** True if the text is visible, false if it is invisible.
*
* @default true */
readonly visible?: boolean;
/** The color of the text. */
readonly color?: Color;
/** The color of the text outline. Set to Color.Transparent to hide the outline.
*
* @default Color.Transparent */
readonly outlineColor?: Color;
/** The width of the text outline, in pixels. Set to 0 to hide the outline.
*
* @default 0 */
readonly outlineWidth?: number;
/** Overall opacity of the label, from 0 to 1.
*
* @default 1 */
readonly alpha?: number;
/** Do not use.
*
* @deprecated This field has an unwanted and confusing interaction with
* the underlying Actor class.
*
* @see https://github.com/excaliburjs/Excalibur/issues/874#issuecomment-814557137 */
readonly opacity?: number;
/** The color of the shadow. Set to Color.Transparent to hide the shadow.
*
* @default Color.Transparent */
readonly shadowColor?: Color;
/** The offset of the shadow from the text, in pixels.
*
* @default Vector.Zero */
readonly shadowOffset?: Vector;
/** Radius of the shadow blur in pixels.
*
* @default 0 */
readonly shadowBlurRadius?: number;
}
const offscreenCanvas = (() => {
let cache: HTMLCanvasElement | null = null;
return (onscreenCanvas: HTMLCanvasElement): HTMLCanvasElement | null => {
if (cache == null) {
cache = onscreenCanvas.ownerDocument?.createElement("canvas") ?? null;
}
if (cache != null) {
cache.width = onscreenCanvas.width;
cache.height = onscreenCanvas.height;
}
return cache;
};
})();
export default class Label extends Actor {
/** The text to draw. */
public text: string;
/** True if the text is bold.
*
* @default false */
public bold: boolean;
/** The CSS font family string (e.g. `sans-serif`, `Droid Sans Pro`). Web fonts
* are supported, same as in CSS. */
public fontFamily: string;
/** Font size in the selected units (`fontUnit`).
*
* @default 10 */
public fontSize: number;
/** The font style for this label
*
* @default FontStyle.Normal */
public fontStyle: FontStyle;
/** Horizontal text alignment.
*
* @default TextAlign.Left */
public textAlign: TextAlign;
/** Vertical baseline text alignment.
*
* @default BaseAlign.Bottom */
public baseAlign: BaseAlign;
/** The height of each line of text in pixels, for multiline text.
*
* Set to `undefined` to use the font size as the line height. */
public lineHeight: number | undefined;
/** The maximum width of a line of text, in pixels, after which the text
* will wrap to the next line.
*
* Set to `Infinity` to disable text wrapping. */
public wrapWidth: number;
/** The color of the text outline. Set to Color.Transparent to hide the outline. */
public outlineColor: Color;
/** The width of the text outline, in pixels. Set to 0 to hide the outline. */
public outlineWidth: number;
/** The color of the shadow. Set to Color.Transparent to hide the shadow. */
public shadowColor: Color;
/** The offset of the shadow from the text, in pixels. */
public shadowOffset: Vector;
/** Radius of the shadow blur in pixels. */
public shadowBlurRadius: number;
/** Overall opacity of the label, from 0 to 1. */
public alpha: number;
/** Do not use.
*
* @deprecated This field has an unwanted and confusing interaction with
* the underlying Actor class.
*
* @see https://github.com/excaliburjs/Excalibur/issues/874#issuecomment-814557137 */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore Unused override to mark deprecated
public opacity: number;
public constructor(options?: LabelOptions) {
super(options);
this.text = options?.text ?? "";
this.bold = options?.bold ?? false;
this.fontFamily = options?.fontFamily ?? "sans-serif";
this.fontSize = options?.fontSize ?? 10;
this.fontStyle = options?.fontStyle ?? FontStyle.Normal;
this.textAlign = options?.textAlign ?? TextAlign.Left;
this.baseAlign = options?.baseAlign ?? BaseAlign.Bottom;
this.lineHeight = options?.lineHeight;
this.wrapWidth = options?.wrapWidth ?? Infinity;
this.outlineColor = options?.outlineColor ?? Color.Transparent;
this.outlineWidth = options?.outlineWidth ?? 0;
this.shadowColor = options?.shadowColor ?? Color.Transparent;
this.shadowOffset = options?.shadowOffset ?? Vector.Zero;
this.shadowBlurRadius = options?.shadowBlurRadius ?? 0;
this.alpha = options?.alpha ?? options?.opacity ?? 1;
if (
hasProperty(excalibur, "Flags") &&
hasProperty(excalibur, "Legacy") &&
hasProperty(excalibur.Legacy, "LegacyDrawing") &&
!excalibur.Flags.isEnabled(excalibur.Legacy.LegacyDrawing)
) {
throw new Error(
"excalibur-extended-label requires you to call Flags.useLegacyDrawing() before constructing Excalibur Engine"
);
}
}
public override draw(context: CanvasRenderingContext2D, delta: number): void {
const shadowVisible =
this.shadowColor.a !== 0 &&
(this.shadowBlurRadius !== 0 || this.shadowOffset.x !== 0 || this.shadowOffset.y !== 0);
const canvas2 = shadowVisible ? offscreenCanvas(context.canvas) : null;
const context2 = canvas2?.getContext("2d") ?? context;
context2.save();
if (context2 !== context) {
context2.setTransform(context.getTransform());
}
context2.translate(this.pos.x, this.pos.y);
context2.scale(this.scale.x, this.scale.y);
context2.rotate(this.rotation);
context2.textAlign = lookupTextAlign(this.textAlign);
context2.textBaseline = lookupBaseAlign(this.baseAlign);
context2.font = `${lookupFontStyle(this.fontStyle)} ${lookupFontWeight(this.bold)} ${
this.fontSize
}px ${this.fontFamily}`;
context2.lineWidth = this.outlineWidth * 2;
context2.strokeStyle =
this.outlineWidth === 0 ? "transparent" : this.outlineColor.toString();
context2.fillStyle = this.color.toString();
const lines = this.wrapLines(context2);
const lineHeight = this.lineHeight ?? this.fontSize;
lines.forEach((line, i) => void context2.strokeText(line, 0, i * lineHeight));
lines.forEach((line, i) => void context2.fillText(line, 0, i * lineHeight));
context2.restore();
if (canvas2 != null) {
context.save();
context.resetTransform();
context.shadowBlur = this.shadowBlurRadius;
context.shadowColor = this.shadowColor.toString();
context.shadowOffsetX = this.shadowOffset.x;
context.shadowOffsetY = this.shadowOffset.y;
context.globalAlpha = this.alpha;
context.drawImage(canvas2, 0, 0);
context.restore();
}
}
private wrapLines(context: CanvasRenderingContext2D): string[] {
const lines = this.text.split("\n");
if (isFinite(this.wrapWidth)) {
return chain(lines)
.map(mapFn(line => line.split(/\s+/u)))
.map(
mapFn(
foldFn(
([line, ...lines], word) =>
line == null
? [word]
: context.measureText(`${line} ${word}`).width < this.wrapWidth
? [`${line} ${word}`, ...lines]
: [word, line, ...lines],
[] as string[]
)
)
)
.map(concat)
.map(toArray)
.value.reverse();
} else {
return lines;
}
}
}
function lookupTextAlign(textAlign: TextAlign): CanvasTextAlign {
switch (textAlign) {
case TextAlign.Left:
return "left";
case TextAlign.Right:
return "right";
case TextAlign.Center:
return "center";
case TextAlign.End:
return "end";
case TextAlign.Start:
return "start";
}
}
function lookupBaseAlign(baseAlign: BaseAlign): CanvasTextBaseline {
switch (baseAlign) {
case BaseAlign.Alphabetic:
return "alphabetic";
case BaseAlign.Bottom:
return "bottom";
case BaseAlign.Hanging:
return "hanging";
case BaseAlign.Ideographic:
return "ideographic";
case BaseAlign.Middle:
return "middle";
case BaseAlign.Top:
return "top";
}
}
function lookupFontStyle(fontStyle: FontStyle): string {
switch (fontStyle) {
case FontStyle.Italic:
return "italic";
case FontStyle.Normal:
return "normal";
case FontStyle.Oblique:
return "oblique";
}
}
function lookupFontWeight(bold: boolean): string {
return bold ? "bold" : "normal";
}