@tsparticles/updater-life
Version:
tsParticles particles life updater
50 lines (49 loc) • 1.95 kB
JavaScript
import { getRandom, getRangeValue, loadOptionProperty, millisecondsToSeconds, } from "@tsparticles/engine";
import { Life } from "./Options/Classes/Life.js";
import { updateLife } from "./Utils.js";
const noTime = 0, identity = 1, infiniteValue = -1;
export 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 : getRandom(), durationFactor = lifeOptions.duration.sync ? identity : getRandom();
particle.life = {
delay: container.retina.reduceFactor
? ((getRangeValue(lifeOptions.delay.value) * delayFactor) / container.retina.reduceFactor) *
millisecondsToSeconds
: noTime,
delayTime: noTime,
duration: container.retina.reduceFactor
? ((getRangeValue(lifeOptions.duration.value) * durationFactor) / container.retina.reduceFactor) *
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) {
loadOptionProperty(options, "life", Life, ...sources);
}
update(particle, delta) {
if (!this.isEnabled(particle) || !particle.life) {
return;
}
updateLife(particle, delta, this.#container.canvas.size);
}
}