Flickering Grid
magicui/flickering-gridFreeComponent
A flickering grid background made with SVGs, fully customizable using Tailwind CSS.
Live preview
live · rendered by the registry
Rendered by magicui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://magicui.design/r/flickering-grid.jsonSource
1"use client"23import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"45import { cn } from "@/lib/utils"67interface FlickeringGridProps extends React.HTMLAttributes<HTMLDivElement> {8 squareSize?: number9 gridGap?: number10 flickerChance?: number11 color?: string12 width?: number13 height?: number14 className?: string15 maxOpacity?: number16}1718export const FlickeringGrid: React.FC<FlickeringGridProps> = ({19 squareSize = 4,20 gridGap = 6,21 flickerChance = 0.3,22 color = "rgb(0, 0, 0)",23 width,24 height,25 className,26 maxOpacity = 0.3,27 ...props28}) => {29 const canvasRef = useRef<HTMLCanvasElement>(null)30 const containerRef = useRef<HTMLDivElement>(null)31 const [isInView, setIsInView] = useState(false)32 const [canvasSize, setCanvasSize] = useState({ width: 0, height: 0 })3334 const memoizedColor = useMemo(() => {35 const toRGBA = (color: string) => {36 if (typeof window === "undefined") {37 return `rgba(0, 0, 0,`38 }39 const canvas = document.createElement("canvas")40 canvas.width = canvas.height = 141 const ctx = canvas.getContext("2d")42 if (!ctx) return "rgba(255, 0, 0,"43 ctx.fillStyle = color44 ctx.fillRect(0, 0, 1, 1)45 const [r, g, b] = Array.from(ctx.getImageData(0, 0, 1, 1).data)46 return `rgba(${r}, ${g}, ${b},`47 }48 return toRGBA(color)49 }, [color])5051 const setupCanvas = useCallback(52 (canvas: HTMLCanvasElement, width: number, height: number) => {53 const dpr = window.devicePixelRatio || 154 canvas.width = width * dpr55 canvas.height = height * dpr56 canvas.style.width = `${width}px`57 canvas.style.height = `${height}px`58 const cols = Math.ceil(width / (squareSize + gridGap))59 const rows = Math.ceil(height / (squareSize + gridGap))6061 const squares = new Float32Array(cols * rows)62 for (let i = 0; i < squares.length; i++) {63 squares[i] = Math.random() * maxOpacity64 }6566 return { cols, rows, squares, dpr }67 },68 [squareSize, gridGap, maxOpacity]69 )7071 const updateSquares = useCallback(72 (squares: Float32Array, deltaTime: number) => {73 for (let i = 0; i < squares.length; i++) {74 if (Math.random() < flickerChance * deltaTime) {75 squares[i] = Math.random() * maxOpacity76 }77 }78 },79 [flickerChance, maxOpacity]80 )8182 const drawGrid = useCallback(83 (84 ctx: CanvasRenderingContext2D,85 width: number,86 height: number,87 cols: number,88// … truncated
Files it writes
More from magicui
All 247 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Androidandroid | magicui | Components | Free | no deps | |
| Animated Beamanimated-beam | magicui | Components | Free | 1 dep | |
| Animated Circular Progress Baranimated-circular-progress-bar | magicui | Components | Free | no deps | |
| Animated Gradient Textanimated-gradient-text | magicui | Components | Free | no deps | |
| Animated Grid Patternanimated-grid-pattern | magicui | Components | Free | 1 dep | |
| Animated Listanimated-list | magicui | Components | Free | 1 dep | |
| Animated Shiny Textanimated-shiny-text | magicui | Components | Free | no deps | |
| Theme Toggleranimated-theme-toggler | magicui | Components | Free | 1 dep |
