Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
Add a fun particle effect that follows mouse interactions, with support for emojis, images, and custom shapes.
cool-mode.svelte
<script lang="ts"> import { CoolMode } from "$lib/components/magic/cool-mode/index.js"; import { Button } from "$lib/components/ui/button/index.js";</script>
<div class="relative flex justify-center"> <CoolMode> <Button>Click Me!</Button> </CoolMode></div>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/cool-mode.json
Copy the Source Code
Copy and paste the following code into your project:
cool-mode.svelte
<script lang="ts"> import { onMount, onDestroy } from "svelte"; import type { Snippet } from "svelte";
export interface BaseParticle { element: HTMLElement | SVGSVGElement; left: number; size: number; top: number; }
export interface BaseParticleOptions { particle?: string; size?: number; }
export interface CoolParticle extends BaseParticle { direction: number; speedHorz: number; speedUp: number; spinSpeed: number; spinVal: number; }
export interface CoolParticleOptions extends BaseParticleOptions { particleCount?: number; speedHorz?: number; speedUp?: number; }
interface CoolModeProps { children: Snippet; options?: CoolParticleOptions; }
let { children, options }: CoolModeProps = $props();
let ref: HTMLSpanElement; let cleanup: (() => void) | undefined;
const getContainer = () => { const id = "_coolMode_effect"; const existingContainer = document.getElementById(id);
if (existingContainer) { return existingContainer; }
const container = document.createElement("div"); container.setAttribute("id", id); container.setAttribute( "style", "overflow:hidden; position:fixed; height:100%; top:0; left:0; right:0; bottom:0; pointer-events:none; z-index:2147483647" );
document.body.appendChild(container);
return container; };
let instanceCounter = 0;
const applyParticleEffect = ( element: HTMLElement, options?: CoolParticleOptions ): (() => void) => { instanceCounter++;
const defaultParticle = "circle"; const particleType = options?.particle || defaultParticle; const sizes = [15, 20, 25, 35, 45]; const limit = 45;
let particles: CoolParticle[] = []; let autoAddParticle = false; let mouseX = 0; let mouseY = 0;
const container = getContainer();
function generateParticle() { const size = options?.size || sizes[Math.floor(Math.random() * sizes.length)]; const speedHorz = options?.speedHorz || Math.random() * 10; const speedUp = options?.speedUp || Math.random() * 25; const spinVal = Math.random() * 360; const spinSpeed = Math.random() * 35 * (Math.random() <= 0.5 ? -1 : 1); const top = mouseY - size / 2; const left = mouseX - size / 2; const direction = Math.random() <= 0.5 ? -1 : 1;
const particle = document.createElement("div");
if (particleType === "circle") { const svgNS = "http://www.w3.org/2000/svg"; const circleSVG = document.createElementNS(svgNS, "svg"); const circle = document.createElementNS(svgNS, "circle"); circle.setAttributeNS(null, "cx", (size / 2).toString()); circle.setAttributeNS(null, "cy", (size / 2).toString()); circle.setAttributeNS(null, "r", (size / 2).toString()); circle.setAttributeNS(null, "fill", `hsl(${Math.random() * 360}, 70%, 50%)`);
circleSVG.appendChild(circle); circleSVG.setAttribute("width", size.toString()); circleSVG.setAttribute("height", size.toString());
particle.appendChild(circleSVG); } else if (particleType.startsWith("http") || particleType.startsWith("/")) { // Handle URL-based images particle.innerHTML = `<img src="${particleType}" width="${size}" height="${size}" style="border-radius: 50%">`; } else { // Handle emoji or text characters const fontSizeMultiplier = 3; // Make emojis 3x bigger const emojiSize = size * fontSizeMultiplier; particle.innerHTML = `<div style="font-size: ${emojiSize}px; line-height: 1; text-align: center; width: ${size}px; height: ${size}px; display: flex; align-items: center; justify-content: center; transform: scale(${fontSizeMultiplier}); transform-origin: center;">${particleType}</div>`; }
particle.style.position = "absolute"; particle.style.transform = `translate3d(${left}px, ${top}px, 0px) rotate(${spinVal}deg)`;
container.appendChild(particle);
particles.push({ direction, element: particle, left, size, speedHorz, speedUp, spinSpeed, spinVal, top, }); }
function refreshParticles() { particles.forEach((p) => { p.left = p.left - p.speedHorz * p.direction; p.top = p.top - p.speedUp; p.speedUp = Math.min(p.size, p.speedUp - 1); p.spinVal = p.spinVal + p.spinSpeed;
if (p.top >= Math.max(window.innerHeight, document.body.clientHeight) + p.size) { particles = particles.filter((o) => o !== p); p.element.remove(); }
p.element.setAttribute( "style", [ "position:absolute", "will-change:transform", `top:${p.top}px`, `left:${p.left}px`, `transform:rotate(${p.spinVal}deg)`, ].join(";") ); }); }
let animationFrame: number | undefined;
let lastParticleTimestamp = 0; const particleGenerationDelay = 30;
function loop() { const currentTime = performance.now(); if ( autoAddParticle && particles.length < limit && currentTime - lastParticleTimestamp > particleGenerationDelay ) { generateParticle(); lastParticleTimestamp = currentTime; }
refreshParticles(); animationFrame = requestAnimationFrame(loop); }
loop();
const isTouchInteraction = "ontouchstart" in window;
const tap = isTouchInteraction ? "touchstart" : "mousedown"; const tapEnd = isTouchInteraction ? "touchend" : "mouseup"; const move = isTouchInteraction ? "touchmove" : "mousemove";
const updateMousePosition = (e: MouseEvent | TouchEvent) => { if ("touches" in e) { mouseX = e.touches?.[0].clientX; mouseY = e.touches?.[0].clientY; } else { mouseX = e.clientX; mouseY = e.clientY; } };
const tapHandler = (e: MouseEvent | TouchEvent) => { updateMousePosition(e); autoAddParticle = true; };
const disableAutoAddParticle = () => { autoAddParticle = false; };
element.addEventListener(move, updateMousePosition, { passive: true }); element.addEventListener(tap, tapHandler, { passive: true }); element.addEventListener(tapEnd, disableAutoAddParticle, { passive: true }); element.addEventListener("mouseleave", disableAutoAddParticle, { passive: true, });
return () => { element.removeEventListener(move, updateMousePosition); element.removeEventListener(tap, tapHandler); element.removeEventListener(tapEnd, disableAutoAddParticle); element.removeEventListener("mouseleave", disableAutoAddParticle);
const interval = setInterval(() => { if (animationFrame && particles.length === 0) { cancelAnimationFrame(animationFrame); clearInterval(interval);
if (--instanceCounter === 0) { container.remove(); } } }, 500); }; };
onMount(() => { if (ref) { cleanup = applyParticleEffect(ref, options); } });
onDestroy(() => { if (cleanup) { cleanup(); } });</script>
<span bind:this={ref}> {@render children()}</span> index.ts
export { default as CoolMode } from "./cool-mode.svelte";export type { CoolParticleOptions, BaseParticle, CoolParticle, BaseParticleOptions,} from "./cool-mode.svelte";Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── cool-mode/ ├── cool-mode.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/cool-mode/llms.txt
Follow the guide's installation and usage instructions.Match the project's existing structure, imports, and styles.Keep changes scoped to the component and verify it works.Reply with a short bullet list of completed changes.Usage
<script lang="ts"> import { CoolMode } from "#lib/components/magic/cool-mode/index.js";</script>
<CoolMode> <button type="button">Click me</button></CoolMode>Examples
Custom Image
cool-mode-custom-img.svelte
<script lang="ts"> import { CoolMode } from "$lib/components/magic/cool-mode/index.js"; import { Button } from "$lib/components/ui/button/index.js";</script>
<div class="relative flex justify-center"> <CoolMode options={{ particle: "https://raw.githubusercontent.com/sveltejs/branding/refs/heads/master/svelte-logo.png", }} > <Button>Svelte Image</Button> </CoolMode></div>Props
Cool Mode
A wrapper component that adds particle effects on interaction.
| Name | Type | Default | Description |
|---|---|---|---|
children | Snippet | required | The content to wrap with cool mode effect |
options | CoolParticleOptions | undefined | Configuration options for particles |
Cool Particle Options
Configuration options for the particle effect.
| Name | Type | Default | Description |
|---|---|---|---|
particle | string | "circle" | Particle type: "circle", emoji, or image URL |
particleCount | number | undefined | Maximum number of particles |
size | number | random | Size of particles in pixels |
speedHorz | number | random | Horizontal speed of particles |
speedUp | number | random | Vertical speed of particles |