UNPKG

@tsparticles/confetti

Version:

tsParticles confetti bundle — easily create confetti, confetti cannon, confetti explosions, confetti falling, and confetti parade animations with presets. Ready to use components available for React, Vue.js (2.x and 3.x), Angular, Svelte, jQuery, Preact,

280 lines (279 loc) 8.7 kB
import { defaultFps, percentDenominator } from "@tsparticles/engine"; import { ConfettiOptions } from "./ConfettiOptions.js"; const defaultGravity = 9.81, sizeFactor = 5, speedFactor = 3, decayOffset = 1, disableRotate = 0, disableTilt = 0, noOpacityChange = 0, ids = new Map(), minTicks = 0; export async function addEmitter(container, actualOptions, opacitySpeed) { await container.addEmitter?.({ startCount: actualOptions.count, position: actualOptions.position, size: { width: 0, height: 0, }, rate: { delay: 0, quantity: 0, }, life: { duration: 0.1, count: 1, }, particles: { paint: { fill: { color: { value: actualOptions.colors, }, enable: true, }, }, shape: { type: actualOptions.shapes, options: actualOptions.shapeOptions, }, life: { count: 1, }, opacity: { value: { min: 0, max: 1 }, animation: { enable: true, sync: true, speed: opacitySpeed, startValue: "max", destroy: "min", count: 1, }, }, size: { value: sizeFactor * actualOptions.scalar, }, move: { angle: { value: actualOptions.spread, offset: 0, }, drift: { min: -actualOptions.drift, max: actualOptions.drift, }, gravity: { acceleration: actualOptions.gravity * defaultGravity, }, speed: actualOptions.startVelocity * speedFactor, decay: decayOffset - actualOptions.decay, direction: -actualOptions.angle, }, rotate: { value: actualOptions.flat ? disableRotate : { min: 0, max: 360, }, direction: "random", animation: { enable: !actualOptions.flat, speed: 60, }, }, tilt: { direction: "random", enable: !actualOptions.flat, value: actualOptions.flat ? disableTilt : { min: 0, max: 360, }, animation: { enable: true, speed: 60, }, }, roll: { darken: { enable: true, value: 25, }, enable: !actualOptions.flat, speed: { min: 15, max: 25, }, }, wobble: { distance: 30, enable: !actualOptions.flat, speed: { min: -15, max: 15, }, }, }, }); } export function convertOptions(actualOptions, params, opacitySpeed) { return { fullScreen: { enable: !params.canvas, zIndex: actualOptions.zIndex, }, fpsLimit: 120, particles: { number: { value: 0, }, paint: { fill: { color: { value: actualOptions.colors, }, enable: true, }, }, shape: { type: actualOptions.shapes, options: actualOptions.shapeOptions, }, opacity: { value: { min: 0, max: 1 }, animation: { enable: true, sync: true, speed: opacitySpeed, startValue: "max", destroy: "min", count: 1, }, }, size: { value: sizeFactor * actualOptions.scalar, }, links: { enable: false, }, life: { count: 1, }, move: { angle: { value: actualOptions.spread, offset: 0, }, drift: { min: -actualOptions.drift, max: actualOptions.drift, }, enable: true, gravity: { enable: true, acceleration: actualOptions.gravity * defaultGravity, }, speed: actualOptions.startVelocity * speedFactor, decay: decayOffset - actualOptions.decay, direction: -actualOptions.angle, random: true, straight: false, outModes: { top: "none", default: "destroy", }, }, rotate: { value: actualOptions.flat ? disableRotate : { min: 0, max: 360, }, direction: "random", animation: { enable: !actualOptions.flat, speed: 60, }, }, tilt: { direction: "random", enable: !actualOptions.flat, value: actualOptions.flat ? disableTilt : { min: 0, max: 360, }, animation: { enable: true, speed: 60, }, }, roll: { darken: { enable: true, value: 25, }, enable: !actualOptions.flat, speed: { min: 15, max: 25, }, }, wobble: { distance: 30, enable: !actualOptions.flat, speed: { min: -15, max: 15, }, }, }, motion: { disable: actualOptions.disableForReducedMotion, }, emitters: { name: "confetti", startCount: actualOptions.count, position: actualOptions.position, size: { width: 0, height: 0, }, rate: { delay: 0, quantity: 0, }, life: { duration: 0.1, count: 1, }, }, }; } export async function setConfetti(engine, params) { const actualOptions = new ConfettiOptions(); actualOptions.load(params.options); const fpsLimit = 120, safeTicks = Number.isFinite(actualOptions.ticks) && actualOptions.ticks > minTicks ? actualOptions.ticks : undefined, opacitySpeed = safeTicks === undefined ? noOpacityChange : (fpsLimit * percentDenominator) / (defaultFps * safeTicks); let containerOrPromise = ids.get(params.id); if (containerOrPromise instanceof Promise) { await containerOrPromise; containerOrPromise = ids.get(params.id); } const container = containerOrPromise; if (container && !container.destroyed) { const alias = container; if ("addEmitter" in alias) { await addEmitter(alias, actualOptions, opacitySpeed); return container; } } const create = async () => { const particlesOptions = convertOptions(actualOptions, params, opacitySpeed), newContainer = await engine.load({ id: params.id, element: params.canvas, options: particlesOptions, }); ids.set(params.id, newContainer); return newContainer; }, createPromise = create(); ids.set(params.id, createPromise); return createPromise; }