# Monochrome Smoke

> Soft smoke drifting slowly across the background in a WebGL shader, parting around the cursor. Calm enough to sit behind text.

Category: Backgrounds · Free · Updated 2026-11-14 · Live demo: https://www.motionkitui.com/components/monochrome-smoke

## When to use it

Editorial and fashion sites, agencies, luxury brands, film and photography portfolios, and calm hero sections.

## How it works

A WebGL shader layers noise and folds it back on itself (domain warping), drifting it slowly sideways so the wisps curl and change shape. The pointer bends the lookup away from itself and thins the smoke nearby, so it seems to part. It renders at half resolution because smoke is soft, adds a touch of film grain, and pauses off screen.

## Installation

```bash
npx shadcn@latest add https://www.motionkitui.com/r/monochrome-smoke.json
```

Or copy `components/motionkit/monochrome-smoke.tsx` below. Requires react and Tailwind CSS v4. Free for personal projects; commercial use needs MotionKit Plus (https://www.motionkitui.com/pricing).

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `color` | `string` | `"#C9CDD1"` | Smoke colour as a hex value. |
| `density` | `number` | `0.7` | How thick the smoke is, 0 to 1. |
| `speed` | `number` | `1` | Drift speed. 0 holds it still. |
| `scale` | `number` | `1.8` | Zoom: lower gives larger, softer wisps. |
| `interactive` | `boolean` | `true` | Let the smoke part around the pointer. |
| `children` | `ReactNode` |  | Your content, layered above. |

## Accessibility

- **Keyboard: Not applicable.** Decorative, nothing to focus. Your content inside stays fully interactive.
- **Screen readers: Pass.** The canvas is hidden from assistive tech; only your content is read.
- **Reduced motion: Pass.** With reduced motion on, it renders one still frame. It also stops drawing while off screen.
- **Colour contrast: Partial.** At the default density the smoke stays dim, but light text over its thickest parts can drop below 4.5:1; lower density, or give text a backdrop.

## Source

```tsx
"use client";

import { useEffect, useRef, useState, type ReactNode } from "react";

type MonochromeSmokeProps = {
  children?: ReactNode;
  /** Colour of the smoke. Greys read as editorial; a pale tint works too. */
  color?: string;
  /** How thick the smoke is, 0 to 1. */
  density?: number;
  /** Drift speed. 0 holds it still. */
  speed?: number;
  /** Zoom: lower values give larger, softer wisps. */
  scale?: number;
  /** Let the smoke part around the pointer. */
  interactive?: boolean;
  className?: string;
};

const vertex = `attribute vec2 p; void main() { gl_Position = vec4(p, 0.0, 1.0); }`;

// layered noise drifting sideways and folded back on itself (domain warping) gives slow, curling wisps; the pointer
// pushes the smoke aside by bending the lookup away from it and thinning it nearby
const fragment = `
precision highp float;
uniform vec2 res; uniform float time; uniform vec3 pointer; uniform vec3 smoke; uniform vec3 bg; uniform float density; uniform float scale;
float hash(vec2 p) { p = fract(p * vec2(123.34, 456.21)); p += dot(p, p + 45.32); return fract(p.x * p.y); }
float noise(vec2 p) {
  vec2 i = floor(p); vec2 f = fract(p); vec2 u = f * f * (3.0 - 2.0 * f);
  return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), u.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), u.x), u.y);
}
float fbm(vec2 p) {
  float v = 0.0; float a = 0.5; mat2 turn = mat2(0.8, 0.6, -0.6, 0.8);
  for (int i = 0; i < 5; i++) { v += a * noise(p); p = turn * p * 2.01; a *= 0.5; }
  return v;
}
void main() {
  vec2 uv = (gl_FragCoord.xy - 0.5 * res) / min(res.x, res.y);
  vec2 d = uv - pointer.xy;
  float near = pointer.z * exp(-dot(d, d) * 10.0);
  vec2 p = uv * scale + normalize(d + 0.0001) * near * 0.5;
  float t = time * 0.05;
  vec2 q = vec2(fbm(p + vec2(t * 1.6, 0.0)), fbm(p + vec2(4.7, 2.3) + vec2(t * 1.2, -t * 0.4)));
  float f = fbm(p + 2.2 * q + vec2(t, 0.0));
  float amount = smoothstep(0.35, 0.95, f) * density * (1.0 - near * 0.8);
  amount *= 1.0 - smoothstep(0.55, 1.35, length(uv * vec2(0.7, 1.0))) * 0.6;
  vec3 col = mix(bg, smoke, amount);
  col += (hash(gl_FragCoord.xy + fract(time)) - 0.5) * 0.025;
  gl_FragColor = vec4(col, 1.0);
}`;

const toVec = (hex: string) => {
  const value = parseInt(hex.replace("#", "").padEnd(6, "0").slice(0, 6), 16);
  return [((value >> 16) & 255) / 255, ((value >> 8) & 255) / 255, (value & 255) / 255];
};

/*
 * WebGL starts on the visitor's first move, touch, scroll or key press rather than on load, with the still fallback
 * until then. Without a GPU, browsers draw WebGL on the CPU and creating the first context can freeze the page, so
 * where WebGPU reports only a software adapter the shader never starts. Waiting also keeps set-up off the first load.
 */
type GpuProbe = { requestAdapter: () => Promise<{ isFallbackAdapter?: boolean; info?: { isFallbackAdapter?: boolean } } | null> };
let gpuReady: Promise<boolean> | undefined;
function whenGpuReady() {
  return (gpuReady ??= (async () => {
    await new Promise<void>((resolve) => {
      const events = ["pointermove", "pointerdown", "keydown", "wheel", "touchstart", "scroll"] as const;
      const start = () => {
        events.forEach((name) => window.removeEventListener(name, start));
        resolve();
      };
      events.forEach((name) => window.addEventListener(name, start, { passive: true }));
    });
    const gpu = (navigator as Navigator & { gpu?: GpuProbe }).gpu;
    const adapter = gpu ? await gpu.requestAdapter().catch(() => null) : null;
    return !(adapter?.isFallbackAdapter || adapter?.info?.isFallbackAdapter);
  })());
}

/**
 * Soft smoke drifting slowly across the background, rendered in a WebGL shader with a touch of film grain, and
 * parting around the pointer as it moves. Calm enough to sit behind text. Falls back to a still haze.
 */
export function MonochromeSmoke({ children, color = "#C9CDD1", density = 0.7, speed = 1, scale = 1.8, interactive = true, className = "" }: MonochromeSmokeProps) {
  const canvasRef = useRef<HTMLCanvasElement>(null);
  const [generation, setGeneration] = useState(0);
  const [gpu, setGpu] = useState(false);
  useEffect(() => {
    let live = true;
    whenGpuReady().then((ready) => {
      if (live && ready) setGpu(true);
    });
    return () => {
      live = false;
    };
  }, []);

  // hand the context back when this canvas is done with: browsers allow only about 16 at once
  useEffect(() => {
    const canvas = canvasRef.current;
    return () => {
      if (canvas?.dataset.gl) canvas.getContext("webgl")?.getExtension("WEBGL_lose_context")?.loseContext();
    };
  }, [generation]);

  useEffect(() => {
    const canvas = canvasRef.current;
    const area = canvas?.parentElement;
    if (!gpu) return;
    const gl = canvas?.getContext("webgl", { antialias: false, premultipliedAlpha: false });
    if (!canvas || !area || !gl) return;
    canvas.dataset.gl = "1";
    const renderer = gl.getExtension("WEBGL_debug_renderer_info");
    if (renderer && /swiftshader|llvmpipe|softpipe|software/i.test(String(gl.getParameter(renderer.UNMASKED_RENDERER_WEBGL)))) return;
    if (gl.isContextLost()) {
      const retry = requestAnimationFrame(() => setGeneration((count) => (count < 8 ? count + 1 : count)));
      return () => cancelAnimationFrame(retry);
    }
    const onLost = (event: Event) => {
      event.preventDefault();
      setGeneration((count) => (count < 8 ? count + 1 : count));
    };
    canvas.addEventListener("webglcontextlost", onLost);
    const still = speed === 0 || window.matchMedia("(prefers-reduced-motion: reduce)").matches;

    const compile = (type: number, source: string) => {
      const shader = gl.createShader(type)!;
      gl.shaderSource(shader, source);
      gl.compileShader(shader);
      return shader;
    };
    const program = gl.createProgram()!;
    const shaders = [compile(gl.VERTEX_SHADER, vertex), compile(gl.FRAGMENT_SHADER, fragment)];
    shaders.forEach((shader) => gl.attachShader(program, shader));
    gl.linkProgram(program);
    if (!gl.getProgramParameter(program, gl.LINK_STATUS)) return;
    gl.useProgram(program);
    const buffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
    const position = gl.getAttribLocation(program, "p");
    gl.enableVertexAttribArray(position);
    gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0);
    const uniform = (name: string) => gl.getUniformLocation(program, name);
    const u = { res: uniform("res"), time: uniform("time"), pointer: uniform("pointer") };
    gl.uniform3fv(uniform("smoke"), toVec(color));
    gl.uniform3fv(uniform("bg"), toVec("#0C1010"));
    gl.uniform1f(uniform("density"), density);
    gl.uniform1f(uniform("scale"), scale);

    let frame = 0;
    let visible = true;
    let elapsed = 0;
    let last = 0;
    const pointer = { x: 0, y: 0, strength: 0, target: 0 };

    function resize() {
      const rect = canvas!.getBoundingClientRect();
      // smoke is soft, so it renders at half resolution and stays light on the GPU
      const ratio = Math.min(window.devicePixelRatio || 1, 2) * 0.5;
      canvas!.width = Math.max(1, Math.round(rect.width * ratio));
      canvas!.height = Math.max(1, Math.round(rect.height * ratio));
      gl!.viewport(0, 0, canvas!.width, canvas!.height);
      gl!.uniform2f(u.res, canvas!.width, canvas!.height);
    }

    function draw(time: number) {
      elapsed += last ? Math.min(0.05, (time - last) / 1000) * speed : 0;
      last = time;
      pointer.strength += (pointer.target - pointer.strength) * 0.04;
      gl!.uniform1f(u.time, elapsed + 20);
      gl!.uniform3f(u.pointer, pointer.x, pointer.y, pointer.strength);
      gl!.drawArrays(gl!.TRIANGLES, 0, 3);
    }

    function loop(time: number) {
      draw(time);
      frame = visible ? requestAnimationFrame(loop) : 0;
      if (!frame) last = 0;
    }

    resize();
    draw(0);
    if (!still) frame = requestAnimationFrame(loop);
    const resizeObserver = new ResizeObserver(() => {
      resize();
      draw(performance.now());
    });
    resizeObserver.observe(canvas);
    const visibilityObserver = new IntersectionObserver(([entry]) => {
      visible = entry.isIntersecting;
      if (visible && !frame && !still) frame = requestAnimationFrame(loop);
    });
    visibilityObserver.observe(canvas);

    function move(event: PointerEvent) {
      if (!interactive || still || event.pointerType === "touch") return;
      const rect = canvas!.getBoundingClientRect();
      const size = Math.min(rect.width, rect.height);
      pointer.x = (event.clientX - rect.left - rect.width / 2) / size;
      pointer.y = -(event.clientY - rect.top - rect.height / 2) / size;
      pointer.target = 1;
    }
    const leave = () => (pointer.target = 0);
    area.addEventListener("pointermove", move);
    area.addEventListener("pointerleave", leave);

    return () => {
      cancelAnimationFrame(frame);
      resizeObserver.disconnect();
      visibilityObserver.disconnect();
      area.removeEventListener("pointermove", move);
      area.removeEventListener("pointerleave", leave);
      gl.deleteBuffer(buffer);
      shaders.forEach((shader) => gl.deleteShader(shader));
      gl.deleteProgram(program);
      canvas.removeEventListener("webglcontextlost", onLost);
    };
  }, [color, density, speed, scale, interactive, generation, gpu]);

  return (
    <div
      className={`relative isolate overflow-hidden bg-[#0C1010] ${className}`}
      // the still fallback: a soft haze in the smoke colour
      style={{ backgroundImage: `radial-gradient(ellipse at 30% 40%, color-mix(in oklab, ${color} 22%, transparent), transparent 60%), radial-gradient(ellipse at 75% 65%, color-mix(in oklab, ${color} 14%, transparent), transparent 55%)` }}
    >
      <canvas key={generation} ref={canvasRef} aria-hidden className="absolute inset-0 -z-10 size-full" />
      {children}
    </div>
  );
}
```
