Flickering Grid
uifoundry/flickering-gridUnverifiedComponent
Animated flickering grid background component with canvas rendering
Install it
npx shadcn@latest add https://raw.githubusercontent.com/UIFoundry/uifoundry/master/flickering-grid.jsonSource
1"use client";23import { useEffect, useRef } from "react";4import { cn } from "@/registry/default/utils";56interface FlickeringGridProps {7 className?: string;8 squareSize?: number;9 gridGap?: number;10 color?: string;11 maxOpacity?: number;12 flickerChance?: number;13 width?: number;14 height?: number;15}1617export default function FlickeringGrid({18 className,19 squareSize = 4,20 gridGap = 6,21 color = "#60A5FA",22 maxOpacity = 0.5,23 flickerChance = 0.1,24 width = 800,25 height = 800,26}: FlickeringGridProps) {27 const canvasRef = useRef<HTMLCanvasElement>(null);2829 useEffect(() => {30 const canvas = canvasRef.current;31 if (!canvas) return;3233 const ctx = canvas.getContext("2d");34 if (!ctx) return;3536 // Set canvas size37 canvas.width = width;38 canvas.height = height;3940 // Calculate grid dimensions41 const cellSize = squareSize + gridGap;42 const cols = Math.ceil(width / cellSize);43 const rows = Math.ceil(height / cellSize);4445 // Create grid state46 const grid: number[][] = [];47 for (let i = 0; i < rows; i++) {48 grid[i] = [];49 for (let j = 0; j < cols; j++) {50 const row = grid[i];51 if (row) {52 row[j] = Math.random() * maxOpacity;53 }54 }55 }5657 // Animation loop with frame throttling58 let animationFrameId: number;59 let lastFrameTime = 0;60 const frameDelay = 1000 / 5; // 20fps for slower animation6162 const animate = (currentTime: number) => {63 animationFrameId = requestAnimationFrame(animate);6465 // Throttle to ~20fps for slower, more subtle animation66 const deltaTime = currentTime - lastFrameTime;67 if (deltaTime < frameDelay) return;68 lastFrameTime = currentTime;6970 ctx.clearRect(0, 0, width, height);7172 // Draw grid73 for (let i = 0; i < rows; i++) {74 for (let j = 0; j < cols; j++) {75 const row = grid[i];76 if (!row) continue;7778 const cell = row[j];79 if (cell === undefined) continue;8081 // Randomly flicker cells (less frequently)82 if (Math.random() < flickerChance) {83 row[j] = Math.random() * maxOpacity;84 } else {85 // Gradually fade (slower fade)86 row[j] = cell * 0.95;87 }8889 // Draw cell if visible90 const currentCell = row[j];91 if (currentCell !== undefined && currentCell > 0.01) {92 ctx.fillStyle = color;93 ctx.globalAlpha = currentCell;94 ctx.fillRect(j * cellSize, i * cellSize, squareSize, squareSize);95 }96 }97 }9899 ctx.globalAlpha = 1;100 };101102 animationFrameId = requestAnimationFrame(animate);103104 return () => {105// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from uifoundry
All 54 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Animated Groupanimated-group | uifoundry | Components | Unverified | 2 deps | |
| Buttonbutton | uifoundry | Components | Unverified | 3 deps | |
| Call To Action Pair Fieldcall-to-action-pair-field | uifoundry | Components | Unverified | 3 deps | |
| Cardcard | uifoundry | Components | Unverified | 1 dep | |
| Description Fielddescription-field | uifoundry | Components | Unverified | 2 deps | |
| Header Fieldheader-field | uifoundry | Components | Unverified | 2 deps | |
| Icon Componenticon | uifoundry | Components | Unverified | 2 deps | |
| Render Blocksrenderblocks | uifoundry | Components | Unverified | 2 deps |
