@tsparticles/shape-text
Version:
tsParticles text shape
102 lines (95 loc) • 7.69 kB
JavaScript
(function(g){g.__tsParticlesInternals=g.__tsParticlesInternals||{};g.__tsParticlesInternals.bundles=g.__tsParticlesInternals.bundles||{};g.__tsParticlesInternals.effects=g.__tsParticlesInternals.effects||{};g.__tsParticlesInternals.engine=g.__tsParticlesInternals.engine||{};g.__tsParticlesInternals.interactions=g.__tsParticlesInternals.interactions||{};g.__tsParticlesInternals.palettes=g.__tsParticlesInternals.palettes||{};g.__tsParticlesInternals.paths=g.__tsParticlesInternals.paths||{};g.__tsParticlesInternals.plugins=g.__tsParticlesInternals.plugins||{};g.__tsParticlesInternals.plugins=g.__tsParticlesInternals.plugins||{};g.__tsParticlesInternals.plugins.emittersShapes=g.__tsParticlesInternals.plugins.emittersShapes||{};g.__tsParticlesInternals.presets=g.__tsParticlesInternals.presets||{};g.__tsParticlesInternals.shapes=g.__tsParticlesInternals.shapes||{};g.__tsParticlesInternals.updaters=g.__tsParticlesInternals.updaters||{};g.__tsParticlesInternals.utils=g.__tsParticlesInternals.utils||{};g.__tsParticlesInternals.canvas=g.__tsParticlesInternals.canvas||{};g.__tsParticlesInternals.canvas=g.__tsParticlesInternals.canvas||{};g.__tsParticlesInternals.canvas.utils=g.__tsParticlesInternals.canvas.utils||{};g.__tsParticlesInternals.path=g.__tsParticlesInternals.path||{};g.__tsParticlesInternals.path=g.__tsParticlesInternals.path||{};g.__tsParticlesInternals.path.utils=g.__tsParticlesInternals.path.utils||{};var __tsProxyFactory=typeof Proxy!=="undefined"?function(obj){return new Proxy(obj,{get:function(target,key){if(!(key in target)){target[key]={};}return target[key];}});}:function(obj){return obj;};g.__tsParticlesInternals.bundles=__tsProxyFactory(g.__tsParticlesInternals.bundles);g.__tsParticlesInternals.effects=__tsProxyFactory(g.__tsParticlesInternals.effects);g.__tsParticlesInternals.interactions=__tsProxyFactory(g.__tsParticlesInternals.interactions);g.__tsParticlesInternals.palettes=__tsProxyFactory(g.__tsParticlesInternals.palettes);g.__tsParticlesInternals.paths=__tsProxyFactory(g.__tsParticlesInternals.paths);g.__tsParticlesInternals.plugins=__tsProxyFactory(g.__tsParticlesInternals.plugins);g.__tsParticlesInternals.plugins.emittersShapes=__tsProxyFactory(g.__tsParticlesInternals.plugins.emittersShapes);g.__tsParticlesInternals.presets=__tsProxyFactory(g.__tsParticlesInternals.presets);g.__tsParticlesInternals.shapes=__tsProxyFactory(g.__tsParticlesInternals.shapes);g.__tsParticlesInternals.updaters=__tsProxyFactory(g.__tsParticlesInternals.updaters);g.__tsParticlesInternals.utils=__tsProxyFactory(g.__tsParticlesInternals.utils);g.__tsParticlesInternals.canvas=__tsProxyFactory(g.__tsParticlesInternals.canvas);g.__tsParticlesInternals.path=__tsProxyFactory(g.__tsParticlesInternals.path);g.tsparticlesInternalExports=g.tsparticlesInternalExports||{};})(typeof globalThis!=="undefined"?globalThis:typeof window!=="undefined"?window:this);
/* Shape v4.1.0 */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tsparticles/engine'), require('@tsparticles/canvas-utils')) :
typeof define === 'function' && define.amd ? define(['exports', '@tsparticles/engine', '@tsparticles/canvas-utils'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.__tsParticlesInternals = global.__tsParticlesInternals || {}, global.__tsParticlesInternals.shapes = global.__tsParticlesInternals.shapes || {}, global.__tsParticlesInternals.shapes.text = global.__tsParticlesInternals.shapes.text || {}), global.__tsParticlesInternals.engine, global.__tsParticlesInternals.canvas.utils));
})(this, (function (exports, engine, canvasUtils) { 'use strict';
const validTypes = ["text", "character", "char", "multiline-text"];
const firstIndex$1 = 0, minLength$1 = 0;
function drawText(data) {
const { context, particle, fill, stroke, radius, opacity } = data, character = particle.shapeData;
if (!character) {
return;
}
const textData = character.value;
particle.textLines ??= engine.itemFromSingleOrMultiple(textData, particle.randomIndexData)?.split("\n") ?? [];
particle.maxTextLength ??= particle.textLines.length
? Math.max(...particle.textLines.map(t => t.length))
: (particle.textLines[firstIndex$1]?.length ?? minLength$1);
if (!particle.textLines.length || !particle.maxTextLength) {
return;
}
const lines = particle.textLines, style = character.style ?? "", weight = character.weight ?? "400", font = character.font ?? "Verdana", size = (Math.round(radius) * engine.double) / (lines.length * particle.maxTextLength);
context.font = `${style} ${weight} ${size.toString()}px "${font}"`;
const originalGlobalAlpha = context.globalAlpha;
context.globalAlpha = opacity;
for (let i = 0; i < lines.length; i++) {
const currentLine = lines[i];
if (!currentLine) {
continue;
}
drawTextLine(context, currentLine, size, i, fill, stroke);
}
context.globalAlpha = originalGlobalAlpha;
}
function drawTextLine(context, line, size, index, fill, stroke) {
const pos = {
x: -(line.length * size * engine.half),
y: size * engine.half + index * size,
};
if (fill) {
context.fillText(line, pos.x, pos.y);
}
if (stroke) {
context.strokeText(line, pos.x, pos.y);
}
}
const firstIndex = 0, minLength = 0;
class TextDrawer {
draw(data) {
drawText(data);
}
async init(container) {
const options = container.actualOptions;
if (validTypes.find(t => engine.isInArray(t, options.particles.shape.type))) {
const shapeOptions = validTypes
.map(t => options.particles.shape.options[t])
.find(t => !!t), promises = [];
engine.executeOnSingleOrMultiple(shapeOptions, shape => {
promises.push(canvasUtils.loadFont(shape.font, shape.weight));
});
await Promise.all(promises);
}
}
particleInit(_container, particle) {
if (!particle.shape || !validTypes.includes(particle.shape)) {
return;
}
const character = particle.shapeData;
if (character === undefined) {
return;
}
const textData = character.value;
if (!textData) {
return;
}
particle.textLines = engine.itemFromSingleOrMultiple(textData, particle.randomIndexData)?.split("\n") ?? [];
particle.maxTextLength = particle.textLines.length
? Math.max(...particle.textLines.map(t => t.length))
: (particle.textLines[firstIndex]?.length ?? minLength);
}
}
async function loadTextShape(engine) {
engine.checkVersion("4.1.0");
await engine.pluginManager.register(e => {
e.pluginManager.addShape(validTypes, () => Promise.resolve(new TextDrawer()));
});
}
const globalObject = globalThis;
globalObject.__tsParticlesInternals = globalObject.__tsParticlesInternals ?? {};
globalObject.loadTextShape = loadTextShape;
exports.loadTextShape = loadTextShape;
}));
Object.assign(globalThis.window || globalThis, { loadTextShape: (globalThis.__tsParticlesInternals.shapes.text || {}).loadTextShape });
delete (globalThis.window || globalThis).tsparticlesInternalExports;