-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathicongenerator5000.ts
58 lines (45 loc) · 1.53 KB
/
icongenerator5000.ts
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
53
54
55
56
57
58
const puppeteer = require('puppeteer');
const fs = require('fs');
const urlRoot = 'http://localhost:5173';
/*
Converts a url where there is an element with an id #icon-screenshot
into an image.
*/
export async function convertUrlToIcon(
urlPath: string,
outputDir: string,
outputFilename: string,
quality: number = 90
): Promise<void> {
return new Promise(async (resolve, reject) => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const url = `${urlRoot}/${urlPath}`;
const targetFile = `${outputDir}/${outputFilename}.jpg`;
await page.goto(url);
console.info(
`Puppeteer is visiting ${url}, writing result to ${targetFile} with a quality of ${quality}.`
);
// Set the viewport size to ensure the element is visible
await page.setViewport({ width: 1280, height: 800 });
// Wait for the element to be fully loaded and visible
await page.waitForSelector('#icon-screenshot', { visible: true });
// Add a short delay to ensure any animations or transitions have finished
await page.waitForTimeout(1000);
// Select the element to capture
const element = await page.$('#icon-screenshot');
// Get the element's bounding box
const boundingBox = await element?.boundingBox();
// Take a screenshot of the element
const screenshot = await page.screenshot({
type: 'jpeg',
quality,
clip: boundingBox
});
fs.mkdirSync(outputDir, { recursive: true });
// Save the screenshot to a file
fs.writeFileSync(targetFile, screenshot);
await browser.close();
resolve();
});
}