@tsparticles/particles
Version:
Minimal tsParticles particles bundle — lightweight particle engine without confetti or fireworks extras. Perfect for pure particle backgrounds. Ready to use components available for React, Vue.js (2.x and 3.x), Angular, Svelte, jQuery, Preact, Riot.js, In
66 lines (65 loc) • 2.13 kB
JavaScript
const instances = new Map();
export function deleteParticlesInstance(id) {
instances.delete(id);
}
const defaultCount = 80, defaultLinksWidth = 100, defaultSpeed = 3, defaultOpacity = 1, defaultRadius = 3;
function getDefaultOptions(options, canvas) {
return {
fullScreen: {
enable: !canvas,
},
particles: {
number: {
value: options.count ?? defaultCount,
},
color: {
value: options.color ?? "#fff",
},
links: {
enable: options.links ?? false,
color: options.linksColor ?? "#fff",
distance: options.linksLength ?? defaultLinksWidth,
},
collisions: {
enable: options.collisions ?? false,
},
move: {
enable: true,
speed: options.speed ?? defaultSpeed,
},
opacity: {
value: options.opacity ?? defaultOpacity,
},
shape: {
type: options.shape ?? "circle",
},
size: {
value: options.radius ?? defaultRadius,
},
},
};
}
export async function getParticlesInstance(engine, id, sourceOptions, canvas) {
const existing = instances.get(id);
if (existing instanceof Promise) {
return existing;
}
if (existing) {
if (!existing.destroyed) {
return existing;
}
instances.delete(id);
}
const create = async () => {
const particlesOptions = getDefaultOptions(sourceOptions, canvas), container = await engine.load({ id, element: canvas, options: particlesOptions });
if (!container) {
instances.delete(id);
return;
}
const { ParticlesInstance } = await import("./ParticlesInstance.js"), instance = new ParticlesInstance(container, id);
instances.set(id, instance);
return instance;
}, createPromise = create();
instances.set(id, createPromise);
return createPromise;
}