screenshot.js (2402B)
1 const fs = require('fs'); 2 const path = require('path'); 3 const { chromium } = require('playwright'); 4 const serve = require('../serve.js'); 5 6 async function save(page, filePath) { 7 await page.screenshot({ path: filePath, fullPage: false }); 8 console.log(filePath); 9 } 10 11 async function main() { 12 const outDir = path.join(__dirname, '..', 'screenshots'); 13 fs.mkdirSync(outDir, { recursive: true }); 14 15 const { server, url } = await serve(8767); 16 const browser = await chromium.launch(); 17 18 try { 19 const fireTruckPage = async () => { 20 const page = await browser.newPage({ viewport: { width: 1280, height: 800 } }); 21 await page.addInitScript(() => { 22 window.__TEST_MODE__ = true; 23 window.GAME_SPEED_MULTIPLIER = 0; 24 }); 25 await page.goto(url + '/games/fire-truck/', { waitUntil: 'domcontentloaded' }); 26 await page.waitForFunction(() => window.__FT_SCENE__ && window.__FT_SCENE__.island); 27 return page; 28 }; 29 30 const overview = await fireTruckPage(); 31 await overview.evaluate(() => { 32 const s = window.__FT_SCENE__; 33 s.cameras.main.stopFollow(); 34 s.cameras.main.setZoom(0.05); 35 s.cameras.main.centerOn((s.island.width - 1) / 2 * s.cellSize, (s.island.height - 1) / 2 * s.cellSize); 36 }); 37 await save(overview, path.join(outDir, 'fire-truck-overview.png')); 38 await overview.close(); 39 40 const gameplay = await fireTruckPage(); 41 await save(gameplay, path.join(outDir, 'fire-truck-gameplay.png')); 42 await gameplay.close(); 43 44 const intersection = await fireTruckPage(); 45 await intersection.evaluate(() => { 46 const s = window.__FT_SCENE__; 47 const index = s.route.findIndex(step => s.cellAt(step.x, step.y).meta.kind === 'four'); 48 if (index < 0) throw new Error('No 4-way route step found'); 49 s.snapTruckToRouteIndex(index); 50 s.cameras.main.stopFollow(); 51 s.cameras.main.centerOn(s.truck.x, s.truck.y); 52 }); 53 await save(intersection, path.join(outDir, 'fire-truck-intersection.png')); 54 await intersection.close(); 55 56 const portal = await browser.newPage({ viewport: { width: 1280, height: 800 } }); 57 await portal.goto(url + '/', { waitUntil: 'domcontentloaded' }); 58 await save(portal, path.join(outDir, 'portal.png')); 59 await portal.close(); 60 } finally { 61 await browser.close(); 62 server.close(); 63 } 64 } 65 66 main().catch(e => { console.error(e); process.exit(1); });