Files
cube/apps/music/frontend/scripts/gen-icons.mjs
T
Fam Zheng 0b87085ca9
deploy music / build-and-deploy (push) Successful in 2m3s
music(pwa): 自家 icon — 黑底圆角 + 金黄八分音符
2026-05-27 17:37:24 +01:00

40 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
// 一次性脚本:把 icon-source.svg 渲染成 PWA 所需的各尺寸 PNG。
// 用法: node scripts/gen-icons.mjs (需先 npm i -D sharp
import sharp from 'sharp'
import { readFileSync, writeFileSync } from 'fs'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const src = readFileSync(resolve(__dirname, 'icon-source.svg'))
const pub = resolve(__dirname, '..', 'public')
async function render(out, size) {
await sharp(src).resize(size, size).png().toFile(resolve(pub, out))
console.log('wrote', out)
}
// Maskable 版本:留 ~10% safe-zone padding,避免 Android 圆形遮罩切到音符
async function renderMaskable(out, size) {
const pad = Math.round(size * 0.1)
const inner = size - pad * 2
const innerBuf = await sharp(src).resize(inner, inner).png().toBuffer()
await sharp({
create: { width: size, height: size, channels: 4, background: '#0f0f0f' },
})
.composite([{ input: innerBuf, top: pad, left: pad }])
.png()
.toFile(resolve(pub, out))
console.log('wrote', out)
}
await render('pwa-192x192.png', 192)
await render('pwa-512x512.png', 512)
await render('apple-touch-icon-180x180.png', 180)
await render('favicon-48x48.png', 48)
await renderMaskable('maskable-icon-512x512.png', 512)
console.log('done')