-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #942 from ZeLonewolf/zlw-render-samples
Script for generating render samples
- Loading branch information
Showing
6 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ dist | |
download | ||
local.config.js | ||
shieldlib/docs | ||
samples/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import fs from "node:fs"; | ||
import { chromium } from "@playwright/test"; | ||
|
||
// Declare a global augmentation for the Window interface | ||
declare global { | ||
interface Window { | ||
map?: { | ||
loaded: () => boolean; | ||
}; | ||
} | ||
} | ||
|
||
type LocationClip = { | ||
x: number; | ||
y: number; | ||
width: number; | ||
height: number; | ||
}; | ||
|
||
type SampleSpecification = { | ||
location: string; | ||
name: string; | ||
viewport: { | ||
width: number; | ||
height: number; | ||
}; | ||
clip: LocationClip; | ||
}; | ||
|
||
// Load list of locations to take map screenshots | ||
const loadSampleLocations = (filePath: string): SampleSpecification[] => { | ||
const rawData = fs.readFileSync(filePath, "utf8"); | ||
return JSON.parse(rawData); | ||
}; | ||
|
||
const sampleFolder = "./samples"; | ||
|
||
const jsonSampleLocations = process.argv[2] ?? "test/sample_locations.json"; | ||
|
||
console.log(`Loading sample locations from ${jsonSampleLocations}`); | ||
|
||
const screenshots: SampleSpecification[] = | ||
loadSampleLocations(jsonSampleLocations); | ||
|
||
fs.mkdirSync(sampleFolder, { recursive: true }); | ||
|
||
const browser = await chromium.launch({ | ||
headless: true, | ||
executablePath: process.env.CHROME_BIN, | ||
}); | ||
const context = await browser.newContext(); | ||
|
||
const page = await context.newPage(); | ||
|
||
for (const screenshot of screenshots) { | ||
await page.setViewportSize(screenshot.viewport); | ||
await createImage(screenshot); | ||
} | ||
|
||
async function createImage(screenshot: SampleSpecification) { | ||
await page.goto(`http://localhost:1776/#map=${screenshot.location}`); | ||
|
||
// Wait for map to load, then wait two more seconds for images, etc. to load. | ||
try { | ||
await page.waitForFunction(() => window.map?.loaded(), { | ||
timeout: 3000, | ||
}); | ||
|
||
// Wait for 1.5 seconds on 3D model examples, since this takes longer to load. | ||
const waitTime = 1500; | ||
console.log(`waiting for ${waitTime} ms`); | ||
await page.waitForTimeout(waitTime); | ||
} catch (e) { | ||
console.log(`Timed out waiting for map load`); | ||
} | ||
|
||
try { | ||
await page.screenshot({ | ||
path: `${sampleFolder}/${screenshot.name}.png`, | ||
type: "png", | ||
clip: screenshot.clip, | ||
}); | ||
console.log(`Created ${sampleFolder}/${screenshot.name}.png`); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
|
||
await browser.close(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
[ | ||
{ | ||
"location": "4/40.5/-94", | ||
"name": "full_window", | ||
"viewport": { | ||
"width": 600, | ||
"height": 250 | ||
}, | ||
"clip": { | ||
"x": 0, | ||
"y": 0, | ||
"width": 600, | ||
"height": 250 | ||
} | ||
}, | ||
{ | ||
"location": "13/40.01264/-75.70446", | ||
"name": "downingtown_alt_trk_bus", | ||
"viewport": { | ||
"width": 600, | ||
"height": 600 | ||
}, | ||
"clip": { | ||
"x": 100, | ||
"y": 100, | ||
"width": 400, | ||
"height": 400 | ||
} | ||
}, | ||
{ | ||
"location": "13/35.22439/-99.99495", | ||
"name": "historic_us66_oklahoma", | ||
"viewport": { | ||
"width": 600, | ||
"height": 600 | ||
}, | ||
"clip": { | ||
"x": 100, | ||
"y": 100, | ||
"width": 400, | ||
"height": 400 | ||
} | ||
} | ||
] |