@tsparticles/confetti
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.
95 lines (94 loc) • 3.15 kB
JavaScript
import { isString, tsParticles } from "@tsparticles/engine";
import { loadBasic } from "@tsparticles/basic";
import { loadCardSuitsShape } from "@tsparticles/shape-cards/suits";
import { loadEmittersPluginSimple } from "@tsparticles/plugin-emitters/plugin";
import { loadEmojiShape } from "@tsparticles/shape-emoji";
import { loadHeartShape } from "@tsparticles/shape-heart";
import { loadImageShape } from "@tsparticles/shape-image";
import { loadLifeUpdater } from "@tsparticles/updater-life";
import { loadMotionPlugin } from "@tsparticles/plugin-motion";
import { loadPolygonShape } from "@tsparticles/shape-polygon";
import { loadRollUpdater } from "@tsparticles/updater-roll";
import { loadRotateUpdater } from "@tsparticles/updater-rotate";
import { loadSquareShape } from "@tsparticles/shape-square";
import { loadStarShape } from "@tsparticles/shape-star";
import { loadTiltUpdater } from "@tsparticles/updater-tilt";
import { loadWobbleUpdater } from "@tsparticles/updater-wobble";
import { setConfetti } from "./utils.js";
let initPromise = null;
async function doInitPlugins(engine) {
engine.checkVersion("4.0.5");
await engine.pluginManager.register(async (e) => {
await Promise.all([
loadBasic(e),
loadMotionPlugin(e),
loadEmittersPluginSimple(e),
loadCardSuitsShape(e),
loadHeartShape(e),
loadImageShape(e),
loadPolygonShape(e),
loadSquareShape(e),
loadStarShape(e),
loadEmojiShape(e),
loadRotateUpdater(e),
loadLifeUpdater(e),
loadRollUpdater(e),
loadTiltUpdater(e),
loadWobbleUpdater(e),
]);
});
}
async function initPlugins(engine) {
if (initPromise) {
return initPromise;
}
initPromise = doInitPlugins(engine);
return initPromise;
}
export async function confetti(idOrOptions, confettiOptions) {
await initPlugins(tsParticles);
let options, id;
if (isString(idOrOptions)) {
id = idOrOptions;
options = confettiOptions ?? {};
}
else {
id = "confetti";
options = idOrOptions;
}
return setConfetti(tsParticles, {
id,
options,
});
}
confetti.create = async (canvas, options = {}) => {
await initPlugins(tsParticles);
const id = canvas?.getAttribute("id") ?? "confetti";
canvas?.setAttribute("id", id);
await setConfetti(tsParticles, {
id,
canvas: canvas ?? undefined,
options,
});
return async (idOrOptions, confettiOptions) => {
let subOptions, subId;
if (isString(idOrOptions)) {
subId = idOrOptions;
subOptions = confettiOptions ?? options;
}
else {
subId = id;
subOptions = idOrOptions;
}
return setConfetti(tsParticles, {
id: subId,
canvas: canvas ?? undefined,
options: subOptions,
});
};
};
confetti.init = async () => {
await initPlugins(tsParticles);
};
confetti.version = "4.0.5";
globalThis.confetti = confetti;