Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
An interactive 3D tag cloud component.
icon-cloud.svelte
<script lang="ts"> import { IconCloud } from "$lib/components/magic/icon-cloud/index.js";
const slugs = [ "typescript", "javascript", "dart", "java", "svelte", "flutter", "android", "html5", "css3", "nodedotjs", "express", "nextdotjs", "prisma", "amazonaws", "postgresql", "firebase", "nginx", "vercel", "testinglibrary", "jest", "cypress", "docker", "git", "jira", "github", "gitlab", "visualstudiocode", "androidstudio", "sonarqube", "figma", ];
let images = $state(slugs.map((slug) => `https://cdn.simpleicons.org/${slug}/${slug}`));</script>
<div class="relative flex size-full items-center justify-center overflow-hidden"> <IconCloud {images} /></div>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/icon-cloud.json
Copy the Source Code
Copy and paste the following code into your project:
icon-cloud.svelte
<script lang="ts"> import { onMount } from "svelte"; import type { Component } from "svelte";
interface Icon { x: number; y: number; z: number; scale: number; opacity: number; id: number; }
interface IconCloudProps { icons?: Component[]; images?: string[]; class?: string; }
let { icons, images, class: className = "" }: IconCloudProps = $props();
let canvasRef: HTMLCanvasElement | null = $state(null); let iconPositions = $state<Icon[]>([]); let isDragging = $state(false); let lastMousePos = $state({ x: 0, y: 0 }); let mousePos = $state({ x: 0, y: 0 }); let targetRotation = $state<{ x: number; y: number; startX: number; startY: number; distance: number; startTime: number; duration: number; } | null>(null);
let animationFrameId = 0; let rotationRef = { x: 0, y: 0 }; let iconCanvases: HTMLCanvasElement[] = []; let imagesLoaded: boolean[] = [];
function easeOutCubic(t: number): number { return 1 - Math.pow(1 - t, 3); }
// Create icon canvases $effect(() => { if (!icons && !images) return;
const items = icons || images || []; imagesLoaded = new Array(items.length).fill(false);
const newIconCanvases = items.map((_item, index) => { const offscreen = document.createElement("canvas"); offscreen.width = 40; offscreen.height = 40; const offCtx = offscreen.getContext("2d");
if (offCtx) { if (images) { const img = new Image(); img.crossOrigin = "anonymous"; img.src = items[index] as string; img.onload = () => { offCtx.clearRect(0, 0, offscreen.width, offscreen.height); offCtx.beginPath(); offCtx.arc(20, 20, 20, 0, Math.PI * 2); offCtx.closePath(); offCtx.clip(); offCtx.drawImage(img, 0, 0, 40, 40); imagesLoaded[index] = true; }; } } return offscreen; });
iconCanvases = newIconCanvases; });
// Generate initial icon positions on a sphere $effect(() => { const items = icons || images || []; const newIcons: Icon[] = []; const numIcons = items.length || 20;
const offset = 2 / numIcons; const increment = Math.PI * (3 - Math.sqrt(5));
for (let i = 0; i < numIcons; i++) { const y = i * offset - 1 + offset / 2; const r = Math.sqrt(1 - y * y); const phi = i * increment;
const x = Math.cos(phi) * r; const z = Math.sin(phi) * r;
newIcons.push({ x: x * 100, y: y * 100, z: z * 100, scale: 1, opacity: 1, id: i, }); } iconPositions = newIcons; });
function handleMouseDown(e: MouseEvent) { const rect = canvasRef?.getBoundingClientRect(); if (!rect || !canvasRef) return;
const x = e.clientX - rect.left; const y = e.clientY - rect.top;
const ctx = canvasRef.getContext("2d"); if (!ctx) return;
for (const icon of iconPositions) { const cosX = Math.cos(rotationRef.x); const sinX = Math.sin(rotationRef.x); const cosY = Math.cos(rotationRef.y); const sinY = Math.sin(rotationRef.y);
const rotatedX = icon.x * cosY - icon.z * sinY; const rotatedZ = icon.x * sinY + icon.z * cosY; const rotatedY = icon.y * cosX + rotatedZ * sinX;
const screenX = canvasRef.width / 2 + rotatedX; const screenY = canvasRef.height / 2 + rotatedY;
const scale = (rotatedZ + 200) / 300; const radius = 20 * scale; const dx = x - screenX; const dy = y - screenY;
if (dx * dx + dy * dy < radius * radius) { const targetX = -Math.atan2(icon.y, Math.sqrt(icon.x * icon.x + icon.z * icon.z)); const targetY = Math.atan2(icon.x, icon.z);
const currentX = rotationRef.x; const currentY = rotationRef.y; const distance = Math.sqrt( Math.pow(targetX - currentX, 2) + Math.pow(targetY - currentY, 2) );
const duration = Math.min(2000, Math.max(800, distance * 1000));
targetRotation = { x: targetX, y: targetY, startX: currentX, startY: currentY, distance, startTime: performance.now(), duration, }; return; } }
isDragging = true; lastMousePos = { x: e.clientX, y: e.clientY }; }
function handleMouseMove(e: MouseEvent) { const rect = canvasRef?.getBoundingClientRect(); if (rect) { const x = e.clientX - rect.left; const y = e.clientY - rect.top; mousePos = { x, y }; }
if (isDragging) { const deltaX = e.clientX - lastMousePos.x; const deltaY = e.clientY - lastMousePos.y;
rotationRef = { x: rotationRef.x + deltaY * 0.002, y: rotationRef.y + deltaX * 0.002, };
lastMousePos = { x: e.clientX, y: e.clientY }; } }
function handleMouseUp() { isDragging = false; }
// Animation and rendering onMount(() => { const canvas = canvasRef; const ctx = canvas?.getContext("2d"); if (!canvas || !ctx) return;
const animate = () => { ctx.clearRect(0, 0, canvas.width, canvas.height);
const centerX = canvas.width / 2; const centerY = canvas.height / 2; const maxDistance = Math.sqrt(centerX * centerX + centerY * centerY); const dx = mousePos.x - centerX; const dy = mousePos.y - centerY; const distance = Math.sqrt(dx * dx + dy * dy); const speed = 0.003 + (distance / maxDistance) * 0.01;
if (targetRotation) { const elapsed = performance.now() - targetRotation.startTime; const progress = Math.min(1, elapsed / targetRotation.duration); const easedProgress = easeOutCubic(progress);
rotationRef = { x: targetRotation.startX + (targetRotation.x - targetRotation.startX) * easedProgress, y: targetRotation.startY + (targetRotation.y - targetRotation.startY) * easedProgress, };
if (progress >= 1) { targetRotation = null; } } else if (!isDragging) { rotationRef = { x: rotationRef.x + (dy / canvas.height) * speed, y: rotationRef.y + (dx / canvas.width) * speed, }; }
iconPositions.forEach((icon, index) => { const cosX = Math.cos(rotationRef.x); const sinX = Math.sin(rotationRef.x); const cosY = Math.cos(rotationRef.y); const sinY = Math.sin(rotationRef.y);
const rotatedX = icon.x * cosY - icon.z * sinY; const rotatedZ = icon.x * sinY + icon.z * cosY; const rotatedY = icon.y * cosX + rotatedZ * sinX;
const scale = (rotatedZ + 200) / 300; const opacity = Math.max(0.2, Math.min(1, (rotatedZ + 150) / 200));
ctx.save(); ctx.translate(canvas.width / 2 + rotatedX, canvas.height / 2 + rotatedY); ctx.scale(scale, scale); ctx.globalAlpha = opacity;
if (icons || images) { if (iconCanvases[index] && imagesLoaded[index]) { ctx.drawImage(iconCanvases[index], -20, -20, 40, 40); } } else { ctx.beginPath(); ctx.arc(0, 0, 20, 0, Math.PI * 2); ctx.fillStyle = "#4444ff"; ctx.fill(); ctx.fillStyle = "white"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.font = "16px Arial"; ctx.fillText(`${icon.id + 1}`, 0, 0); }
ctx.restore(); });
animationFrameId = requestAnimationFrame(animate); };
animate();
return () => { if (animationFrameId) { cancelAnimationFrame(animationFrameId); } }; });</script>
<canvas bind:this={canvasRef} width={400} height={400} onmousedown={handleMouseDown} onmousemove={handleMouseMove} onmouseup={handleMouseUp} onmouseleave={handleMouseUp} class={`rounded-lg ${className}`} aria-label="Interactive 3D Icon Cloud"></canvas> index.ts
export { default as IconCloud } from "./icon-cloud.svelte";Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── icon-cloud/ ├── icon-cloud.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/icon-cloud/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 { IconCloud } from "#lib/components/magic/icon-cloud/index.js";
const images = ["svelte", "typescript", "vite"].map( (slug) => `https://cdn.simpleicons.org/${slug}/${slug}` );</script>
<IconCloud {images} />Examples
Images
Icon cloud using images from Simple Icons CDN.
images.svelte
<script lang="ts"> import { IconCloud } from "$lib/components/magic/icon-cloud/index.js";
let images = $state([ "https://images.unsplash.com/photo-1633356122544-f134324a6cee?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1614680376593-902f74cf0d41?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1618761714954-0b8cd0026356?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1587620962725-abab7fe55159?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1542831371-29b0f74f9713?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1498050108023-c5249f4df085?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1581089778245-3ce67677f718?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1605379399642-870262d3d051?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1593720219276-0b1eacd0aef4?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1526374965328-7f61d4dc18c5?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1517694712202-14dd9538aa97?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1550745165-9bc0b252726f?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1504639725590-34d0984388bd?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1580927752452-89d86da3fa0a?w=100&h=100&fit=crop", "https://images.unsplash.com/photo-1534972195531-d756b9bfa9f2?w=100&h=100&fit=crop", ]);</script>
<div class="bg-background relative flex size-full items-center justify-center overflow-hidden rounded-lg border px-20 pt-8 pb-20"> <IconCloud {images} /></div>Props
Icon Cloud
A component for Icon Cloud.
| Name | Type | Default | Description |
|---|---|---|---|
icons | Component[] | [] | |
images | string[] | [] | Array of image URLs to display in the cloud. |
class | string | "" | Additional CSS classes to apply |