/ tutorials / wave-acoustics
Sign in
tutorials / wave-acoustics
wave-acoustics / scripts / figure-shots.mjs
35 lines · 1.8 KBBlameHistoryRaw
1#!/usr/bin/env node
2// Screenshots every static figure on its own, into tmp/figs/.
3//
4// npm run build && node scripts/figure-shots.mjs
5//
6// The full-page shots from browser-test.mjs --shots are too coarse to judge a
7// diagram by: text in these figures is placed by hand, so a label that fits at
8// one width collides at another, and the only way to know is to look at each
9// one at the size it is drawn.
10import { spawn } from 'node:child_process'
11import { mkdirSync } from 'node:fs'
12import { dirname, join } from 'node:path'
13import { fileURLToPath } from 'node:url'
14import puppeteer from 'puppeteer'
15const root = join(dirname(fileURLToPath(import.meta.url)), '..')
16const PORT = 5199
17const server = spawn('npx', ['vite', 'preview', '--port', String(PORT), '--strictPort'], { cwd: root, stdio: 'ignore', detached: true })
18const stop = () => { try { process.kill(-server.pid, 'SIGKILL') } catch {} }
19process.on('exit', stop)
20const URL = `http://localhost:${PORT}/`
21for (let i = 0; i < 120; i++) { try { const r = await fetch(URL); if (r.ok) break } catch {} await new Promise(r => setTimeout(r, 250)) }
22mkdirSync(`${root}/tmp/figs`, { recursive: true })
23const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox'] })
24const page = await browser.newPage()
25await page.setViewport({ width: 1000, height: 900 })
26const chapters = ['introduction','exact','leapfrog','waveguide','square','order','round','open','images','fdtd']
27let k = 0
28for (const c of chapters) {
29 await page.goto(`${URL}#/${c}`, { waitUntil: 'networkidle2' })
30 await new Promise(r => setTimeout(r, 700))
31 const figs = await page.$$('figure.static-figure')
32 for (const f of figs) { await f.screenshot({ path: `${root}/tmp/figs/${String(++k).padStart(2,'0')}-${c}.png` }) }
34await browser.close(); stop()
35console.log('done', k)
↑↓ move↵openescclose