@tsparticles/particles
Version:
Easily create highly customizable particle 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.
56 lines (55 loc) • 1.89 kB
JavaScript
const instances = new Map(), defaultCount = 80, defaultLinksWidth = 100, defaultSpeed = 3, defaultOpacity = 1, defaultRadius = 3;
function getDefaultOptions(options) {
return {
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) {
return existing;
}
const create = async () => {
const particlesOptions = getDefaultOptions(sourceOptions), 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);
instances.set(id, instance);
return instance;
}, createPromise = create();
instances.set(id, createPromise);
return createPromise;
}