/** * Render frames of a meditation to PNG without a browser, for checking timing and art. * npm run snapshot -- northern-cabin 0 120 200 280 * Output goes to snapshots/-.png at 3x scale. */ import { mkdirSync, writeFileSync } from 'node:fs'; import { crc32, deflateSync } from 'node:zlib'; import { Surface } from '../src/engine/Surface'; import { findMeditation, meditations } from '../src/meditations/registry'; const SCALE = 3; function png(w: number, h: number, rgba: Uint8ClampedArray): Buffer { const raw = Buffer.alloc((w * 4 + 1) * h); for (let y = 0; y < h; y++) { raw[y * (w * 4 + 1)] = 0; Buffer.from(rgba.buffer, rgba.byteOffset + y * w * 4, w * 4).copy(raw, y * (w * 4 + 1) + 1); } const chunk = (type: string, data: Buffer) => { const len = Buffer.alloc(4); len.writeUInt32BE(data.length); const td = Buffer.concat([Buffer.from(type), data]); const crc = Buffer.alloc(4); crc.writeUInt32BE(crc32(td)); return Buffer.concat([len, td, crc]); }; const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(w, 0); ihdr.writeUInt32BE(h, 4); ihdr[8] = 8; ihdr[9] = 6; return Buffer.concat([ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk('IHDR', ihdr), chunk('IDAT', deflateSync(raw)), chunk('IEND', Buffer.alloc(0)), ]); } const [id, ...times] = process.argv.slice(2); const m = findMeditation(id ?? ''); if (!m) { console.error(`Usage: npm run snapshot -- \nKnown ids: ${meditations.map((x) => x.id).join(', ')}`); process.exit(1); } const scene = m.createScene(); const s = new Surface(320, 180); mkdirSync('snapshots', { recursive: true }); for (const t of times.length ? times.map(Number) : [m.previewTime]) { scene.render(s, t); const big = new Uint8ClampedArray(s.w * SCALE * s.h * SCALE * 4); for (let y = 0; y < s.h * SCALE; y++) { for (let x = 0; x < s.w * SCALE; x++) { const si = (((y / SCALE) | 0) * s.w + ((x / SCALE) | 0)) * 4; big.set(s.data.subarray(si, si + 4), (y * s.w * SCALE + x) * 4); } } const file = `snapshots/${m.id}-${String(t).padStart(3, '0')}.png`; writeFileSync(file, png(s.w * SCALE, s.h * SCALE, big)); console.log(file); }