ASCII field
Hero backgrounds. Floodlight drifting over the pitch, drawn as balls, rackets and kit.
Floodlights on.
Move over it.
- Sport
- Football
- Move
- Floodlight drifting over the pitch, drawn as balls, rackets and kit.
- For
- Hero backgrounds
WebGL, one draw call · pointer lifts the light · reduced: still frame
01 Install
npx shadcn@latest add https://moveui-red.vercel.app/r/ascii-field.jsonIt brings the tokens with it. The first time, import the tokens once in your root layout:
import "@/components/moveui/tokens.css";Not using shadcn? Copy the files below into components/moveui/, and tokens.css next to them.
02 Use it
import { AsciiField } from "@/components/moveui/ascii-field/ascii-field";
// The field fills its parent. Content after it needs position: relative to sit on top.
<section style={{ position: "relative", background: "var(--ml-night)" }}>
<AsciiField glyphs="sport" />
<h1 style={{ position: "relative" }}>Floodlights on.</h1>
</section>03 Or ask your agent
Paste this into Claude Code, Cursor or any coding agent. It installs the piece and wires it in.
Add the Move UI "ASCII field" piece to this project. It's for: Hero backgrounds.
1. Install it: npx shadcn@latest add https://moveui-red.vercel.app/r/ascii-field.json
2. If the project doesn't import the Move UI tokens yet, add this once to the root layout: import "@/components/moveui/tokens.css";
3. Use it like this, adapted to where it belongs in my code:
import { AsciiField } from "@/components/moveui/ascii-field/ascii-field";
// The field fills its parent. Content after it needs position: relative to sit on top.
<section style={{ position: "relative", background: "var(--ml-night)" }}>
<AsciiField glyphs="sport" />
<h1 style={{ position: "relative" }}>Floodlights on.</h1>
</section>
Keep its reduced-motion behaviour and keyboard support. Don't hardcode durations, easing or colours: they come from the --ml-* tokens.
Docs: https://moveui-red.vercel.app/pieces/ascii-fieldWant your agent to know the whole library and its motion rules? Add the Move UI skill to your project:
npx shadcn@latest add https://moveui-red.vercel.app/r/skill.json04 Code
"use client";
import { useEffect, useRef } from "react";
import "./ascii-field.css";
/**
* ASCII field: a WebGL layer that draws slow drifting light as sport pictograms (dots, balls, rackets,
* shuttlecocks, dumbbells), heavier kit where the light is brighter. glyphs="ascii" draws " .:-=+*#%@".
* Sits on top of any background (absolute, fills its parent). The pointer lifts the light around it.
* Pauses off screen and in background tabs. Reduced motion: one still frame. No WebGL: renders nothing.
*/
const RAMP = " .:-=+*#%@";
/*
Sport set: ten pictograms from empty to full ink, so brighter light draws heavier kit.
Empty, dot, far ball, tennis ball, racket, shuttlecock, basketball, football, dumbbell, solid ball.
Drawn as white line art in a 64px cell, the same weight as the characters they replace.
*/
const SPORT_STEPS = 10;
function drawSport(ctx: CanvasRenderingContext2D, i: number, x: number, y: number, G: number) {
const r = G * 0.3, lw = G * 0.075;
ctx.save();
ctx.translate(x, y);
ctx.lineWidth = lw;
ctx.lineCap = "round";
ctx.lineJoin = "round";
ctx.strokeStyle = ctx.fillStyle = "#fff";
const circle = (rad: number) => { ctx.beginPath(); ctx.arc(0, 0, rad, 0, Math.PI * 2); };
switch (i) {
case 0: break;
case 1: circle(G * 0.06); ctx.fill(); break; // a dot
case 2: circle(r * 0.55); ctx.stroke(); break; // a ball far away
case 3: // tennis ball: two seams
circle(r * 0.8); ctx.stroke();
ctx.beginPath(); ctx.arc(-r * 1.15, 0, r * 0.75, -0.75, 0.75); ctx.stroke();
ctx.beginPath(); ctx.arc(r * 1.15, 0, r * 0.75, Math.PI - 0.75, Math.PI + 0.75); ctx.stroke();
break;
case 4: // racket: strung head and a handle
ctx.rotate(-Math.PI / 4);
ctx.beginPath(); ctx.ellipse(0, -r * 0.35, r * 0.6, r * 0.78, 0, 0, Math.PI * 2); ctx.stroke();
ctx.lineWidth = lw * 0.55;
ctx.beginPath(); ctx.moveTo(0, -r * 1.05); ctx.lineTo(0, r * 0.35); ctx.moveTo(-r * 0.52, -r * 0.35); ctx.lineTo(r * 0.52, -r * 0.35); ctx.stroke();
ctx.lineWidth = lw * 1.3;
ctx.beginPath(); ctx.moveTo(0, r * 0.45); ctx.lineTo(0, r * 1.2); ctx.stroke();
break;
case 5: // shuttlecock: cork and feathers
ctx.rotate(Math.PI / 4);
ctx.beginPath(); ctx.moveTo(-r * 0.25, r * 0.4); ctx.lineTo(-r * 0.7, -r * 0.9); ctx.lineTo(r * 0.7, -r * 0.9); ctx.lineTo(r * 0.25, r * 0.4); ctx.closePath(); ctx.stroke();
ctx.beginPath(); ctx.moveTo(0, r * 0.4); ctx.lineTo(0, -r * 0.9); ctx.stroke();
ctx.beginPath(); ctx.arc(0, r * 0.62, r * 0.32, 0, Math.PI); ctx.fill();
break;
case 6: // basketball
circle(r); ctx.stroke();
ctx.beginPath(); ctx.moveTo(-r, 0); ctx.lineTo(r, 0); ctx.moveTo(0, -r); ctx.lineTo(0, r); ctx.stroke();
ctx.beginPath(); ctx.arc(-r * 1.25, 0, r * 0.85, -0.85, 0.85); ctx.stroke();
ctx.beginPath(); ctx.arc(r * 1.25, 0, r * 0.85, Math.PI - 0.85, Math.PI + 0.85); ctx.stroke();
break;
case 7: { // football: a filled pentagon and its seams
circle(r); ctx.stroke();
const p = r * 0.42;
ctx.beginPath();
for (let k = 0; k < 5; k++) { const a = -Math.PI / 2 + (k * 2 * Math.PI) / 5; ctx[k ? "lineTo" : "moveTo"](Math.cos(a) * p, Math.sin(a) * p); }
ctx.closePath(); ctx.fill();
ctx.beginPath();
for (let k = 0; k < 5; k++) { const a = -Math.PI / 2 + (k * 2 * Math.PI) / 5; ctx.moveTo(Math.cos(a) * p, Math.sin(a) * p); ctx.lineTo(Math.cos(a) * r, Math.sin(a) * r); }
ctx.stroke();
break;
}
case 8: // dumbbell
ctx.rotate(-Math.PI / 4);
ctx.lineWidth = lw * 1.2;
ctx.beginPath(); ctx.moveTo(-r * 0.7, 0); ctx.lineTo(r * 0.7, 0); ctx.stroke();
ctx.fillRect(-r * 1.15, -r * 0.6, r * 0.42, r * 1.2);
ctx.fillRect(r * 0.73, -r * 0.6, r * 0.42, r * 1.2);
break;
default: // a solid ball with its seams cut out
circle(r * 1.05); ctx.fill();
ctx.globalCompositeOperation = "destination-out";
ctx.lineWidth = lw * 0.7;
ctx.beginPath(); ctx.moveTo(-r, 0); ctx.lineTo(r, 0); ctx.stroke();
ctx.beginPath(); ctx.arc(-r * 1.25, 0, r * 0.85, -0.85, 0.85); ctx.stroke();
ctx.beginPath(); ctx.arc(r * 1.25, 0, r * 0.85, Math.PI - 0.85, Math.PI + 0.85); ctx.stroke();
}
ctx.restore();
}
const VERT = `
attribute vec2 p;
void main() { gl_Position = vec4(p, 0.0, 1.0); }`;
const FRAG = `
precision mediump float;
uniform vec2 uRes;
uniform float uTime;
uniform vec2 uMouse;
uniform float uCell;
uniform float uGlyphs;
uniform float uOpacity;
uniform sampler2D uAtlas;
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
float noise(vec2 p) {
vec2 i = floor(p), f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(mix(hash(i), hash(i + vec2(1, 0)), u.x), mix(hash(i + vec2(0, 1)), hash(i + vec2(1, 1)), u.x), u.y);
}
float fbm(vec2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 4; i++) { v += a * noise(p); p *= 2.02; a *= 0.5; }
return v;
}
void main() {
vec2 cell = floor(gl_FragCoord.xy / uCell);
vec2 inCell = fract(gl_FragCoord.xy / uCell);
vec2 c = (cell + 0.5) * uCell; // cell centre in px
vec2 uv = c / uRes.y;
// Two slow layers of drifting light, brighter toward the top like the backdrop.
float t = uTime * 0.04;
float n = fbm(uv * 1.6 + vec2(t, -t * 0.6));
n = mix(n, fbm(uv * 3.2 - vec2(t * 0.7, t * 0.3) + n), 0.45);
float lum = smoothstep(0.32, 0.82, n) * mix(0.35, 1.0, c.y / uRes.y);
// The pointer lifts the light in a soft circle.
float d = distance(c, uMouse);
lum += 0.55 * exp(-d * d / (2.0 * pow(uRes.y * 0.14, 2.0)));
lum = clamp(lum, 0.0, 1.0);
float idx = floor(lum * (uGlyphs - 1.0) + 0.5);
float a = texture2D(uAtlas, vec2((idx + inCell.x) / uGlyphs, inCell.y)).a;
a *= uOpacity * (0.35 + 0.65 * lum);
gl_FragColor = vec4(vec3(a), a); // white, premultiplied
}`;
type Props = {
cell?: number;
opacity?: number;
className?: string;
/** "sport" draws balls, rackets and kit; "ascii" draws the classic " .:-=+*#%@" ramp. */
glyphs?: "sport" | "ascii";
};
export function AsciiField({ cell = 12, opacity = 0.55, className, glyphs = "sport" }: Props) {
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
// A fresh canvas per mount: a context lost in cleanup can never be handed back to the next mount.
const wrap = ref.current;
if (!wrap) return;
const canvas = document.createElement("canvas");
canvas.style.cssText = "display:block;width:100%;height:100%";
wrap.append(canvas);
const gl = canvas.getContext("webgl", { premultipliedAlpha: true, antialias: false });
if (!gl) { canvas.remove(); return; }
const reduce = matchMedia("(prefers-reduced-motion: reduce)").matches;
const compile = (type: number, src: string) => {
const s = gl.createShader(type)!;
gl.shaderSource(s, src);
gl.compileShader(s);
return s;
};
const prog = gl.createProgram()!;
gl.attachShader(prog, compile(gl.VERTEX_SHADER, VERT));
gl.attachShader(prog, compile(gl.FRAGMENT_SHADER, FRAG));
gl.linkProgram(prog);
if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) { canvas.remove(); return; }
gl.useProgram(prog);
const buf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
const loc = gl.getAttribLocation(prog, "p");
gl.enableVertexAttribArray(loc);
gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);
// Glyph atlas: one row of cells, sport pictograms or characters in the page's mono font.
const G = 64;
const steps = glyphs === "sport" ? SPORT_STEPS : RAMP.length;
const atlas = document.createElement("canvas");
atlas.width = G * steps;
atlas.height = G;
const ctx = atlas.getContext("2d")!;
if (glyphs === "sport") {
for (let i = 0; i < steps; i++) drawSport(ctx, i, i * G + G / 2, G / 2, G);
} else {
const mono = getComputedStyle(document.documentElement).getPropertyValue("--font-geist-mono").trim() || "monospace";
ctx.font = `500 ${G * 0.78}px ${mono}, monospace`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#fff";
[...RAMP].forEach((ch, i) => ctx.fillText(ch, i * G + G / 2, G / 2 + G * 0.04));
}
const tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, atlas);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
const u = (n: string) => gl.getUniformLocation(prog, n);
const uRes = u("uRes"), uTime = u("uTime"), uMouse = u("uMouse"), uCell = u("uCell");
gl.uniform1f(u("uGlyphs"), steps);
gl.uniform1f(u("uOpacity"), opacity);
gl.uniform1i(u("uAtlas"), 0);
let dpr = 1;
const resize = () => {
dpr = Math.min(devicePixelRatio || 1, 2);
canvas.width = Math.round(canvas.clientWidth * dpr);
canvas.height = Math.round(canvas.clientHeight * dpr);
gl.viewport(0, 0, canvas.width, canvas.height);
gl.uniform2f(uRes, canvas.width, canvas.height);
gl.uniform1f(uCell, Math.round(cell * dpr));
if (reduce) draw(0);
};
// Pointer, eased so the glow trails the hand a little. Starts off screen.
const m = { x: -1e4, y: -1e4 }, mt = { x: -1e4, y: -1e4 };
const host = wrap.parentElement ?? wrap;
const onMove = (e: PointerEvent) => {
const r = canvas.getBoundingClientRect();
mt.x = (e.clientX - r.left) * dpr;
mt.y = (r.bottom - e.clientY) * dpr;
if (m.x < -1e3) { m.x = mt.x; m.y = mt.y; }
if (reduce) draw(0);
};
const onLeave = () => { mt.x = mt.y = -1e4; };
host.addEventListener("pointermove", onMove, { passive: true });
host.addEventListener("pointerleave", onLeave);
let raf = 0, visible = true;
const t0 = performance.now();
function draw(time: number) {
m.x += (mt.x - m.x) * 0.08;
m.y += (mt.y - m.y) * 0.08;
gl!.uniform1f(uTime, time);
gl!.uniform2f(uMouse, reduce ? mt.x : m.x, reduce ? mt.y : m.y);
gl!.drawArrays(gl!.TRIANGLES, 0, 3);
}
const loop = (t: number) => {
draw((t - t0) / 1000);
raf = requestAnimationFrame(loop);
};
const run = () => {
cancelAnimationFrame(raf);
raf = 0;
if (!reduce && visible && !document.hidden) raf = requestAnimationFrame(loop);
};
const ro = new ResizeObserver(resize);
ro.observe(canvas);
const io = new IntersectionObserver(([e]) => { visible = e.isIntersecting; run(); });
io.observe(canvas);
document.addEventListener("visibilitychange", run);
resize();
if (reduce) draw(0);
run();
return () => {
cancelAnimationFrame(raf);
ro.disconnect();
io.disconnect();
document.removeEventListener("visibilitychange", run);
host.removeEventListener("pointermove", onMove);
host.removeEventListener("pointerleave", onLeave);
gl.getExtension("WEBGL_lose_context")?.loseContext();
canvas.remove();
};
}, [cell, opacity, glyphs]);
return <div ref={ref} aria-hidden="true" className={["ml-ascii", className].filter(Boolean).join(" ")} />;
}
/*
ASCII field: fills its parent (give the parent position: relative) and never takes the pointer.
.is-off fades it out on --ml-dur-travel, for a toggle.
*/
.ml-ascii {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
transition: opacity var(--ml-dur-travel) var(--ml-ease-out);
}
.ml-ascii.is-off { opacity: 0; }
