Something went wrong. Try again.
doll.systems community website
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import { hasJapanese } from '../lib/site';import type { PageScope } from './page-scope';// Keep bitmap glyphs, icons, and canvas surfaces on the same CSS-pixel grid.export const cssValue = (name: string) => getComputedStyle(document.documentElement).getPropertyValue(name).trim();export const cssNumber = (name: string) => Number.parseFloat(cssValue(name));export const pixelSize = cssNumber('--display-pixel');export const snapPixel = (value: number) => Math.round(value / pixelSize) * pixelSize;const fontSize = cssNumber('--pixel-em');const japaneseFontSize = cssNumber('--japanese-pixel-size');export const glyphGrid = { width: Math.ceil((fontSize * 8) / 11), height: fontSize + 2, fontSize, baseline: fontSize - 1,} as const;export const labelGrid = (text: string) => { const size = hasJapanese(text) ? japaneseFontSize : glyphGrid.fontSize; return { font: `${size}px "Departure Mono", "DotGothic16", monospace`, height: size + 6, baseline: size + 1, };};
let align: (element: HTMLElement | SVGElement) => void = () => {};export const alignToPixelGrid = (element: HTMLElement | SVGElement) => align(element);export function mountPixelGrid(scope: PageScope) { type AlignedElement = { element: HTMLElement | SVGElement; x: number; y: number }; const aligned = new Map<Element, AlignedElement>(); let frame = 0; function scheduleAlignment() { if (frame) return; frame = requestAnimationFrame(() => { frame = 0; // Batch geometry reads before writes; scrolling must not cause layout thrashing. const offsets = [...aligned.values()].map((item) => { const rect = item.element.getBoundingClientRect(); const left = rect.left - item.x, top = rect.top - item.y; return { item, x: snapPixel(left) - left, y: snapPixel(top) - top }; }); for (const { item, x, y } of offsets) { if (x === item.x && y === item.y) continue; item.x = x; item.y = y; item.element.style.translate = `${x}px ${y}px`; } }); } const observer = scope.observe(new ResizeObserver(scheduleAlignment)); align = (element: HTMLElement | SVGElement) => { if (aligned.has(element)) return; aligned.set(element, { element, x: 0, y: 0 }); observer.observe(element); scheduleAlignment(); }; scope.defer(() => { cancelAnimationFrame(frame); aligned.clear(); align = () => {}; }); scope.on(window, 'resize', scheduleAlignment, { passive: true }); scope.on(window, 'scroll', scheduleAlignment, { passive: true, capture: true }); document.fonts.ready.then(() => { if (scope.signal.aborted) return; document .querySelectorAll<HTMLElement>('a, button, span, time, h2, p, div') .forEach((element) => { const hasText = [...element.childNodes].some( (node) => node.nodeType === Node.TEXT_NODE && node.textContent?.trim(), ); if (hasText && getComputedStyle(element).fontFamily.includes('Departure Mono')) alignToPixelGrid(element); }); document.querySelectorAll<HTMLElement>('.pixel-icon, .site-mark').forEach(alignToPixelGrid); scheduleAlignment(); });}