文件
fenleixinxi/uniapp/tools/gen-tab-category.mjs
MiAn c2e0f72b30 feat: 新增分类页面与规范化顶部导航栏组件
新增分类页面及图标资源,完善底部导航分类 tab,新增 navbar 度量工具适配不同设备,优化请求参数处理,修复通知相关显示问题,统一顶部导航栏样式以适配系统状态栏与胶囊避让。
2026-10-20 11:02:42 +08:00

120 行
3.5 KiB
JavaScript

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

/**
* 生成底部导航「分类」Tab 图标(常态与选中态)
* 采用 192x192 真实透明通道 PNG,带平滑抗锯齿
*/
import fs from 'node:fs'
import path from 'node:path'
import url from 'node:url'
import { encodePng } from './png-util.mjs'
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
const OUT_DIR = path.resolve(__dirname, '..', 'static', 'icons')
/**
* 16进制颜色解析为 RGBA 数组
* @param {string} hex 16进制颜色,如 #04874A
* @param {number} a 透明度 0~1
* @returns {number[]} [r, g, b, a255]
*/
function parseHex(hex, a = 1) {
const clean = hex.replace('#', '')
const r = parseInt(clean.substring(0, 2), 16)
const g = parseInt(clean.substring(2, 4), 16)
const b = parseInt(clean.substring(4, 6), 16)
return [r, g, b, Math.round(a * 255)]
}
/**
* 圆角矩形的带符号距离场(SDF)
* @param {number} px 当前像素 x
* @param {number} py 当前像素 y
* @param {number} cx 矩形中心 x
* @param {number} cy 矩形中心 y
* @param {number} hw 半宽
* @param {number} hh 半高
* @param {number} r 圆角半径
* @returns {number} 距离(负数在内部,正数在外部)
*/
function sdRoundRect(px, py, cx, cy, hw, hh, r) {
const dx = Math.abs(px - cx) - (hw - r)
const dy = Math.abs(py - cy) - (hh - r)
const ax = Math.max(dx, 0)
const ay = Math.max(dy, 0)
const outsideDist = Math.hypot(ax, ay)
const insideDist = Math.min(Math.max(dx, dy), 0)
return outsideDist + insideDist - r
}
/**
* 绘制分类 4 宫格方块图标
* @param {string} c1 主色(左上、右上、左下)
* @param {string} c2 强调色(右下)
* @returns {Buffer} PNG 二进制 Buffer
*/
function renderCategoryIcon(c1, c2) {
const W = 192
const H = 192
const rgba = Buffer.alloc(W * H * 4, 0)
const col1 = parseHex(c1)
const col2 = parseHex(c2)
// 4 个方块的中心与规格
const boxes = [
{ cx: 56, cy: 56, hw: 30, hh: 30, r: 10, col: col1 }, // 左上
{ cx: 136, cy: 56, hw: 30, hh: 30, r: 10, col: col1 }, // 右上
{ cx: 56, cy: 136, hw: 30, hh: 30, r: 10, col: col1 }, // 左下
{ cx: 136, cy: 136, hw: 30, hh: 30, r: 10, col: col2 }, // 右下
]
for (let y = 0; y < H; y++) {
for (let x = 0; x < W; x++) {
let finalA = 0
let finalR = 0
let finalG = 0
let finalB = 0
for (const box of boxes) {
const d = sdRoundRect(x + 0.5, y + 0.5, box.cx, box.cy, box.hw, box.hh, box.r)
// 抗锯齿平滑过渡 [-0.75, 0.75]
let cov = 0
if (d < -0.75) cov = 1
else if (d > 0.75) cov = 0
else cov = (0.75 - d) / 1.5
if (cov > 0) {
finalR = box.col[0]
finalG = box.col[1]
finalB = box.col[2]
finalA = Math.max(finalA, Math.round(cov * 255))
}
}
const idx = (y * W + x) * 4
rgba[idx] = finalR
rgba[idx + 1] = finalG
rgba[idx + 2] = finalB
rgba[idx + 3] = finalA
}
}
return encodePng({ width: W, height: H, rgba })
}
/**
* 主执行函数:输出常态与选中态图标
*/
function main() {
// 常态:灰绿 #7C9A8B / #B4CDC0
const offBuf = renderCategoryIcon('#7C9A8B', '#B4CDC0')
fs.writeFileSync(path.join(OUT_DIR, 'tab-category.png'), offBuf)
console.log('✅ 生成 tab-category.png 完成')
// 选中态:品牌春绿 #04874A / #A9F2CE
const onBuf = renderCategoryIcon('#04874A', '#A9F2CE')
fs.writeFileSync(path.join(OUT_DIR, 'tab-category-on.png'), onBuf)
console.log('✅ 生成 tab-category-on.png 完成')
}
main()