tsparticles
Version:
Easily create highly customizable particle animations and use them as animated backgrounds for your website. Ready to use components available also for React, Vue.js (2.x and 3.x), Angular, Svelte, jQuery, Preact, Riot.js, Inferno.
77 lines (76 loc) • 3.24 kB
JavaScript
;
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 Utils_1 = require("../../Utils");
exports.validTypes = ["text", "character", "char"];
class TextDrawer {
getSidesCount() {
return 12;
}
init(container) {
return __awaiter(this, void 0, void 0, function* () {
const options = container.actualOptions;
if (exports.validTypes.find((t) => Utils_1.isInArray(t, options.particles.shape.type))) {
const shapeOptions = exports.validTypes.map((t) => options.particles.shape.options[t]).find((t) => !!t);
if (shapeOptions instanceof Array) {
const promises = [];
for (const character of shapeOptions) {
promises.push(Utils_1.loadFont(character));
}
yield Promise.allSettled(promises);
}
else {
if (shapeOptions !== undefined) {
yield Utils_1.loadFont(shapeOptions);
}
}
}
});
}
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 =
textData instanceof Array ? Utils_1.itemFromArray(textData, particle.randomIndexData) : textData;
}
const text = textParticle.text;
const style = (_a = character.style) !== null && _a !== void 0 ? _a : "";
const weight = (_b = character.weight) !== null && _b !== void 0 ? _b : "400";
const size = Math.round(radius) * 2;
const font = (_c = character.font) !== null && _c !== void 0 ? _c : "Verdana";
const fill = particle.fill;
const 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;
}
}
exports.TextDrawer = TextDrawer;