Client bundles: no drizzle in the browser, lazy palette, one copy of the painting
- The enum arrays move from schema.ts to src/lib/db/enums.ts (no imports), which schema.ts re-exports. The endpoint form and benchmark start imported ENDPOINT_ENGINES/QUALITY_SECTIONS from the schema, which put drizzle-orm, every table definition and ulid (18 KB gzipped) on the endpoint pages. An ESLint rule now refuses value imports from the schema in client code. - The ⌘K palette dialog is its own chunk, loaded on first open and preloaded when you hover or focus Search or press ⌘/Ctrl. - The dot painting's brightness grid was copied into 10 page chunks (~10 KB gzipped each); it is now one async chunk, parsed once per tab. The canvas skips redraws when size, pixel ratio and theme are unchanged (the ResizeObserver's first callback drew everything a second time) and draws at most once per frame while resizing. First-load JS per route drops by 10-29 KB gzipped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EooZ9biu7kFNB4eDJrwQc5
This commit is contained in:
9 files changed
+458
-339
No files matched your search
@@ -1,10 +1,20 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef } from "react";
|
||||
import { ADAM_DOTS, ADAM_H, ADAM_W } from "./adam-dots";
|
||||
|
||||
const SPACING = 6; // CSS px between dot centres
|
||||
const levels = Uint8Array.from(ADAM_DOTS, (c) => parseInt(c, 16));
|
||||
|
||||
type Grid = { w: number; h: number; levels: Uint8Array };
|
||||
|
||||
// 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: Uint8Array.from(ADAM_DOTS, (c) => parseInt(c, 16)),
|
||||
})));
|
||||
|
||||
/**
|
||||
* The Creation of Adam as a dot matrix. `view` is the horizontal slice of the painting (0–1) that
|
||||
@@ -30,30 +40,43 @@ export function DotPainting({
|
||||
useEffect(() => {
|
||||
const canvas = ref.current;
|
||||
if (!canvas) return;
|
||||
let data: Grid | null = null;
|
||||
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 = getComputedStyle(canvas).color;
|
||||
const dark = document.documentElement.classList.contains("dark");
|
||||
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 * ADAM_W), height / ADAM_H);
|
||||
const offX = ax * width - fx * ADAM_W * scale;
|
||||
const offY = ay * height - fy * ADAM_H * scale;
|
||||
const scale = Math.max(width / (view * 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, ADAM_W * scale);
|
||||
const oy = clampOff(offY, height, ADAM_H * scale);
|
||||
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(ADAM_H - 1, Math.max(0, Math.floor((y - oy) / scale)));
|
||||
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(ADAM_W - 1, Math.max(0, Math.floor((x - ox) / scale)));
|
||||
const v = levels[iy * ADAM_W + ix] / 15;
|
||||
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);
|
||||
if (r < 0.35) continue;
|
||||
g.beginPath();
|
||||
@@ -62,13 +85,23 @@ export function DotPainting({
|
||||
}
|
||||
}
|
||||
};
|
||||
draw();
|
||||
const resize = new ResizeObserver(draw);
|
||||
// While a window is resized, observations arrive every frame: draw at most once per frame.
|
||||
const schedule = () => {
|
||||
if (!frame) frame = requestAnimationFrame(draw);
|
||||
};
|
||||
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(draw);
|
||||
const theme = new MutationObserver(schedule);
|
||||
theme.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] });
|
||||
return () => {
|
||||
cancelled = true;
|
||||
cancelAnimationFrame(frame);
|
||||
resize.disconnect();
|
||||
theme.disconnect();
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user