-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.js
More file actions
52 lines (42 loc) · 1.86 KB
/
generate.js
File metadata and controls
52 lines (42 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import puppeteer from 'puppeteer';
import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const dir = path.dirname(fileURLToPath(import.meta.url));
// Parse CLI args: --output logo.png --width 1920 --height 1080 --timeout 10000
const args = process.argv.slice(2);
const get = (flag, def) => { const i = args.indexOf(flag); return i !== -1 ? args[i + 1] : def; };
const output = get('--output', 'logo.png');
const width = parseInt(get('--width', '1920'));
const height = parseInt(get('--height', '1080'));
const timeout = parseInt(get('--timeout', '10000'));
// Minimal static file server
const MIME = { '.html': 'text/html', '.js': 'application/javascript', '.json': 'application/json', '.png': 'image/png' };
const server = http.createServer((req, res) => {
const file = path.join(dir, req.url === '/' ? 'index.html' : req.url);
fs.readFile(file, (err, data) => {
if (err) { res.writeHead(404); res.end(); return; }
res.writeHead(200, { 'Content-Type': MIME[path.extname(file)] ?? 'application/octet-stream' });
res.end(data);
});
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
const { port } = server.address();
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--use-gl=swiftshader', // software WebGL — no GPU needed
'--ignore-gpu-blocklist',
'--enable-webgl',
],
});
const page = await browser.newPage();
await page.setViewport({ width, height, deviceScaleFactor: 1 });
await page.goto(`http://127.0.0.1:${port}/`);
await page.waitForFunction('window.sceneReady === true', { timeout });
await page.screenshot({ path: output, omitBackground: true });
console.log(`Saved ${output} (${width}x${height})`);
await browser.close();
server.close();