Settings → Appearance: upload an image and every page header draws it as the dot matrix instead of The Creation of Adam, with a clickable focus point and a switch back to the built-in painting. The browser turns the image into a brightness grid; only that grid is saved as a setting. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
133 lines
5.3 KiB
TypeScript
133 lines
5.3 KiB
TypeScript
"use client";
|
||
|
||
import { createContext, useContext, useEffect, useMemo, useRef } from "react";
|
||
import type { Backdrop } from "@/lib/backdrop";
|
||
|
||
const SPACING = 6; // CSS px between dot centres
|
||
|
||
type Grid = { w: number; h: number; levels: Uint8Array };
|
||
const parse = (dots: string) => Uint8Array.from(dots, (c) => parseInt(c, 16));
|
||
|
||
// The brightness grid (26 KB of text) is its own chunk, shared by every page and parsed once per
|
||
// tab. Imported statically, it was copied into each page's chunk.
|
||
let grid: Promise<Grid> | null = null;
|
||
const loadGrid = () =>
|
||
(grid ??= import("./adam-dots").then(({ ADAM_DOTS, ADAM_H, ADAM_W }) => ({
|
||
w: ADAM_W,
|
||
h: ADAM_H,
|
||
levels: parse(ADAM_DOTS),
|
||
})));
|
||
|
||
const BackdropContext = createContext<Backdrop | null>(null);
|
||
|
||
/** Provides the custom picture from Settings → Appearance (null for the built-in painting). */
|
||
export function BackdropProvider({ backdrop, children }: { backdrop: Backdrop | null; children: React.ReactNode }) {
|
||
return <BackdropContext.Provider value={backdrop}>{children}</BackdropContext.Provider>;
|
||
}
|
||
|
||
/**
|
||
* The Creation of Adam as a dot matrix. `view` is the horizontal slice of the painting (0–1) that
|
||
* fills the box (at least covering it), placed so `focus` (the hands) lands at `anchor` (x, y as
|
||
* fractions of the box) as far as the painting's edges allow. Dots use the element's text color; bright parts of the painting get big dots in
|
||
* dark mode, dark parts in light mode, so it reads as the painting either way.
|
||
*
|
||
* With a custom picture (Settings → Appearance, or `source`), that picture covers the box instead,
|
||
* with its own focus point at `anchor`; `view` and `focus` only apply to the built-in painting.
|
||
*/
|
||
export function DotPainting({
|
||
className,
|
||
view = 0.7,
|
||
focus = [0.48, 0.42],
|
||
anchor = [0.68, 0.5],
|
||
source,
|
||
}: {
|
||
className?: string;
|
||
view?: number;
|
||
focus?: [number, number];
|
||
anchor?: [number, number];
|
||
/** Draw this picture instead of the one from the settings (the Appearance preview). */
|
||
source?: Backdrop | null;
|
||
}) {
|
||
const ref = useRef<HTMLCanvasElement>(null);
|
||
const fromSettings = useContext(BackdropContext);
|
||
const custom = source !== undefined ? source : fromSettings;
|
||
const customGrid = useMemo(() => (custom ? { w: custom.w, h: custom.h, levels: parse(custom.dots) } : null), [custom]);
|
||
|
||
const [fx, fy] = custom ? custom.focus : focus;
|
||
const [ax, ay] = anchor;
|
||
const v = custom ? 1 : view;
|
||
useEffect(() => {
|
||
const canvas = ref.current;
|
||
if (!canvas) return;
|
||
let data: Grid | null = customGrid;
|
||
let cancelled = false;
|
||
let frame = 0;
|
||
// What the canvas shows (size, pixel ratio, theme, color): a drawing takes tens of ms, so skip it
|
||
// when nothing changed, e.g. for the ResizeObserver's first callback or another class on <html>.
|
||
let drawn = "";
|
||
const draw = () => {
|
||
frame = 0;
|
||
if (!data) return;
|
||
const { width, height } = canvas.getBoundingClientRect();
|
||
if (!width || !height) return;
|
||
const dpr = window.devicePixelRatio || 1;
|
||
const color = getComputedStyle(canvas).color;
|
||
const dark = document.documentElement.classList.contains("dark");
|
||
const key = `${width}x${height}@${dpr} ${dark} ${color}`;
|
||
if (key === drawn) return;
|
||
drawn = key;
|
||
const { w, h, levels } = data;
|
||
canvas.width = Math.round(width * dpr);
|
||
canvas.height = Math.round(height * dpr);
|
||
const g = canvas.getContext("2d")!;
|
||
g.setTransform(dpr, 0, 0, dpr, 0, 0);
|
||
g.clearRect(0, 0, width, height);
|
||
g.fillStyle = color;
|
||
// CSS px per grid cell: `view` of the painting's width fills the box (at least covering it).
|
||
const scale = Math.max(width / (v * w), height / h);
|
||
const offX = ax * width - fx * w * scale;
|
||
const offY = ay * height - fy * h * scale;
|
||
const clampOff = (o: number, box: number, size: number) => Math.min(0, Math.max(box - size, o));
|
||
const ox = clampOff(offX, width, w * scale);
|
||
const oy = clampOff(offY, height, h * scale);
|
||
const maxR = SPACING * 0.48;
|
||
for (let y = SPACING / 2; y < height; y += SPACING) {
|
||
const iy = Math.min(h - 1, Math.max(0, Math.floor((y - oy) / scale)));
|
||
for (let x = SPACING / 2; x < width; x += SPACING) {
|
||
const ix = Math.min(w - 1, Math.max(0, Math.floor((x - ox) / scale)));
|
||
const level = levels[iy * w + ix] / 15;
|
||
const r = maxR * (dark ? level : 1 - level);
|
||
if (r < 0.35) continue;
|
||
g.beginPath();
|
||
g.arc(x, y, r, 0, Math.PI * 2);
|
||
g.fill();
|
||
}
|
||
}
|
||
};
|
||
// While a window is resized, observations arrive every frame: draw at most once per frame.
|
||
const schedule = () => {
|
||
if (!frame) frame = requestAnimationFrame(draw);
|
||
};
|
||
if (data) draw();
|
||
else
|
||
void loadGrid().then((g) => {
|
||
if (cancelled) return;
|
||
data = g;
|
||
draw();
|
||
});
|
||
const resize = new ResizeObserver(schedule);
|
||
resize.observe(canvas);
|
||
// next-themes switches the class on <html>.
|
||
const theme = new MutationObserver(schedule);
|
||
theme.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] });
|
||
return () => {
|
||
cancelled = true;
|
||
cancelAnimationFrame(frame);
|
||
resize.disconnect();
|
||
theme.disconnect();
|
||
};
|
||
}, [customGrid, v, fx, fy, ax, ay]);
|
||
|
||
return <canvas ref={ref} aria-hidden className={className} />;
|
||
}
|