Graphley / website / tools / render-icons.js
| 1 | // Renders mark.svg (the Graphley app icon) to the site's PNG icons. |
| 2 | // Run from the repository root: node tools/render-icons.js [out-dir-for-sibling-icons] |
| 3 | // Writes assets/icon.png (512, rounded), favicon.png (64, rounded), assets/apple-touch-icon.png |
| 4 | // (180, square; iOS rounds it). With an out dir it also writes graphley-96.png and graphley-144.png |
| 5 | //. |
| 6 | const fs = require('fs'); |
| 7 | const path = require('path'); |
| 8 | const { chromium } = require('playwright'); |
| 9 | const root = path.resolve(__dirname, '..'); |
| 10 | const svg = fs.readFileSync(path.join(root, 'mark.svg'), 'utf8'); |
| 11 | const rounded = (size) => `<html><body style="margin:0;background:transparent"> |
| 12 | <div style="width:${size}px;height:${size}px;display:grid;place-items:center"> |
| 13 | <div style="width:${size * 0.92}px;height:${size * 0.92}px;border-radius:${size * 0.92 * 0.225}px;overflow:hidden;box-shadow:0 ${size * 0.01}px ${size * 0.02}px rgba(11,27,33,.28)"> |
| 14 | ${svg.replace('<svg ', '<svg style="display:block;width:100%;height:100%" ')} |
| 15 | </div></div></body></html>`; |
| 16 | const square = (size) => `<html><body style="margin:0">${svg.replace('<svg ', `<svg style="display:block;width:${size}px;height:${size}px" `)}</body></html>`; |
| 17 | (async () => { |
| 18 | const browser = await chromium.launch(process.env.PW_CHROME ? { executablePath: process.env.PW_CHROME } : {}); |
| 19 | const shot = async (html, size, file) => { |
| 20 | const page = await browser.newPage({ viewport: { width: size, height: size } }); |
| 21 | await page.setContent(html); |
| 22 | await page.screenshot({ path: file, omitBackground: true }); |
| 23 | await page.close(); |
| 24 | }; |
| 25 | await shot(rounded(512), 512, path.join(root, 'assets/icon.png')); |
| 26 | await shot(rounded(64), 64, path.join(root, 'favicon.png')); |
| 27 | await shot(square(180), 180, path.join(root, 'assets/apple-touch-icon.png')); |
| 28 | const out = process.argv[2]; |
| 29 | if (out) { |
| 30 | await shot(rounded(96), 96, path.join(out, 'graphley-96.png')); |
| 31 | await shot(rounded(144), 144, path.join(out, 'graphley-144.png')); |
| 32 | } |
| 33 | await browser.close(); |
| 34 | })(); |