2023-02-17 11:06:01 +01:00

64 lines
2.9 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextDrawer = exports.validTypes = void 0;
const tsparticles_engine_1 = require("tsparticles-engine");
exports.validTypes = ["text", "character", "char"];
class TextDrawer {
draw(context, particle, radius, opacity) {
var _a, _b, _c;
const character = particle.shapeData;
if (character === undefined) {
return;
}
const textData = character.value;
if (textData === undefined) {
return;
}
const textParticle = particle;
if (textParticle.text === undefined) {
textParticle.text = (0, tsparticles_engine_1.itemFromSingleOrMultiple)(textData, particle.randomIndexData);
}
const text = textParticle.text, style = (_a = character.style) !== null && _a !== void 0 ? _a : "", weight = (_b = character.weight) !== null && _b !== void 0 ? _b : "400", size = Math.round(radius) * 2, font = (_c = character.font) !== null && _c !== void 0 ? _c : "Verdana", fill = particle.fill, offsetX = (text.length * radius) / 2;
context.font = `${style} ${weight} ${size}px "${font}"`;
const pos = {
x: -offsetX,
y: radius / 2,
};
context.globalAlpha = opacity;
if (fill) {
context.fillText(text, pos.x, pos.y);
}
else {
context.strokeText(text, pos.x, pos.y);
}
context.globalAlpha = 1;
}
getSidesCount() {
return 12;
}
init(container) {
return __awaiter(this, void 0, void 0, function* () {
const options = container.actualOptions;
if (exports.validTypes.find((t) => (0, tsparticles_engine_1.isInArray)(t, options.particles.shape.type))) {
const shapeOptions = exports.validTypes
.map((t) => options.particles.shape.options[t])
.find((t) => !!t), promises = [];
(0, tsparticles_engine_1.executeOnSingleOrMultiple)(shapeOptions, (shape) => {
promises.push((0, tsparticles_engine_1.loadFont)(shape.font, shape.weight));
});
yield Promise.all(promises);
}
});
}
}
exports.TextDrawer = TextDrawer;