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.
73 lines (72 loc) • 3.07 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());
});
};
import { isInArray, itemFromArray, loadFont } from "../../Utils";
export const validTypes = ["text", "character", "char"];
export class TextDrawer {
getSidesCount() {
return 12;
}
init(container) {
return __awaiter(this, void 0, void 0, function* () {
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);
if (shapeOptions instanceof Array) {
const promises = [];
for (const character of shapeOptions) {
promises.push(loadFont(character));
}
yield Promise.allSettled(promises);
}
else {
if (shapeOptions !== undefined) {
yield 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 ? 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;
}
}