webdevpuneet.com

Flickering Grid — Free HTML CSS JS Canvas Texture Snippet

Flickering Grid · Animations · Plain HTML, CSS & JS · Live preview

CategoryAnimations
TagsCanvas

Copy & paste

Source Code

<section class="fg-hero">
  <canvas class="fg-canvas" id="fgCanvas" aria-hidden="true"></canvas>
  <div class="fg-fade" aria-hidden="true"></div>
  <div class="fg-content">
    <h1>Signal in the noise</h1>
    <p>A grid of squares flickering at random — a calm, living texture on canvas.</p>
  </div>
</section>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Typed-array grid
Cell brightness in a fast Float32Array.
Random flicker
A fraction of cells re-randomize each frame.
Subtle by design
Capped opacity keeps it a backdrop.
Cheap rect fills
Solid squares with a skip threshold.
HiDPI-sharp
devicePixelRatio scaling for crisp edges.
Rebuild on resize
Cell count re-fits the viewport.
Vignette frame
A radial fade emerges the grid from dark.
One canvas
Single element for any cell count.

About this UI Snippet

Flickering Grid — Randomly Flickering Squares on Canvas

Screenshot of the Flickering Grid snippet rendered live

The flickering grid is the subtle, techy backdrop where a lattice of small squares flickers to random opacities — never the same twice, always quietly alive. It is a favorite ambient texture for AI, data, and developer sites. This snippet renders it on an HTML <canvas> with plain vanilla JavaScript, sharp on retina and efficient even with hundreds of cells.

A grid stored as a typed array

The grid is computed from cell SIZE and GAP: the canvas is divided into as many columns and rows as fit, and each cell's current brightness is stored in a Float32Array indexed by row * cols + col. A typed array is used deliberately — it holds the brightness of every cell in a compact, fast-to-iterate buffer, which matters because the draw loop touches every cell each frame. The array is seeded with random starting opacities so the grid is varied from the first frame.

The flicker logic

Each frame, a small fraction of cells (governed by FLICKER) are chosen at random to jump to a new random brightness, while the rest hold their current value. Redrawing every cell at its stored opacity then produces the flicker: most cells stay put on any given frame while a scattered few change, so the texture shimmers gently rather than strobing. Capping brightness at MAXA (0.55) keeps it subtle — a backdrop, not a distraction.

Drawing efficiently

The loop clears the canvas and fills each cell as a small fillRect at its opacity, skipping cells dimmer than a threshold so near-invisible squares cost nothing. Drawing solid rects (rather than per-cell gradients or shadows) keeps each frame cheap, which is what lets a few hundred cells animate at 60fps. The whole grid is one canvas, so it is a single element regardless of cell count.

Crisp on retina

Like any canvas, it scales with devicePixelRatio (capped at 2): the backing buffer is sized up and a setTransform lets drawing happen in CSS pixels while rendering at device resolution, so the square edges stay sharp on HiDPI screens. The grid rebuilds on resize to fit the new dimensions and recomputes the cell count.

Framing the content

A radial .fg-fade vignette darkens the grid toward the edges so the flicker emerges from darkness and the lattice does not end abruptly, keeping focus on the centered headline. The content sits above the canvas at a higher z-index with a soft text-shadow for legibility.

Customizing it

Change SIZE and GAP for a finer or chunkier grid, FLICKER for a busier or calmer shimmer, MAXA for brighter or subtler squares, and COLOR for any hue. Lower the DPR cap or raise the skip threshold for very low-end targets. Pair it with a dot pattern or particle network for a layered technical hero, or an aurora text headline on top.

Step by step

How to Use

  1. 1
    Paste HTML, CSS, and JSA grid of squares flickers softly behind the headline.
  2. 2
    Watch the shimmerScattered cells change brightness each moment.
  3. 3
    Note the vignetteThe grid fades into the page toward the edges.
  4. 4
    Resize the windowThe grid rebuilds and stays crisp on retina.
  5. 5
    Tune the flickerAdjust FLICKER, MAXA, SIZE, and GAP.
  6. 6
    Recolor itChange the COLOR RGB string.

Real-world uses

Common Use Cases

AI and data heroes
Add a living texture behind an aurora text headline, with a fraction of cells re-randomising each frame at capped opacity.
Developer site backdrops
Pair with a dot pattern section, using a Float32Array to keep brightness updates fast across thousands of cells.
Dashboard backgrounds
Give a status dashboard a quiet, living backdrop, with an edge vignette keeping attention on the content.
Launch page textures
Combine with a particle network for a layered tech aesthetic, while cheap solid rectangle fills keep frame cost low.
Cyberpunk and terminal themes
Offer a calmer cousin of matrix rain, where nothing falls but the grid still feels alive.
Related: Loot Box Reveal Animation
See the Loot Box Reveal Animation for a related animations pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

A Float32Array holds the brightness of every cell in a compact, fast-to-iterate buffer indexed by row times columns plus column. The draw loop touches every cell each frame, so a typed array is faster and lighter than an array of objects, which keeps hundreds of cells animating smoothly.

Each frame only a small fraction of cells, set by FLICKER, jump to a new random brightness while the rest hold their value, and brightness is capped at MAXA around 0.55. So most cells stay put on any frame and only a scattered few change, producing a gentle shimmer rather than a harsh strobe.

The loop clears the canvas and fills each cell as a small solid fillRect at its opacity, skipping cells dimmer than a threshold so near-invisible squares cost nothing. Using solid rects instead of per-cell gradients or shadows keeps each frame cheap, which lets a few hundred cells run at 60fps on one canvas.

Yes. It scales with devicePixelRatio capped at 2: the canvas backing buffer is sized up and a setTransform lets drawing happen in CSS pixels while rendering at device resolution, so the square edges stay crisp. The grid also rebuilds on resize, recomputing the cell count for the new dimensions.

Put the canvas behind your content with a ref and run build plus the rAF draw loop in a mount effect, cancelling the frame and removing the resize listener on unmount. Keep the cells typed array and dimensions in refs, not state. The component is framework-agnostic; in Tailwind position the canvas absolute inset-0 behind a relative content layer with a vignette overlay.