This is the dark-mode landing background as a self-contained Canvas 2D component. Glyphs do not move with forces. Each frame samples a domain-warped FBM field and maps the value onto an ASCII ramp. Inspired by the ambient ASCII look of Portrait Reveal, implemented here without WebGL.
"use client";
import { useEffect, useRef } from "react";
const ASCII_RAMP = [" ", ".", ":", "-", "=", "+", "*", "%", "@"] as const;
function fract(n: number) {
return n - Math.floor(n);
}
function hash(x: number, y: number) {
let px = fract(x * 123.34);
let py = fract(y * 456.21);
const d = px * (px + 45.32) + py * (py + 45.32);
px += d;
py += d;
return fract(px * py);
}
function noise(x: number, y: number) {
const ix = Math.floor(x);
const iy = Math.floor(y);
const fx = x - ix;
const fy = y - iy;
const a = hash(ix, iy);
const b = hash(ix + 1, iy);
const c = hash(ix, iy + 1);
const d = hash(ix + 1, iy + 1);
const ux = fx * fx * (3 - 2 * fx);
const uy = fy * fy * (3 - 2 * fy);
return a + (b - a) * ux + (c - a) * uy * (1 - ux) + (d - b) * ux * uy;
}
function fbm(x: number, y: number) {
let v = 0;
let amp = 0.5;
let px = x;
let py = y;
for (let i = 0; i < 4; i++) {
v += amp * noise(px, py);
px *= 2.03;
py *= 2.03;
amp *= 0.5;
}
return v;
}
function warpedField(u: number, v: number, t: number) {
const driftX = Math.sin(t * 0.05) * 1.3;
const driftY = Math.cos(t * 0.04) * 1.3;
const qX = fbm(u + t * 0.012, v + t * 0.012);
const qY = fbm(u + 3.1 + t * 0.009, v + 1.7 + t * 0.009);
const rX = fbm(u + 3 * qX + 1.2 + driftX, v + 3 * qY + 7.3 + driftY);
const rY = fbm(u + 3 * qX + 4.8 + driftX, v + 3 * qY + 2.1 + driftY);
return Math.min(1, Math.max(0, fbm(u + 3 * rX, v + 3 * rY)));
}
export function AsciiWaveField({
cellSize = 13,
speed = 1,
className,
}: {
cellSize?: number;
speed?: number;
className?: string;
}) {
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const canvas = canvasRef.current;
const context = canvas?.getContext("2d");
if (!canvas || !context) return;
const motion = window.matchMedia("(prefers-reduced-motion: reduce)");
let width = 0;
let height = 0;
let frame = 0;
let started = 0;
const cell = Math.max(8, Math.min(28, cellSize));
const animate = speed > 0;
const resize = () => {
const rect = canvas.getBoundingClientRect();
const dpr = Math.min(window.devicePixelRatio || 1, 2);
width = rect.width;
height = rect.height;
canvas.width = Math.max(1, Math.floor(width * dpr));
canvas.height = Math.max(1, Math.floor(height * dpr));
context.setTransform(dpr, 0, 0, dpr, 0, 0);
};
const paint = (now: number) => {
if (!started) started = now;
const t = motion.matches || !animate ? 0 : ((now - started) / 1000) * speed;
context.clearRect(0, 0, width, height);
context.fillStyle = "#000";
context.fillRect(0, 0, width, height);
context.font = `500 ${cell}px ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace`;
context.textAlign = "center";
context.textBaseline = "middle";
const cols = Math.ceil(width / cell);
const rows = Math.ceil(height / cell);
const aspect = width / Math.max(height, 1);
const scaleX = aspect * 2.4;
const scaleY = 2.4;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const x = (col + 0.5) * cell;
const y = (row + 0.5) * cell;
const value = warpedField(
(x / width) * scaleX,
(y / height) * scaleY,
t,
);
const level = Math.min(
ASCII_RAMP.length - 1,
Math.max(0, Math.round(value * (ASCII_RAMP.length - 1))),
);
const glyph = ASCII_RAMP[level];
if (glyph === " ") continue;
const density = level / (ASCII_RAMP.length - 1);
const ridge = density * density;
context.fillStyle = ridge > 0.35 ? "#ffffff" : "#b8becc";
context.globalAlpha = 0.05 + ridge * 0.78;
context.fillText(glyph, x, y);
}
}
context.globalAlpha = 1;
if (animate && !motion.matches) frame = requestAnimationFrame(paint);
};
const restart = () => {
cancelAnimationFrame(frame);
started = 0;
resize();
frame = requestAnimationFrame(paint);
};
restart();
window.addEventListener("resize", resize);
motion.addEventListener("change", restart);
return () => {
cancelAnimationFrame(frame);
window.removeEventListener("resize", resize);
motion.removeEventListener("change", restart);
};
}, [cellSize, speed]);
return (
<canvas
ref={canvasRef}
aria-hidden="true"
className={className ? `block size-full ${className}` : "block size-full"}
/>
);
}The live demo adds staged tutorial layers and playground chrome. Those are for teaching; the block above is the full ambient effect.
How it works
This is not a physics simulation. Nothing has mass, velocity, or collision. We paint a grid of characters. The character in each cell is chosen from a number between 0 and 1. Changing that number over time makes the clouds appear to drift.
Step 1. Start with a canvas
A <canvas> is a blank bitmap. The browser will not lay out text inside it—you paint with the Canvas 2D API. Size the parent so the canvas has a width and height to measure.
Mark the canvas aria-hidden when it is decorative. Put the meaning in a nearby screen-reader sentence.
<div className="relative h-72 overflow-hidden bg-black">
<canvas ref={canvasRef} className="block size-full" aria-hidden="true" />
<span className="sr-only">Animated ASCII wave field.</span>
</div>What you see: a black rectangle. Nothing is drawn yet.
Step 2. Draw a lattice of dashes
Measure the canvas with getBoundingClientRect. Multiply by devicePixelRatio (capped at 2) so the bitmap stays sharp. setTransform(dpr, 0, 0, dpr, 0, 0) lets later coordinates stay in CSS pixels.
Divide the surface into cells of size cell. At the center of each cell, draw a fixed - with fillText.
const cols = Math.ceil(width / cell)
const rows = Math.ceil(height / cell)
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const x = (col + 0.5) * cell
const y = (row + 0.5) * cell
context.fillText("-", x, y)
}
}What you see: a regular grid of dashes. This lattice is the display. Later steps only change which glyph each cell shows.
Step 3. Hash cell coordinates
A hash turns integer coordinates into a repeatable pseudo-random number in 0..1. Same input always gives the same output. Nearby cells are unrelated—so the field looks speckled, not smooth.
function hash(x: number, y: number) {
let px = fract(x * 123.34)
let py = fract(y * 456.21)
const d = px * (px + 45.32) + py * (py + 45.32)
px += d
py += d
return fract(px * py)
}Map the hash to brightness and pick . or =.
What you see: static speckles. No soft clouds yet—hashes jump from cell to cell.
Step 4. Smooth with value noise
Value noise still hashes the integer lattice, but for a fractional point it blends the four surrounding corners. The blend uses a smoothstep curve f*f*(3-2*f) so the field has no hard edges.
function noise(x: number, y: number) {
const ix = Math.floor(x)
const iy = Math.floor(y)
const fx = x - ix
const fy = y - iy
// hash the four corners, then mix with smoothstep weights
}Sample noise at the cell’s UV position, not at the integer column index.
What you see: soft blobs. The speckles have melted into cloudy patches.
Step 5. Stack octaves with FBM
Fractional Brownian motion (FBM) adds several noise layers. Each octave doubles the frequency (p *= 2.03) and halves the amplitude (amp *= 0.5). Large waves set the shape; small waves add detail.
function fbm(x: number, y: number) {
let v = 0
let amp = 0.5
for (let i = 0; i < 4; i++) {
v += amp * noise(x, y)
x *= 2.03
y *= 2.03
amp *= 0.5
}
return v
}What you see: richer clouds with fine structure inside larger ridges.
Step 6. Domain warp and time
Domain warping samples FBM at coordinates that were themselves displaced by FBM. That folds the field into the long, flowing ridges you see on the landing page.
Time t advances each frame. A slow circular drift and small time offsets inside the warp make the pattern reorganize continuously. The glyphs still sit on fixed cell centers—only the sampled value changes.
function warpedField(u: number, v: number, t: number) {
const driftX = Math.sin(t * 0.05) * 1.3
const driftY = Math.cos(t * 0.04) * 1.3
const q = /* fbm of uv + time */
const r = /* fbm of uv + 3*q + drift */
return fbm(u + 3 * r.x, v + 3 * r.y)
}requestAnimationFrame drives the loop. When prefers-reduced-motion: reduce matches, freeze t at 0.
What you see: the clouds begin to drift and fold. Still only a few glyph kinds.
Step 7. Map the field to an ASCII ramp
An ASCII ramp is an ordered list from empty to dense ink:
const ASCII_RAMP = [" ", ".", ":", "-", "=", "+", "*", "%", "@"]Multiply the field value by ramp.length - 1, round to an index, and draw that glyph. Skip spaces. Square the density for alpha so = + * % @ read near-white while . and : stay soft—copy on top stays readable.
const level = Math.round(value * (ASCII_RAMP.length - 1))
const glyph = ASCII_RAMP[level]
const ridge = (level / (ASCII_RAMP.length - 1)) ** 2
context.fillStyle = ridge > 0.35 ? "#ffffff" : "#b8becc"
context.globalAlpha = 0.05 + ridge * 0.78
context.fillText(glyph, x, y)What you see: the finished ambient field—dense white ridges of heavy glyphs, sparse soft dots between them, slowly warping over time.
Step 8. Cleanup
On unmount, cancel the animation frame and remove the resize and media-query listeners. On resize, rebuild the canvas bitmap and redraw. That keeps the effect sharp when the layout changes and prevents leaks when you leave the page.
Playground
Field controls
Tune the lattice and how fast the warped field drifts.