Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158// The docs background: two dithered ridges on a waterline with their reflection, under a few// pixel stars, in the page's wisteria. It is still; only scrolling slides the layers a little.(() => { const PIXEL = 2; // CSS pixels per canvas pixel const STRENGTH = 0.75; const HEIGHT = 0.18; // of the viewport, from the bottom edge to the far shore's foot const STAR_CELL = 26;
// 8x8 Bayer thresholds in 0..1. const bayer = [ 0, 32, 8, 40, 2, 34, 10, 42, 48, 16, 56, 24, 50, 18, 58, 26, 12, 44, 4, 36, 14, 46, 6, 38, 60, 28, 52, 20, 62, 30, 54, 22, 3, 35, 11, 43, 1, 33, 9, 41, 51, 19, 59, 27, 49, 17, 57, 25, 15, 47, 7, 39, 13, 45, 5, 37, 63, 31, 55, 23, 61, 29, 53, 21, ].map((value) => (value + 0.5) / 64);
const clamp = (value) => (value < 0 ? 0 : value > 1 ? 1 : value); function hash(x, y) { let h = (x * 374761393 + y * 668265263) | 0; h = ((h ^ (h >>> 13)) * 1274126177) | 0; return ((h ^ (h >>> 16)) >>> 0) / 4294967296; } function noise(x, y) { const ix = Math.floor(x); const fx = x - ix; const t = fx * fx * (3 - 2 * fx); const a = hash(ix, y); return a + (hash(ix + 1, y) - a) * t; } const ridge = (u, layer) => noise(u, layer * 5) * 0.8 + noise(u * 3.1, layer * 5 + 2) * 0.2;
const canvas = document.createElement('canvas'); canvas.id = 'lake'; canvas.setAttribute('aria-hidden', 'true'); document.body.prepend(canvas); const context = canvas.getContext('2d'); const darkScheme = matchMedia('(prefers-color-scheme: dark)'); const still = matchMedia('(prefers-reduced-motion: reduce)'); let width, height, image, pixels, text, quiet, queued;
function resize() { width = canvas.width = Math.ceil(innerWidth / PIXEL); height = canvas.height = Math.ceil(innerHeight / PIXEL); image = context.createImageData(width, height); pixels = new Uint32Array(image.data.buffer); const span = (element) => { const box = element.getBoundingClientRect(); return [box.left / PIXEL, box.right / PIXEL]; }; text = span(document.querySelector('article')); quiet = [...document.querySelectorAll('article, .site, .toc')].map(span); draw(); }
function draw() { queued = false; const fuji = getComputedStyle(document.documentElement) .getPropertyValue('--fuji') .trim(); const [r, g, b] = [1, 3, 5].map((i) => parseInt(fuji.slice(i, i + 2), 16)); const base = (darkScheme.matches ? 0.2 : 0.13) * STRENGTH; const tone = (alpha) => (((clamp(alpha) * 255) << 24) | (b << 16) | (g << 8) | r) >>> 0; const scroll = still.matches ? 0 : Math.round(scrollY / PIXEL); pixels.fill(0); lake(tone(base), scroll * 0.12); stars(tone(base * 2.6), tone(base * 1.3), Math.round(scroll * 0.25)); context.putImageData(image, 0, 0); }
// How much of the picture shows at a column: thinned under the text, whole in the margins. function clear(x) { const margin = 22 / PIXEL; const inside = Math.min(x - text[0] + margin, text[1] + margin - x); return inside > 0 ? 0.22 + 0.78 * clamp(1 - inside / (margin * 5)) : 1; }
function lake(tone, slide) { const extent = height * HEIGHT; const water = height - extent * 0.42; const scale = PIXEL / 3; for (let y = Math.floor(water - extent * 1.1); y < height; y++) { const below = y > water; // The reflection is the far shore folded over the waterline, squashed and rippled. const mirrored = below ? water - (y - water) * 1.6 : y; const ripple = below ? Math.sin(y * 0.8 + 36) * (1.5 + (y - water) * 0.06) : 0; const streak = hash(7, y) > 0.42 ? 0.75 * clamp(1 - (y - water) / (extent * 0.42)) : 0; for (let x = 0; x < width; x++) { let density = 0; for (let layer = 0; layer < 2; layer++) { const u = (x + ripple + slide * (layer + 1)) * scale * 0.01 * (1 + layer * 0.7) + 0.2 * (layer + 1) + layer * 37; const top = water - ridge(u, layer) * extent * (1.05 - 0.4 * layer) - extent * 0.02; if (mirrored > top) density = (0.22 + 0.2 * layer) * (1 - 0.6 * clamp((mirrored - top) / (extent * 0.5))) + 0.06 * layer; } if (below) density = density * streak + 0.04; if (density * clear(x) > bayer[(y & 7) * 8 + (x & 7)]) pixels[y * width + x] = tone; } } }
// One star at most in each cell of a grid, placed and lit by the cell's hash, none over text. function stars(bright, dim, rise) { const margin = 22 / PIXEL; for ( let row = Math.floor(rise / STAR_CELL) - 1; row <= (rise + height) / STAR_CELL; row++ ) for (let cell = -1; cell <= width / STAR_CELL + 1; cell++) { const h = hash(cell, row * 31 + 5); if (h > 0.16) continue; const x = Math.round((cell + hash(cell + 9, row)) * STAR_CELL); const y = Math.round((row + hash(cell, row + 9)) * STAR_CELL - rise); if (x < 1 || y < 1 || x >= width - 1 || y >= height - 1) continue; if ( quiet.some(([left, right]) => x > left - margin && x < right + margin) ) continue; const light = 0.5 + 0.5 * Math.sin(h * 450 + 16); if (light < 0.22) continue; const at = y * width + x; pixels[at] = light > 0.6 ? bright : dim; if (h < 0.022 && light > 0.75) pixels[at - 1] = pixels[at + 1] = pixels[at - width] = pixels[at + width] = dim; } }
const redraw = () => queued || (queued = requestAnimationFrame(draw)); addEventListener('scroll', redraw, { passive: true }); addEventListener('resize', resize); // The columns settle after the first draw on a phone, and move when a font loads. new ResizeObserver(resize).observe(document.querySelector('article')); darkScheme.addEventListener('change', redraw); resize();})();