@tsparticles/engine
Version:
Easily create highly customizable particle, confetti and fireworks 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.
142 lines (141 loc) • 5.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParticlesOptions = void 0;
const Utils_js_1 = require("../../../Utils/Utils.js");
const AnimatableColor_js_1 = require("../AnimatableColor.js");
const Effect_js_1 = require("./Effect/Effect.js");
const Fill_js_1 = require("./Fill.js");
const Move_js_1 = require("./Move/Move.js");
const OptionLoader_js_1 = require("../../../Utils/OptionLoader.js");
const Paint_js_1 = require("./Paint.js");
const ParticlesBounce_js_1 = require("./Bounce/ParticlesBounce.js");
const ParticlesNumber_js_1 = require("./Number/ParticlesNumber.js");
const Shape_js_1 = require("./Shape/Shape.js");
const ZIndex_js_1 = require("./ZIndex/ZIndex.js");
const TypeUtils_js_1 = require("../../../Utils/TypeUtils.js");
class ParticlesOptions extends OptionLoader_js_1.OptionLoader {
bounce = new ParticlesBounce_js_1.ParticlesBounce();
effect = new Effect_js_1.Effect();
groups = {};
move = new Move_js_1.Move();
number = new ParticlesNumber_js_1.ParticlesNumber();
paint;
palette;
reduceDuplicates = false;
shape = new Shape_js_1.Shape();
zIndex = new ZIndex_js_1.ZIndex();
#container;
#pluginManager;
constructor(pluginManager, container) {
super();
this.#pluginManager = pluginManager;
this.#container = container;
this.paint = new Paint_js_1.Paint();
this.paint.color = new AnimatableColor_js_1.AnimatableColor();
this.paint.color.value = "#fff";
this.paint.fill = new Fill_js_1.Fill();
this.paint.fill.enable = true;
}
doLoad(data) {
if (data.palette) {
this.palette = data.palette;
this.#importPalette(this.palette);
}
if (data.groups !== undefined) {
for (const group of Object.keys(data.groups)) {
if (!(group in data.groups)) {
continue;
}
const item = data.groups[group];
if (item !== undefined) {
this.groups[group] = (0, Utils_js_1.deepExtend)(this.groups[group] ?? {}, item);
}
}
}
if (data.reduceDuplicates !== undefined) {
this.reduceDuplicates = data.reduceDuplicates;
}
this.bounce.load(data.bounce);
this.effect.load(data.effect);
this.move.load(data.move);
this.number.load(data.number);
const paintToLoad = data.paint;
if (paintToLoad) {
if ((0, TypeUtils_js_1.isArray)(paintToLoad)) {
this.paint = (0, Utils_js_1.executeOnSingleOrMultiple)(paintToLoad, t => {
const tmp = new Paint_js_1.Paint();
tmp.load(t);
return tmp;
});
}
else if ((0, TypeUtils_js_1.isArray)(this.paint)) {
this.paint = new Paint_js_1.Paint();
this.paint.load(paintToLoad);
}
else {
this.paint.load(paintToLoad);
}
}
this.shape.load(data.shape);
this.zIndex.load(data.zIndex);
if (this.#container) {
for (const plugin of this.#pluginManager.plugins) {
if (plugin.loadParticlesOptions) {
plugin.loadParticlesOptions(this.#container, this, data);
}
}
const updaters = this.#container.particleUpdaters;
for (const updater of updaters) {
if (updater.loadOptions) {
updater.loadOptions(this, data);
}
}
}
}
#importPalette(palette) {
const paletteData = this.#pluginManager.getPalette(palette);
if (!paletteData) {
return;
}
const paletteColors = paletteData.colors, defaultPaintStrokeWidth = 0, defaultPaintVariantsLength = 1, firstPaintVariantIndex = 0, defaultPalettePaintVariant = {}, colorVariants = (0, TypeUtils_js_1.isArray)(paletteColors) ? paletteColors : [paletteColors], palettePaintVariants = colorVariants.flatMap(variant => {
const paletteFill = variant.fill, paletteStroke = variant.stroke, fillPart = paletteFill
? {
color: {
value: paletteFill.value,
},
enable: paletteFill.enable,
opacity: paletteFill.opacity,
}
: undefined;
if (!paletteStroke) {
return [
{
fill: fillPart,
},
];
}
return [
{
fill: fillPart,
stroke: {
color: {
value: paletteStroke.value,
},
opacity: paletteStroke.opacity,
width: paletteStroke.width || defaultPaintStrokeWidth,
},
},
];
}), palettePaint = palettePaintVariants.length > defaultPaintVariantsLength
? palettePaintVariants
: (palettePaintVariants[firstPaintVariantIndex] ?? defaultPalettePaintVariant);
this.load({
paint: palettePaint,
blend: {
enable: true,
mode: paletteData.blendMode,
},
});
}
}
exports.ParticlesOptions = ParticlesOptions;