Custom background picture for the dot matrix headers

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>
This commit is contained in:
Pascal LinxweilerandClaude Opus 5.5 committed 2026-11-02 20:13:48 +01:00
1 parent 422e46bf65
commit 02756ddf90
11 files changed
+309 -18

No files matched your search

@@ -9,6 +9,7 @@ import {
LibraryIcon,
MonitorIcon,
MoonIcon,
PaletteIcon,
PlusIcon,
ScanSearchIcon,
SearchIcon,
@@ -98,6 +99,7 @@ export function CommandPaletteDialog({ open, setOpen }: { open: boolean; setOpen
{ id: "s:speed", group: "Settings", title: "Download speed limit", icon: DownloadIcon, hint: "Downloads", keywords: "bandwidth mb/s", run: go("/settings/downloads") },
{ id: "s:recommended", group: "Settings", title: "Recommended files", icon: DownloadIcon, hint: "Downloads", keywords: "mmproj mtp template quant", run: go("/settings/downloads") },
{ id: "s:scan", group: "Settings", title: "Models folder scan schedule", icon: ScanSearchIcon, hint: "Downloads", keywords: "daily weekly monthly nas", run: go("/settings/downloads#scan") },
{ id: "s:backdrop", group: "Settings", title: "Background picture", icon: PaletteIcon, hint: "Appearance", keywords: "image upload dot matrix painting header wallpaper", run: go("/settings/appearance") },
];
for (const m of index?.models ?? []) {
out.push({ id: `m:${m.id}`, group: "Models", title: m.repoId, hint: m.quants.join(", ") || m.status, icon: BoxIcon, keywords: `${m.status} ${m.tags.join(" ")}`, run: go(`/${m.repoId}`) });
+34 -13
View File
@@ -1,10 +1,12 @@
"use client";
import { useEffect, useRef } from "react";
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.
@@ -13,34 +15,51 @@ const loadGrid = () =>
(grid ??= import("./adam-dots").then(({ ADAM_DOTS, ADAM_H, ADAM_W }) => ({
w: ADAM_W,
h: ADAM_H,
levels: Uint8Array.from(ADAM_DOTS, (c) => parseInt(c, 16)),
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] = focus;
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 = null;
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
@@ -65,7 +84,7 @@ export function DotPainting({
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 / (view * w), height / h);
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));
@@ -76,8 +95,8 @@ export function DotPainting({
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 v = levels[iy * w + ix] / 15;
const r = maxR * (dark ? v : 1 - v);
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);
@@ -89,11 +108,13 @@ export function DotPainting({
const schedule = () => {
if (!frame) frame = requestAnimationFrame(draw);
};
void loadGrid().then((g) => {
if (cancelled) return;
data = g;
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>.
@@ -105,7 +126,7 @@ export function DotPainting({
resize.disconnect();
theme.disconnect();
};
}, [view, fx, fy, ax, ay]);
}, [customGrid, v, fx, fy, ax, ay]);
return <canvas ref={ref} aria-hidden className={className} />;
}