/
TextDrawer.ts
66 lines (54 loc) · 1.91 KB
/
TextDrawer.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
import {
type Container,
type IShapeDrawData,
type IShapeDrawer,
type SingleOrMultiple,
executeOnSingleOrMultiple,
isInArray,
itemFromSingleOrMultiple,
loadFont,
} from "@tsparticles/engine";
import type { ITextShape } from "./ITextShape.js";
import type { TextParticle } from "./TextParticle.js";
import { drawText } from "./Utils.js";
export const validTypes = ["text", "character", "char", "multiline-text"];
/**
* Multiline text drawer
*/
export class TextDrawer implements IShapeDrawer<TextParticle> {
draw(data: IShapeDrawData<TextParticle>): void {
drawText(data);
}
async init(container: Container): Promise<void> {
const options = container.actualOptions;
if (validTypes.find((t) => isInArray(t, options.particles.shape.type))) {
const shapeOptions = validTypes
.map((t) => options.particles.shape.options[t])
.find((t) => !!t) as SingleOrMultiple<ITextShape>,
promises: Promise<void>[] = [];
executeOnSingleOrMultiple(shapeOptions, (shape) => {
promises.push(loadFont(shape.font, shape.weight));
});
await Promise.all(promises);
}
}
/**
* Loads the text shape to the given particle
* @param container - the particles container
* @param particle - the particle loading the text shape
*/
particleInit(container: Container, particle: TextParticle): void {
if (!particle.shape || !validTypes.includes(particle.shape)) {
return;
}
const character = particle.shapeData as ITextShape | undefined;
if (character === undefined) {
return;
}
const textData = character.value;
if (textData === undefined) {
return;
}
particle.text = itemFromSingleOrMultiple(textData, particle.randomIndexData);
}
}