Free · MIT

Film Grain

A two-colour gradient under moving film grain.

Canvas 2D with no packages. Use it behind a headline or a quote.

The code

The whole file is below. Install the packages it imports, add the file to your project and import it.

FilmGrain.tsx
"use client";

/**
 * Film Grain — a two-colour gradient under moving photographic grain.
 *
 * Self-contained: no dependencies. Canvas 2D. The grain is a small
 * noise tile redrawn every frame and scaled up, so it costs almost
 * nothing at any size. The editorial standby: behind a serif headline,
 * a quote, a chapter opener.
 *
 *   <FilmGrain from="#2a1a12" to="#c35a2c" grain={0.18} angle={160} />
 */
import { useEffect, useRef } from "react";

export type FilmGrainProps = {
  from?: string;
  to?: string;
  /** Grain opacity, 0–0.5. */
  grain?: number;
  /** Gradient angle in degrees. */
  angle?: number;
  className?: string;
};

export function FilmGrain({ from = "#2a1a12", to = "#c35a2c", grain = 0.18, angle = 160, className = "" }: FilmGrainProps) {
  const canvasRef = useRef<HTMLCanvasElement>(null);

  useEffect(() => {
    const canvas = canvasRef.current;
    if (!canvas) return;
    const context = canvas.getContext("2d");
    if (!context) return;

    const tile = document.createElement("canvas");
    tile.width = 192;
    tile.height = 192;
    const tileContext = tile.getContext("2d");
    if (!tileContext) return;
    const image = tileContext.createImageData(tile.width, tile.height);

    let width = 0;
    let height = 0;
    let raf = 0;
    let visible = true;

    function resize() {
      if (!canvas) return;
      const dpr = Math.min(window.devicePixelRatio || 1, 2);
      width = canvas.clientWidth;
      height = canvas.clientHeight;
      canvas.width = Math.round(width * dpr);
      canvas.height = Math.round(height * dpr);
      context?.setTransform(dpr, 0, 0, dpr, 0, 0);
    }

    function draw() {
      if (!context || !tileContext) return;
      // Gradient.
      const rad = (angle * Math.PI) / 180;
      const dx = Math.cos(rad) * width;
      const dy = Math.sin(rad) * height;
      const gradient = context.createLinearGradient(width / 2 - dx / 2, height / 2 - dy / 2, width / 2 + dx / 2, height / 2 + dy / 2);
      gradient.addColorStop(0, from);
      gradient.addColorStop(1, to);
      context.globalAlpha = 1;
      context.globalCompositeOperation = "source-over";
      context.fillStyle = gradient;
      context.fillRect(0, 0, width, height);

      // Grain: fresh noise every frame.
      const data = image.data;
      for (let i = 0; i < data.length; i += 4) {
        const v = (Math.random() * 255) | 0;
        data[i] = v;
        data[i + 1] = v;
        data[i + 2] = v;
        data[i + 3] = 255;
      }
      tileContext.putImageData(image, 0, 0);
      const pattern = context.createPattern(tile, "repeat");
      if (pattern) {
        context.globalCompositeOperation = "overlay";
        context.globalAlpha = grain;
        context.fillStyle = pattern;
        context.fillRect(0, 0, width, height);
      }

      if (visible) raf = requestAnimationFrame(draw);
    }

    const observer = new IntersectionObserver(([entry]) => {
      visible = entry.isIntersecting;
      if (visible) {
        cancelAnimationFrame(raf);
        raf = requestAnimationFrame(draw);
      }
    });

    resize();
    observer.observe(canvas);
    window.addEventListener("resize", resize);
    raf = requestAnimationFrame(draw);

    return () => {
      cancelAnimationFrame(raf);
      observer.disconnect();
      window.removeEventListener("resize", resize);
    };
  }, [from, to, grain, angle]);

  return <canvas ref={canvasRef} className={className} style={{ display: "block", width: "100%", height: "100%", minHeight: 240 }} />;
}

export default FilmGrain;

Inside

  • Canvas 2D, no dependencies
  • New grain every frame from a small noise tile
  • Props for both colours, grain amount and angle
  • Pauses when off screen

Install

No packages to install.

One file.

Kind
Backgrounds
Licence
MIT
Runs on
React 19
Price
Free

More elements

3D objects
Pro
Chrome Blob: open the element page

Chrome Blob

A metallic sphere that distorts as it turns, with studio reflections.

Price
USD 10, all access
Built with
React Three Fiber

FAQ

What you get, what it runs on, what it costs to run and what the licence allows.

01

What do I get?

One React component file per element, plus any shader or asset files it needs. Members of a series also get the small engine file they share. Free elements are copied from the page. Pro elements are a download after one payment of USD 10, which unlocks all of them. Each element imports only public packages, which are listed on its page.

02

What does it run on?

React 19. The 3D and shader elements use three.js through React Three Fiber. The 2D ones use Framer Motion or plain CSS and canvas. They work in Next.js, Vite, Remix and any other React setup. Framer and Webflow users can embed an element as a code component.

03

Can I change the colours and speed?

Yes. Every element exposes its look as props, the same controls shown on its page. Anything beyond the props is yours to edit in the file.

04

Can I use these in a deck, a magazine or a film?

Yes. Every element page has a Save frame button that downloads the current frame as a PNG at the size shown. Make the browser window large for a print-size still. For motion, record the page or ask us for a rendered clip. The licence covers web, print, decks and video, for your own work and your clients' work.

05

How heavy is it?

The 3D elements mount when they scroll into view and unmount when they leave, so an element off screen costs nothing. The shader backgrounds cost a fraction of a video background. The 2D elements are a few kilobytes.

06

What is the licence?

Free elements are MIT. Use them anywhere, including client work. Pro elements are licensed to you for unlimited projects of your own and your clients'. You may not resell or redistribute the source itself.

07

Can you make one for us?

Yes. A custom element, such as your product in 3D or a branded shader, is a small project quoted up front. All-access members are asked first about what we build next.