UNPKG

2.85 kBJavaScriptView Raw
1var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10import { Utils } from "../Utils";
11import { ShapeType } from "../Enums";
12export class TextDrawer {
13 getSidesCount() {
14 return 12;
15 }
16 init(container) {
17 var _a;
18 return __awaiter(this, void 0, void 0, function* () {
19 const options = container.actualOptions;
20 if (Utils.isInArray(ShapeType.char, options.particles.shape.type) ||
21 Utils.isInArray(ShapeType.character, options.particles.shape.type)) {
22 const shapeOptions = ((_a = options.particles.shape.options[ShapeType.character]) !== null && _a !== void 0 ? _a : options.particles.shape.options[ShapeType.char]);
23 if (shapeOptions instanceof Array) {
24 for (const character of shapeOptions) {
25 yield Utils.loadFont(character);
26 }
27 }
28 else {
29 if (shapeOptions !== undefined) {
30 yield Utils.loadFont(shapeOptions);
31 }
32 }
33 }
34 });
35 }
36 draw(context, particle, radius) {
37 const character = particle.shapeData;
38 if (character === undefined) {
39 return;
40 }
41 const textData = character.value;
42 if (textData === undefined) {
43 return;
44 }
45 const textParticle = particle;
46 if (textParticle.text === undefined) {
47 textParticle.text =
48 textData instanceof Array ? Utils.itemFromArray(textData, particle.randomIndexData) : textData;
49 }
50 const text = textParticle.text;
51 const style = character.style;
52 const weight = character.weight;
53 const size = Math.round(radius) * 2;
54 const font = character.font;
55 const fill = particle.fill;
56 const offsetX = (text.length * radius) / 2;
57 context.font = `${style} ${weight} ${size}px "${font}"`;
58 const pos = {
59 x: -offsetX,
60 y: radius / 2,
61 };
62 if (fill) {
63 context.fillText(text, pos.x, pos.y);
64 }
65 else {
66 context.strokeText(text, pos.x, pos.y);
67 }
68 }
69}