opencode/packages/ui/script/generate-spinner.ts

121 lines
4.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { deflateSync } from "node:zlib"
import { radarDots, radarFps, radarFrameCount, radarOpacity } from "../src/components/spinner-radar"
// Bakes Soft Radar (src/components/spinner-radar.ts) into the masks `Spinner` paints, written to
// src/components/spinner-frames.css. Run `bun run generate:spinner` after changing the radar and commit the result.
//
// The frames are one APNG behind a CSS mask, so a spinner is one element with no CSS animation: the browser advances the
// image itself, every spinner showing it shares one loop (so they stay in phase without script), and none of them
// restyles on a main-thread frame. Keep it that way: see the note on `Spinner`.
// Each dot owns a 3×3 block of texels over the 15 units its cells span (the mask is inset half a unit), so even
// smoothed sampling never blends a neighbour's level into a dot: blending only happens across the gaps the grid cuts.
const cell = 3
const texels = 5 * cell
const dots = radarDots()
const crcTable = Array.from({ length: 256 }, (_, value) =>
Array.from({ length: 8 }).reduce<number>((crc) => ((crc & 1) !== 0 ? 0xedb88320 ^ (crc >>> 1) : crc >>> 1), value),
)
const rects = (fill: (dot: (typeof dots)[number]) => string) =>
dots
.map(
(dot) =>
`<rect x='${dot.x}' y='${dot.y}' width='${dot.size}' height='${dot.size}' rx='${dot.size * 0.24}'${fill(dot)}/>`,
)
.join("")
const svg = (body: string) =>
`data:image/svg+xml,${`<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'>${body}</svg>`.replace(/</g, "%3C").replace(/>/g, "%3E")}`
if (import.meta.main) await Bun.write(new URL("../src/components/spinner-frames.css", import.meta.url), spinnerCss())
/** The stylesheet this script writes to spinner-frames.css. */
export function spinnerCss() {
return [
"/* Generated by `bun run generate:spinner` (packages/ui/script/generate-spinner.ts) from spinner-radar.ts. Do not edit. */",
'[data-slot="spinner-dots"] {',
` --spinner-frames: url("data:image/png;base64,${apng().toString("base64")}");`,
` --spinner-grid: url("${svg(rects(() => ""))}");`,
` --spinner-still: url("${svg(rects((dot) => ` fill-opacity='${dot.still}'`))}");`,
"}",
"",
].join("\n")
}
function apng() {
const header = Buffer.alloc(13)
header.writeUInt32BE(texels, 0)
header.writeUInt32BE(texels, 4)
// 8-bit grey and alpha: the mask only reads alpha.
header[8] = 8
header[9] = 4
const animation = Buffer.alloc(8)
animation.writeUInt32BE(radarFrameCount, 0)
animation.writeUInt32BE(0, 4)
const frames = Array.from({ length: radarFrameCount }, (_, frame) => {
const control = Buffer.alloc(26)
// Sequence numbers count every fcTL and fdAT in file order: 0, then 1 and 2 for frame 1, and so on.
control.writeUInt32BE(frame === 0 ? 0 : frame * 2 - 1, 0)
control.writeUInt32BE(texels, 4)
control.writeUInt32BE(texels, 8)
control.writeUInt16BE(1, 20)
control.writeUInt16BE(radarFps, 22)
const data = deflateSync(pixels(frame / radarFrameCount), { level: 9 })
if (frame === 0) return [chunk("fcTL", control), chunk("IDAT", data)]
const sequenced = Buffer.alloc(data.length + 4)
sequenced.writeUInt32BE(frame * 2, 0)
data.copy(sequenced, 4)
return [chunk("fcTL", control), chunk("fdAT", sequenced)]
}).flat()
return Buffer.concat([
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
chunk("IHDR", header),
chunk("acTL", animation),
...frames,
chunk("IEND", Buffer.alloc(0)),
])
}
function pixels(phase: number) {
const stride = 1 + texels * 2
const rows = Buffer.alloc(stride * texels)
Array.from({ length: texels }, (_, y) =>
Array.from({ length: texels }, (_, x) => {
const dot = dots[Math.floor(y / cell) * 5 + Math.floor(x / cell)]!
const offset = y * stride + 1 + x * 2
rows[offset] = 255
rows[offset + 1] = Math.round(radarOpacity(dot, phase) * 255)
}),
)
return rows
}
function chunk(type: string, data: Buffer) {
const name = Buffer.from(type)
const result = Buffer.alloc(data.length + 12)
result.writeUInt32BE(data.length, 0)
name.copy(result, 4)
data.copy(result, 8)
result.writeUInt32BE(crc32(Buffer.concat([name, data])), data.length + 8)
return result
}
function crc32(data: Buffer) {
return (
(data.reduce<number>((crc, value) => crcTable[(crc ^ value) & 0xff]! ^ (crc >>> 8), 0xffffffff) ^ 0xffffffff) >>> 0
)
}