${p.title}
${L.lede ? `${L.lede}
` : ''}/* light studies — a blog lit by a window.
One fragment shader paints a pleated curtain and the light that falls on it.
Every post sets the sun differently; hovering a title turns the window into its letters. */
const $ = (s, el = document) => el.querySelector(s);
// ---------- per-post light ----------
// angle (rad), bands (panes), warm (0 cool .. 1 amber), pleat (fold freq), kind
const LIGHT = {
'css-grid-lanes': { kind: 'code', angle: -0.62, bands: 5.0, warm: 0.35, pleat: 30, lede: 'a short one about columns' },
'spool': { kind: 'code', angle: -0.48, bands: 7.0, warm: 0.2, pleat: 36, lede: 'tasks as append-only light' },
'ferrotype': { kind: 'code', angle: -0.75, bands: 9.0, warm: 0.1, pleat: 40, lede: 'types, developed on a metal plate' },
'building-ui-in-gpui': { kind: 'code', angle: -0.55, bands: 6.0, warm: 0.25, pleat: 33, lede: 'tips, loosely held' },
'the-black-magic-of-canvas-and-text-editing': { kind: 'note', angle: -0.9, bands: 1.6, warm: 0.7, pleat: 26, lede: 'a reading list' },
'fig-gen-making-generative-art-in-figma': { kind: 'image', angle: -0.42, bands: 3.0, warm: 0.55, pleat: 42, lede: 'sin, cos, tan and an evening' },
'writing-with-adhd': { kind: 'prose', angle: -0.6, bands: 4.0, warm: 0.9, pleat: 32, lede: 'read it from morning to evening' },
'design-workshop': { kind: 'image', angle: -0.5, bands: 3.5, warm: 0.65, pleat: 28, lede: 'making for the sake of making' },
};
const DEFAULT_LIGHT = { angle: -0.58, bands: 4.2, warm: 0.6, pleat: 33 };
function lightFor(p) {
if (LIGHT[p.slug]) return LIGHT[p.slug];
const imgs = (p.html.match(/ 3 ? 'image' : pres > 1 ? 'code' : p.html.length < 2500 ? 'note' : 'prose';
return { ...DEFAULT_LIGHT, kind, lede: '' };
}
// ---------- GL ----------
const canvas = $('#gl');
const gl = canvas.getContext('webgl', { antialias: false, premultipliedAlpha: false });
const VERT = `attribute vec2 a; void main(){ gl_Position = vec4(a,0.,1.); }`;
const FRAG = `
precision highp float;
uniform vec2 uRes;
uniform float uTime;
uniform vec2 uMouse;
uniform sampler2D uText;
uniform float uTextAmt, uTextAspect;
uniform float uAngle, uBands, uWarm, uPleat, uOpen, uDim, uShift, uSpot;
float h1(float n){ return fract(sin(n)*43758.5453123); }
float n1(float x){ float i=floor(x), f=fract(x); return mix(h1(i), h1(i+1.), f*f*(3.-2.*f)); }
float hash(vec2 p){ p = fract(p*vec2(123.34, 456.21)); p += dot(p, p+45.32); return fract(p.x*p.y); }
// height of the drape at x — big folds, wandering folds, small creases
float drape(float x, float y, float t){
float k = uPleat;
float w = x*k + 1.4*sin(x*k*0.31 + 0.7) + 0.6*n1(x*k*0.21);
float hgt = sin(w) * (0.75 + 0.25*sin(x*k*0.13 + 2.0));
hgt += 0.16*sin(x*k*2.3 + 1.7 + 0.4*sin(y*3.0 + t*0.3));
hgt += 0.04*sin(x*k*5.1 + 0.3);
return hgt;
}
float projected(vec2 p, float soft){
// stripes of window light, rotated, sweeping with uShift
vec2 c = p - vec2(0.5*uRes.x/uRes.y, 0.5);
float ca = cos(uAngle), sa = sin(uAngle);
vec2 q = vec2(ca*c.x - sa*c.y, sa*c.x + ca*c.y);
float s = fract(q.y*uBands*0.55 + uShift);
float pane = smoothstep(0.0, soft, s) * smoothstep(0.62, 0.62 - soft, s);
// mullion: a thinner crossbar of shadow perpendicular to the panes
float m = abs(fract(q.x*0.9 + 0.25) - 0.5);
pane *= smoothstep(0.0, soft*0.8, m - 0.035);
// the window has edges too
float win = smoothstep(-1.15, -0.75, q.x) * smoothstep(1.25, 0.8, q.x);
return pane * win;
}
float letters(vec2 p, float soft){
vec2 c = p - vec2(0.5*uRes.x/uRes.y, 0.52);
c.x += c.y*0.38; // skewed like a raking sun
c *= 1.55;
vec2 uv = vec2(c.x/uTextAspect, -c.y) + 0.5;
float a = 0.0; float tot = 0.0;
for (int i = -2; i <= 2; i++){
for (int j = -2; j <= 2; j++){
vec2 o = vec2(float(i), float(j))*soft*0.0035;
float wgt = 1.0 - length(vec2(i,j))*0.18;
a += texture2D(uText, uv + o).r * wgt; tot += wgt;
}
}
a /= tot;
float inside = step(0.0, uv.x)*step(uv.x, 1.0)*step(0.0, uv.y)*step(uv.y, 1.0);
return a*inside;
}
void main(){
vec2 frag = gl_FragCoord.xy;
vec2 p = frag / uRes.y;
float t = uTime;
// the curtain breathes — more at the hem
float sway = (0.006 + 0.01*(1.0 - p.y)) * sin(t*0.35 + p.y*2.2) + 0.003*sin(t*0.9 + p.y*7.0);
float x = p.x + sway;
float e = 0.0015;
float hc = drape(x, p.y, t);
float dh = (drape(x+e, p.y, t) - drape(x-e, p.y, t)) / (2.0*e) / uPleat;
// folds catch light on one side, fall into shadow on the other
float lambert = clamp(0.55 + 0.38*dh, 0.0, 1.0);
float ao = 0.55 + 0.45*smoothstep(-1.1, 1.0, hc);
float rib = 0.975 + 0.025*sin(x*uRes.y*0.7);
rib *= 0.94 + 0.06*n1(x*uRes.y*0.05);
// light is displaced by fold depth: shadow edges ripple across the pleats
vec2 lp = p + vec2(hc*0.012, hc*0.018);
float soft = 0.06 + 0.05*(1.0 - ao);
float win = projected(lp, soft);
float txt = letters(lp + vec2(hc*0.004, 0.0), 1.0 + 1.5*(1.0 - ao));
float light = mix(win, txt*1.25, uTextAmt);
// a held spot that follows the cursor a little
vec2 mp = uMouse * vec2(uRes.x/uRes.y, 1.0);
light += uSpot * 0.5 * smoothstep(0.38, 0.0, length(p - mp)) * (0.4 + 0.6*lambert);
light *= uOpen;
vec3 deep = vec3(0.06, 0.08, 0.13);
vec3 cloth = vec3(0.19, 0.24, 0.34);
vec3 base = mix(deep, cloth, lambert*ao) * rib;
vec3 cool = vec3(0.80, 0.90, 1.00);
vec3 amber = vec3(1.00, 0.86, 0.68);
vec3 lc = mix(cool, amber, uWarm);
// subsurface: light in the folds goes slightly blue, on crests slightly warm
vec3 lit = lc * (0.58 + 0.5*lambert) * rib;
lit = mix(lit * vec3(0.78, 0.86, 1.05), lit, ao);
float L = clamp(light, 0.0, 1.4) * (0.35 + 0.65*lambert);
vec3 col = mix(base, lit, clamp(L, 0.0, 1.0)) + lc*max(L-1.0, 0.0)*0.6;
// bloom-ish bleed around the light
col += lc * 0.05 * win * uOpen * (1.0 - uTextAmt);
// vignette, dim, grain
vec2 uv = frag/uRes;
col *= 0.65 + 0.35*smoothstep(1.15, 0.25, length(uv - vec2(0.45, 0.55)));
col *= uDim;
float g = hash(frag + fract(t*7.0)*100.0) - 0.5;
col += g * 0.045;
col = pow(col, vec3(0.95));
gl_FragColor = vec4(col, 1.0);
}`;
let prog, U = {}, texText, glOK = !!gl;
function initGL() {
if (!gl) return;
const sh = (type, src) => { const s = gl.createShader(type); gl.shaderSource(s, src); gl.compileShader(s);
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) { console.error(gl.getShaderInfoLog(s)); glOK = false; } return s; };
prog = gl.createProgram();
gl.attachShader(prog, sh(gl.VERTEX_SHADER, VERT));
gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, FRAG));
gl.linkProgram(prog); 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, 'a'); gl.enableVertexAttribArray(loc); gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);
['uRes', 'uTime', 'uMouse', 'uText', 'uTextAmt', 'uTextAspect', 'uAngle', 'uBands', 'uWarm', 'uPleat', 'uOpen', 'uDim', 'uShift', 'uSpot']
.forEach(n => U[n] = gl.getUniformLocation(prog, n));
texText = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texText);
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);
gl.uniform1i(U.uText, 0);
}
// text → texture: the letters become the window
const tc = document.createElement('canvas');
tc.width = 1600; tc.height = 640;
const tx = tc.getContext('2d');
let textAspect = tc.width / tc.height;
let currentText = '';
function setText(str) {
if (!gl || str === currentText) return;
currentText = str;
tx.fillStyle = '#000'; tx.fillRect(0, 0, tc.width, tc.height);
tx.fillStyle = '#fff'; tx.textAlign = 'center'; tx.textBaseline = 'middle';
const words = str.split(' ');
// break into at most 3 lines, balanced
let lines = [str];
let size = 300;
tx.font = `italic ${size}px "Instrument Serif"`;
if (tx.measureText(str).width > tc.width * 0.94) {
const n = Math.min(3, Math.ceil(tx.measureText(str).width / (tc.width * 0.9)));
lines = []; const per = Math.ceil(words.length / n);
for (let i = 0; i < words.length; i += per) lines.push(words.slice(i, i + per).join(' '));
}
const widest = Math.max(...lines.map(l => tx.measureText(l).width));
size = Math.min(300, size * (tc.width * 0.94) / widest, (tc.height * 0.92) / (lines.length * 0.95));
tx.font = `italic ${size}px "Instrument Serif"`;
lines.forEach((l, i) => tx.fillText(l, tc.width / 2, tc.height / 2 + (i - (lines.length - 1) / 2) * size * 0.92));
gl.bindTexture(gl.TEXTURE_2D, texText);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, gl.LUMINANCE, gl.UNSIGNED_BYTE, tc);
}
// smoothed light state
const S = { angle: -0.6, bands: 4.2, warm: 0.6, pleat: 33, open: 0, dim: 1, textAmt: 0, shift: 0, spot: 0 };
const T = { ...S, open: 1 };
const mouse = { x: 0.5, y: 0.5, sx: 0.5, sy: 0.5 };
addEventListener('pointermove', e => { mouse.x = e.clientX / innerWidth; mouse.y = 1 - e.clientY / innerHeight; });
let W = 0, H = 0;
function resize() {
const dpr = Math.min(devicePixelRatio || 1, 1.5);
W = canvas.width = Math.floor(innerWidth * dpr);
H = canvas.height = Math.floor(innerHeight * dpr);
if (gl) gl.viewport(0, 0, W, H);
}
addEventListener('resize', resize);
const t0 = performance.now();
let last = t0;
function frame(now) {
const dt = Math.min(0.05, (now - last) / 1000); last = now;
const k = 1 - Math.pow(0.02, dt); // ~ease toward targets
const kk = 1 - Math.pow(0.12, dt); // slower for color/angle
for (const key in T) S[key] += (T[key] - S[key]) * (key === 'textAmt' || key === 'open' ? k : kk);
mouse.sx += (mouse.x - mouse.sx) * k; mouse.sy += (mouse.y - mouse.sy) * k;
if (gl && glOK) {
gl.uniform2f(U.uRes, W, H);
gl.uniform1f(U.uTime, (now - t0) / 1000);
gl.uniform2f(U.uMouse, mouse.sx, mouse.sy);
gl.uniform1f(U.uTextAmt, S.textAmt);
gl.uniform1f(U.uTextAspect, textAspect);
gl.uniform1f(U.uAngle, S.angle + (mouse.sx - 0.5) * 0.25);
gl.uniform1f(U.uBands, S.bands);
gl.uniform1f(U.uWarm, S.warm);
gl.uniform1f(U.uPleat, S.pleat);
gl.uniform1f(U.uOpen, S.open);
gl.uniform1f(U.uDim, S.dim);
gl.uniform1f(U.uShift, S.shift + (now - t0) / 1000 * 0.006 + (mouse.sy - 0.5) * 0.08);
gl.uniform1f(U.uSpot, S.spot);
gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, texText);
gl.drawArrays(gl.TRIANGLES, 0, 3);
}
tickClock();
requestAnimationFrame(frame);
}
// ---------- clock: scroll is time of day ----------
const clockEl = $('#clock');
let clockMode = 'index';
function tickClock() {
let mins;
if (clockMode === 'post') {
const max = Math.max(1, document.documentElement.scrollHeight - innerHeight);
const f = Math.min(1, scrollY / max);
mins = 6 * 60 + 40 + f * (13 * 60); // 06:40 → 19:40 across a post
} else {
mins = 16 * 60 + 20 + (mouse.sx - 0.5) * 120; // late afternoon on the index
}
const hh = String(Math.floor(mins / 60)).padStart(2, '0'), mm = String(Math.floor(mins % 60)).padStart(2, '0');
clockEl.textContent = `${hh}:${mm}`;
}
// ---------- content ----------
let posts = [];
const app = $('#app');
const fmt = d => new Date(d + 'T12:00:00').toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' });
const words = html => html.replace(/
/g, '').replace(/<[^>]+>/g, ' ').split(/\s+/).filter(Boolean).length;
function stripLeadH1(html, title) {
return html.replace(/^\s*]*>([\s\S]*?)<\/h1>\s*/, (m, inner) =>
inner.replace(/<[^>]+>/g, '').trim().toLowerCase() === title.toLowerCase() ? '' : m);
}
function renderIndex() {
clockMode = 'index';
document.body.dataset.view = 'index';
document.body.dataset.kind = '';
Object.assign(T, DEFAULT_LIGHT, { textAmt: 0, dim: 1, spot: 0.35 });
T.shift = S.shift;
app.innerHTML = `
light studies
Eight entries by Nate Butler on design, type systems, canvases and making things. Hover to let one in.
${posts.map((p, i) => {
const L = lightFor(p);
return `-
${String(i + 1).padStart(2, '0')}
${p.title}
${L.kind}${fmt(p.date)}
`;
}).join('')}
move the cursor to move the sun
`;
app.querySelectorAll('.entries a').forEach(a => {
const p = posts.find(x => x.slug === a.dataset.slug), L = lightFor(p);
a.addEventListener('pointerenter', () => {
setText(p.title.replace(/:.*$/, ''));
Object.assign(T, { textAmt: 1, angle: L.angle, warm: L.warm, pleat: L.pleat });
app.querySelector('.entries').classList.add('focus');
});
a.addEventListener('pointerleave', () => {
Object.assign(T, DEFAULT_LIGHT, { textAmt: 0 });
app.querySelector('.entries').classList.remove('focus');
});
});
const dbg = new URLSearchParams(location.search).get('hover');
if (dbg) { const a = app.querySelectorAll('.entries a')[+dbg]; a.dispatchEvent(new Event('pointerenter')); a.classList.add('hov'); }
}
function renderPost(p) {
clockMode = 'post';
const L = lightFor(p);
document.body.dataset.view = 'post';
document.body.dataset.kind = L.kind;
setText(p.title.replace(/:.*$/, ''));
Object.assign(T, { angle: L.angle, bands: L.bands, warm: L.warm, pleat: L.pleat, textAmt: 0.85, dim: 1, spot: 0 });
const idx = posts.indexOf(p);
const next = posts[(idx + 1) % posts.length];
const html = stripLeadH1(p.html, p.title);
const wc = words(p.html);
const mins = Math.max(1, Math.round(wc / 230));
app.innerHTML = `
${p.title}
${L.lede ? `${L.lede}
` : ''}
${html}
`;
const body = $('.body', app);
shape[L.kind]?.(body, p);
body.querySelectorAll('a[href^="http"]').forEach(a => { a.target = '_blank'; a.rel = 'noopener'; });
body.querySelectorAll('img').forEach(img => { img.loading = 'lazy'; img.decoding = 'async'; });
reveal();
}
// each kind of post is staged differently
const shape = {
// gens are black with white lines — screen them straight onto the curtain as light
image(body, p) {
if (p.slug.startsWith('fig-gen')) {
const imgs = [...body.querySelectorAll('img')];
const gens = imgs.slice(3);
if (gens.length) {
const strip = document.createElement('div');
strip.className = 'gens';
gens.forEach((img, i) => {
const f = document.createElement('figure');
f.style.setProperty('--d', i);
f.innerHTML = `gen ${String(i + 1).padStart(2, '0')}`;
f.prepend(img);
strip.append(f);
});
const anchor = [...body.querySelectorAll('p')].find(x => /favorite gens/i.test(x.textContent));
(anchor || body.lastElementChild).after(strip);
body.querySelectorAll('p').forEach(x => { if (!x.textContent.trim() && !x.querySelector('img')) x.remove(); });
}
body.classList.add('screened');
}
body.querySelectorAll('p').forEach(par => {
const imgs = par.querySelectorAll('img');
if (imgs.length && par.textContent.trim() === '') {
const fig = document.createElement('figure'); fig.className = 'plate';
imgs.forEach(i => fig.append(i));
if (imgs[0].alt) fig.insertAdjacentHTML('beforeend', `${imgs[0].alt} `);
par.replaceWith(fig);
}
});
},
code(body) {
body.querySelectorAll('pre').forEach((pre, i) => {
const wrap = document.createElement('div'); wrap.className = 'pane';
const lines = pre.querySelectorAll('.line').length;
wrap.innerHTML = `fig. ${i + 1} — ${lines} line${lines === 1 ? '' : 's'}`;
pre.replaceWith(wrap); wrap.append(pre);
});
body.querySelectorAll('p').forEach(par => {
const imgs = par.querySelectorAll('img');
if (imgs.length && par.textContent.trim() === '') {
const fig = document.createElement('figure'); fig.className = 'plate';
imgs.forEach(i => fig.append(i));
if (imgs[0].alt) fig.insertAdjacentHTML('beforeend', `${imgs[0].alt} `);
par.replaceWith(fig);
}
});
},
// the long one: hours pass as you read; h2s become hours on the wall
prose(body) {
const h2s = [...body.querySelectorAll('h2')];
h2s.forEach((h, i) => {
const f = (i + 1) / (h2s.length + 1);
const mins = 6 * 60 + 40 + f * 13 * 60;
h.dataset.hour = `${String(Math.floor(mins / 60)).padStart(2, '0')}:${String(Math.floor(mins % 60)).padStart(2, '0')}`;
});
const first = body.querySelector('p'); first && first.classList.add('first');
},
// a reading list: links stand alone in the light
note(body) {
body.querySelectorAll('p').forEach(par => {
const a = par.querySelector('a > strong');
if (a && par.firstElementChild?.tagName === 'A') par.classList.add('src');
});
},
};
function reveal() {
const els = [...app.querySelectorAll('.ph > *, .body > *, .pf')].filter(el => !el.matches('.gens, .screened .plate'));
const io = new IntersectionObserver(es => es.forEach(e => {
if (e.isIntersecting) { e.target.classList.add('in'); io.unobserve(e.target); }
}), { rootMargin: '0px 0px -8% 0px' });
els.forEach((el, i) => { el.classList.add('rv'); el.style.setProperty('--rd', `${Math.min(i, 8) * 60}ms`); io.observe(el); });
}
// scroll drives the sun inside a post
addEventListener('scroll', () => {
if (document.body.dataset.view !== 'post') return;
const max = Math.max(1, document.documentElement.scrollHeight - innerHeight);
const f = scrollY / max;
const p = posts.find(x => x.slug === location.hash.slice(1)); if (!p) return;
const L = lightFor(p);
T.textAmt = Math.max(0, 0.85 - scrollY / (innerHeight * 0.7)) ;
T.shift = f * (L.kind === 'prose' ? 1.6 : 0.6);
T.angle = L.angle - f * (L.kind === 'prose' ? 0.7 : 0.25);
if (L.kind === 'prose') T.warm = 0.3 + 0.7 * Math.sin(Math.PI * (0.15 + f * 0.85)); // cool dawn, warm noon, amber dusk
}, { passive: true });
function route() {
const slug = decodeURIComponent(location.hash.slice(1));
const p = posts.find(x => x.slug === slug);
app.classList.remove('enter'); void app.offsetWidth;
if (p) renderPost(p); else renderIndex();
app.classList.add('enter');
scrollTo(0, 0);
S.open = Math.min(S.open, 0.35); // a blink: the light gets drawn again
}
addEventListener('hashchange', route);
(async function boot() {
resize(); initGL();
try { await document.fonts.load('italic 100px "Instrument Serif"'); } catch (e) {}
setText('nate.rip');
requestAnimationFrame(frame);
posts = await fetch('../content/posts.json').then(r => r.json());
route();
})();