@tsparticles/updater-life
Version:
tsParticles particles life updater
163 lines (153 loc) • 9.12 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);
/* Updater v4.3.2 */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tsparticles/engine')) :
typeof define === 'function' && define.amd ? define(['exports', '@tsparticles/engine'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.__tsParticlesInternals = global.__tsParticlesInternals || {}, global.__tsParticlesInternals.updaters = global.__tsParticlesInternals.updaters || {}, global.__tsParticlesInternals.updaters.life = global.__tsParticlesInternals.updaters.life || {}), global.__tsParticlesInternals.engine));
})(this, (function (exports, engine) { 'use strict';
class LifeDelay extends engine.ValueWithRandom {
sync = false;
load(data) {
if (engine.isNull(data)) {
return;
}
super.load(data);
engine.loadProperty(this, "sync", data.sync);
}
}
class LifeDuration extends engine.ValueWithRandom {
sync = false;
load(data) {
if (engine.isNull(data)) {
return;
}
super.load(data);
engine.loadProperty(this, "sync", data.sync);
}
}
class Life {
count = 0;
delay = new LifeDelay();
duration = new LifeDuration();
load(data) {
if (engine.isNull(data)) {
return;
}
engine.loadProperty(this, "count", data.count);
this.delay.load(data.delay);
this.duration.load(data.duration);
}
}
const noTime$1 = 0, infiniteValue$1 = -1, noLife = 0, minCanvasSize = 0;
function updateLife(particle, delta, canvasSize) {
if (!particle.life) {
return;
}
const life = particle.life;
let justSpawned = false;
if (particle.spawning) {
life.delayTime += delta.value;
if (life.delayTime >= particle.life.delay) {
justSpawned = true;
particle.spawning = false;
life.delayTime = noTime$1;
life.time = noTime$1;
}
else {
return;
}
}
if (life.duration === infiniteValue$1) {
return;
}
if (justSpawned) {
life.time = noTime$1;
}
else {
life.time += delta.value;
}
if (life.time < life.duration) {
return;
}
life.time = noTime$1;
if (particle.life.count > noLife) {
particle.life.count--;
}
if (particle.life.count === noLife) {
particle.destroy();
return;
}
const widthRange = engine.setRangeValue(minCanvasSize, canvasSize.width), heightRange = engine.setRangeValue(minCanvasSize, canvasSize.width);
particle.position.x = engine.randomInRangeValue(widthRange);
particle.position.y = engine.randomInRangeValue(heightRange);
particle.spawning = true;
life.delayTime = noTime$1;
life.time = noTime$1;
particle.reset();
const lifeOptions = particle.options.life;
if (lifeOptions) {
life.delay = engine.getRangeValue(lifeOptions.delay.value) * engine.millisecondsToSeconds;
life.duration = engine.getRangeValue(lifeOptions.duration.value) * engine.millisecondsToSeconds;
}
}
const noTime = 0, identity = 1, infiniteValue = -1;
class LifeUpdater {
#container;
constructor(container) {
this.#container = container;
}
init(particle) {
const container = this.#container, particlesOptions = particle.options, lifeOptions = particlesOptions.life;
if (!lifeOptions) {
return;
}
const delayFactor = lifeOptions.delay.sync ? identity : engine.getRandom(), durationFactor = lifeOptions.duration.sync ? identity : engine.getRandom();
particle.life = {
delay: container.retina.reduceFactor
? ((engine.getRangeValue(lifeOptions.delay.value) * delayFactor) / container.retina.reduceFactor) *
engine.millisecondsToSeconds
: noTime,
delayTime: noTime,
duration: container.retina.reduceFactor
? ((engine.getRangeValue(lifeOptions.duration.value) * durationFactor) / container.retina.reduceFactor) *
engine.millisecondsToSeconds
: noTime,
time: noTime,
count: lifeOptions.count,
};
if (particle.life.duration <= noTime) {
particle.life.duration = infiniteValue;
}
if (particle.life.count <= noTime) {
particle.life.count = infiniteValue;
}
particle.spawning = particle.life.delay > noTime;
}
isEnabled(particle) {
return !particle.destroyed;
}
loadOptions(options, ...sources) {
engine.loadOptionProperty(options, "life", Life, ...sources);
}
update(particle, delta) {
if (!this.isEnabled(particle) || !particle.life) {
return;
}
updateLife(particle, delta, this.#container.canvas.size);
}
}
async function loadLifeUpdater(engine) {
engine.checkVersion("4.3.2");
await engine.pluginManager.register(e => {
e.pluginManager.addParticleUpdater("life", container => {
return Promise.resolve(new LifeUpdater(container));
});
});
}
const globalObject = globalThis;
globalObject.__tsParticlesInternals = globalObject.__tsParticlesInternals ?? {};
globalObject.loadLifeUpdater = loadLifeUpdater;
exports.loadLifeUpdater = loadLifeUpdater;
}));
Object.assign(globalThis.window || globalThis, { loadLifeUpdater: (globalThis.__tsParticlesInternals.updaters.life || {}).loadLifeUpdater });
delete (globalThis.window || globalThis).tsparticlesInternalExports;