Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
A customizable Flickering Grid background component for Svelte 5, rendered efficiently with Canvas and styled using Tailwind CSS.
flickering-grid.svelte
<script lang="ts"> import FlickeringGrid from "$lib/components/magic/flickering-grid/flickering-grid.svelte";</script>
<div class="bg-background relative h-125 w-full overflow-hidden rounded-lg border md:max-w-xl"> <FlickeringGrid class="absolute inset-0 z-0 size-full" squareSize={4} gridGap={6} color="#6B7280" maxOpacity={0.5} flickerChance={0.1} height={800} width={800} /></div>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/flickering-grid.json
Copy the Source Code
Copy and paste the following code into your project:
flickering-grid.svelte
<script lang="ts"> import type { ClassValue } from "svelte/elements"; import { onMount } from "svelte"; import { cn } from "#lib/utils.js";
interface FlickeringGridProps { squareSize?: number; gridGap?: number; flickerChance?: number; color?: string; width?: number; height?: number; class?: ClassValue; maxOpacity?: number; }
let { squareSize = 4, gridGap = 6, flickerChance = 0.3, color = "rgb(0, 0, 0)", width, height, class: className, maxOpacity = 0.3, ...props }: FlickeringGridProps = $props();
let canvasRef: HTMLCanvasElement | null = $state(null); let containerRef: HTMLDivElement | null = $state(null); let isInView = $state(false); let canvasSize = $state({ width: 0, height: 0 });
const memoizedColor = $derived.by(() => { const toRGBA = (color: string) => { if (typeof window === "undefined") { return `rgba(0, 0, 0,`; } const canvas = document.createElement("canvas"); canvas.width = canvas.height = 1; const ctx = canvas.getContext("2d"); if (!ctx) return "rgba(255, 0, 0,"; ctx.fillStyle = color; ctx.fillRect(0, 0, 1, 1); const [r, g, b] = Array.from(ctx.getImageData(0, 0, 1, 1).data); return `rgba(${r}, ${g}, ${b},`; }; return toRGBA(color); });
const setupCanvas = (canvas: HTMLCanvasElement, width: number, height: number) => { const dpr = window.devicePixelRatio || 1; canvas.width = width * dpr; canvas.height = height * dpr; canvas.style.width = `${width}px`; canvas.style.height = `${height}px`; const cols = Math.floor(width / (squareSize + gridGap)); const rows = Math.floor(height / (squareSize + gridGap));
const squares = new Float32Array(cols * rows); for (let i = 0; i < squares.length; i++) { squares[i] = Math.random() * maxOpacity; }
return { cols, rows, squares, dpr }; };
const updateSquares = (squares: Float32Array, deltaTime: number) => { for (let i = 0; i < squares.length; i++) { if (Math.random() < flickerChance * deltaTime) { squares[i] = Math.random() * maxOpacity; } } };
const drawGrid = ( ctx: CanvasRenderingContext2D, width: number, height: number, cols: number, rows: number, squares: Float32Array, dpr: number ) => { ctx.clearRect(0, 0, width, height); ctx.fillStyle = "transparent"; ctx.fillRect(0, 0, width, height);
for (let i = 0; i < cols; i++) { for (let j = 0; j < rows; j++) { const opacity = squares[i * rows + j]; ctx.fillStyle = `${memoizedColor}${opacity})`; ctx.fillRect( i * (squareSize + gridGap) * dpr, j * (squareSize + gridGap) * dpr, squareSize * dpr, squareSize * dpr ); } } };
onMount(() => { const canvas = canvasRef; const container = containerRef; if (!canvas || !container) return;
const ctx = canvas.getContext("2d"); if (!ctx) return;
let animationFrameId: number; let gridParams: ReturnType<typeof setupCanvas>;
const updateCanvasSize = () => { const newWidth = width || container.clientWidth; const newHeight = height || container.clientHeight; canvasSize = { width: newWidth, height: newHeight }; gridParams = setupCanvas(canvas, newWidth, newHeight); };
updateCanvasSize();
let lastTime = 0; const animate = (time: number) => { if (!isInView) return;
const deltaTime = (time - lastTime) / 1000; lastTime = time;
updateSquares(gridParams.squares, deltaTime); drawGrid( ctx, canvas.width, canvas.height, gridParams.cols, gridParams.rows, gridParams.squares, gridParams.dpr ); animationFrameId = requestAnimationFrame(animate); };
const resizeObserver = new ResizeObserver(() => { updateCanvasSize(); });
resizeObserver.observe(container);
const intersectionObserver = new IntersectionObserver( ([entry]) => { isInView = entry.isIntersecting; }, { threshold: 0 } );
intersectionObserver.observe(canvas);
$effect(() => { if (isInView) { animationFrameId = requestAnimationFrame(animate); } });
return () => { cancelAnimationFrame(animationFrameId); resizeObserver.disconnect(); intersectionObserver.disconnect(); }; });</script>
<div bind:this={containerRef} class={cn("h-full w-full", className)} {...props}> <canvas bind:this={canvasRef} class="pointer-events-none" style:width="{canvasSize.width}px" style:height="{canvasSize.height}px" ></canvas></div> index.ts
import FlickeringGrid from "./flickering-grid.svelte";export { FlickeringGrid };Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── flickering-grid/ ├── flickering-grid.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/flickering-grid/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 { FlickeringGrid } from "#lib/components/magic/flickering-grid/index.js";</script>
<div class="relative h-64 overflow-hidden"> <FlickeringGrid class="absolute inset-0 size-full" /></div>Examples
Basic Usage
A simple flickering grid background.
flickering-grid-basic.svelte
<script lang="ts"> import { FlickeringGrid } from "$lib/components/magic/flickering-grid/index.js";</script>
<div class="bg-background relative size-150 w-full overflow-hidden rounded-lg border"> <FlickeringGrid class="relative inset-0 z-0 mask-[radial-gradient(450px_circle_at_center,white,transparent)]" squareSize={4} gridGap={6} color="#60A5FA" maxOpacity={0.5} flickerChance={0.1} height={800} width={800} /></div>Header Backdrop
A sparse emerald grid faded away from the heading to keep the text readable.
Developer reference
API documentation
flickering-grid-header.svelte
<script lang="ts"> import { FlickeringGrid } from "$lib/components/magic/flickering-grid/index.js";</script>
<header class="bg-background relative isolate w-full max-w-xl overflow-hidden rounded-lg border px-6 py-16 sm:px-10"> <div class="pointer-events-none absolute inset-0 -z-10" aria-hidden="true"> <FlickeringGrid class="mask-[linear-gradient(to_left,black,transparent)]" color="#10b981" squareSize={3} gridGap={8} maxOpacity={0.35} flickerChance={0.08} /> </div> <p class="text-muted-foreground mb-3 text-sm">Developer reference</p> <h2 class="text-3xl font-semibold">API documentation</h2></header>Inset Panel
A denser amber grid confined to a panel header, leaving the content area still.
Design resources
Tokens, type scales, and component guidelines.
flickering-grid-panel.svelte
<script lang="ts"> import { FlickeringGrid } from "$lib/components/magic/flickering-grid/index.js";</script>
<section class="bg-background w-full max-w-sm overflow-hidden rounded-lg border"> <div class="relative h-32 border-b bg-amber-50 dark:bg-amber-950/20" aria-hidden="true"> <FlickeringGrid class="absolute inset-0" color="#d97706" squareSize={6} gridGap={4} maxOpacity={0.3} flickerChance={0.15} /> </div> <div class="p-6"> <h3 class="font-medium">Design resources</h3> <p class="text-muted-foreground mt-2 text-sm"> Tokens, type scales, and component guidelines. </p> </div></section>Props
Flickering Grid
A component for creating a flickering grid background.
| Name | Type | Default | Description |
|---|---|---|---|
class | ClassValue | "" | Additional CSS classes to apply |
squareSize | number | 4 | The size of each square in the grid |
gridGap | number | 6 | The gap between squares in the grid |
flickerChance | number | 0.3 | The chance of each square flickering |
color | string | rgb(0, 0, 0) | The color of the squares |
width | number | | The width of the grid |
height | number | | The height of the grid |
maxOpacity | number | 0.3 | The maximum opacity of the squares |