partycles
Version:
🎉 Delightful particle animations for React
142 lines (134 loc) • 4.01 kB
TypeScript
import { RefObject } from 'react';
interface Particle {
id: string;
x: number;
y: number;
vx: number;
vy: number;
life: number;
opacity: number;
size: number;
rotation: number;
color: string;
element?: React.ReactNode;
config?: AnimationConfig;
}
interface AnimationConfig {
particleCount?: number;
spread?: number;
startVelocity?: number;
decay?: number;
lifetime?: number;
colors?: string[];
elementSize?: number;
duration?: number;
physics?: {
gravity?: number;
wind?: number;
friction?: number;
};
effects?: {
flutter?: boolean;
twinkle?: boolean;
pulse?: boolean;
spin3D?: boolean;
wobble?: boolean;
windDrift?: boolean;
};
}
type AnimationType = 'confetti' | 'sparkles' | 'fireworks' | 'hearts' | 'stars' | 'bubbles' | 'snow' | 'emoji' | 'coins' | 'petals' | 'aurora' | 'fireflies' | 'paint' | 'balloons' | 'galaxy' | 'leaves' | 'glitch' | 'magicdust' | 'crystals';
interface UseRewardConfig extends AnimationConfig {
animationType: AnimationType;
}
interface AnimationControls {
pause: () => void;
resume: () => void;
replay: () => Promise<void>;
isPaused: boolean;
}
interface UseRewardReturn extends AnimationControls {
reward: () => Promise<void>;
isAnimating: boolean;
targetRef?: RefObject<HTMLElement>;
}
declare function useReward(elementId: string, animationType: AnimationType, config?: AnimationConfig): UseRewardReturn;
declare function useReward(targetRef: RefObject<HTMLElement>, animationType: AnimationType, config?: AnimationConfig): UseRewardReturn;
declare const emojiPresets: {
celebration: string[];
love: string[];
happy: string[];
nature: string[];
food: string[];
default: string[];
};
declare const isMobileDevice: () => boolean;
declare const optimizeConfigForMobile: (config: AnimationConfig) => AnimationConfig;
interface PooledParticle extends Particle {
_pooled?: boolean;
}
declare class ParticlePool {
private pool;
private maxSize;
private created;
constructor(maxSize?: number);
acquire(): PooledParticle;
release(particle: PooledParticle): void;
releaseAll(particles: PooledParticle[]): void;
clear(): void;
getStats(): {
poolSize: number;
totalCreated: number;
maxSize: number;
};
}
declare const particlePool: ParticlePool;
interface AnimationInstance {
id: string;
particles: Particle[];
containerElement: HTMLDivElement;
renderFunction: (particle: Particle) => React.ReactNode;
updateCallback?: (particles: Particle[]) => void;
onComplete?: () => void;
physics: {
gravity: number;
friction: number;
wind: number;
};
animationType: string;
config?: any;
frameCount: number;
isPaused?: boolean;
pausedAt?: number;
}
declare class AnimationManager {
private animations;
private rafId;
private isRunning;
private lastFrameTime;
private frameInterval;
private frameCount;
private fpsUpdateInterval;
private lastFpsUpdate;
private currentFps;
constructor();
addAnimation(animation: AnimationInstance): void;
removeAnimation(id: string): void;
private start;
private stop;
private update;
private applyEffects;
private updateOpacity;
getStats(): {
activeAnimations: number;
currentFps: number;
isRunning: boolean;
totalParticles: number;
};
setTargetFPS(fps: number): void;
pauseAnimation(id: string): void;
resumeAnimation(id: string): void;
isAnimationPaused(id: string): boolean;
getAnimation(id: string): AnimationInstance | undefined;
}
declare const animationManager: AnimationManager;
export { type AnimationConfig, type AnimationType, type UseRewardConfig, animationManager, emojiPresets, isMobileDevice, optimizeConfigForMobile, particlePool, useReward };