仰望星辰工作室

starmusic-mobile

starmusic-mobile/ scripts/gen-tabbar-icons.js 4.3 KB · 138 行 原始文件
1// 一次性脚本:生成 tabBar 图标 PNG(搜索/我的/设置 × 未选中/选中)
2// 输出到 static/tabbar/ 目录。运行:node scripts/gen-tabbar-icons.js
3const fs = require('fs')
4const path = require('path')
5const zlib = require('zlib')
6
7const SIZE = 81 // uni-app 官方推荐 81x81
8const SS = 4 // 4x4 超采样抗锯齿
9
10// ---- PNG 编码(最小实现)----
11const CRC_TABLE = (() => {
12 const t = new Int32Array(256)
13 for (let n = 0; n < 256; n++) {
14 let c = n
15 for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
16 t[n] = c
17 }
18 return t
19})()
20const crc32 = (buf) => {
21 let c = 0xffffffff
22 for (let i = 0; i < buf.length; i++) c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8)
23 return (c ^ 0xffffffff) >>> 0
24}
25const chunk = (type, data) => {
26 const len = Buffer.alloc(4)
27 len.writeUInt32BE(data.length)
28 const body = Buffer.concat([Buffer.from(type), data])
29 const crc = Buffer.alloc(4)
30 crc.writeUInt32BE(crc32(body))
31 return Buffer.concat([len, body, crc])
32}
33const encodePng = (width, height, rgba) => {
34 const ihdr = Buffer.alloc(13)
35 ihdr.writeUInt32BE(width, 0)
36 ihdr.writeUInt32BE(height, 4)
37 ihdr[8] = 8 // bit depth
38 ihdr[9] = 6 // color type RGBA
39 const raw = Buffer.alloc((width * 4 + 1) * height)
40 for (let y = 0; y < height; y++) {
41 raw[y * (width * 4 + 1)] = 0 // filter: none
42 rgba.copy(raw, y * (width * 4 + 1) + 1, y * width * 4, (y + 1) * width * 4)
43 }
44 return Buffer.concat([
45 Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
46 chunk('IHDR', ihdr),
47 chunk('IDAT', zlib.deflateSync(raw)),
48 chunk('IEND', Buffer.alloc(0)),
49 ])
50}
51
52// ---- 形状距离函数(返回值 < 0 在形状内)----
53const circle = (x, y, cx, cy, r) => Math.hypot(x - cx, y - cy) - r
54const ring = (x, y, cx, cy, r, w) => Math.abs(Math.hypot(x - cx, y - cy) - r) - w / 2
55const segment = (x, y, ax, ay, bx, by, w) => {
56 const dx = bx - ax
57 const dy = by - ay
58 const t = Math.max(0, Math.min(1, ((x - ax) * dx + (y - ay) * dy) / (dx * dx + dy * dy)))
59 return Math.hypot(x - (ax + t * dx), y - (ay + t * dy)) - w / 2
60}
61const heart = (x, y) => {
62 // 经典心形曲线,映射到 [0,1] 区间
63 const nx = (x / (SIZE - 1)) * 2 - 1 // -1..1
64 const ny = (y / (SIZE - 1)) * 2 - 1
65 const X = nx
66 const Y = ny * 1.2 - 0.15
67 return Math.pow(X * X + Y * Y - 1, 3) - X * X * Y * Y * Y
68}
69const gear = (x, y) => {
70 const cx = SIZE / 2
71 const cy = SIZE / 2
72 const R = 26 // 外齿半径
73 const r = 19 // 齿根半径
74 const hole = 8 // 中心孔
75 const d = Math.hypot(x - cx, y - cy)
76 if (d < hole) return 1
77 if (d > R) return 1
78 // 8 个齿:角度每 45° 一个齿
79 const angle = (Math.atan2(y - cy, x - cx) * 180) / Math.PI
80 const a = ((angle % 45) + 45) % 45
81 const tooth = a < 22.5 && d > r ? -1 : 1
82 return tooth
83}
84
85// ---- 图标定义:返回像素 (x,y) -> 是否着色 ----
86const draw = {
87 search: (x, y) => {
88 const cx = 32
89 const cy = 32
90 const d = Math.min(ring(x, y, cx, cy, 16, 6), segment(x, y, 45, 45, 62, 62, 7))
91 return d <= 0
92 },
93 my: (x, y) => heart(x, y) <= 0,
94 setting: (x, y) => gear(x, y) <= 0,
95}
96
97// ---- 渲染(4x4 超采样)----
98const render = (iconName, rgb) => {
99 const px = Buffer.alloc(SIZE * SIZE * 4)
100 for (let y = 0; y < SIZE; y++) {
101 for (let x = 0; x < SIZE; x++) {
102 let hit = 0
103 for (let sy = 0; sy < SS; sy++) {
104 for (let sx = 0; sx < SS; sx++) {
105 const px_ = x + (sx + 0.5) / SS
106 const py_ = y + (sy + 0.5) / SS
107 if (draw[iconName](px_, py_)) hit++
108 }
109 }
110 const alpha = Math.round((hit / (SS * SS)) * 255)
111 const i = (y * SIZE + x) * 4
112 px[i] = rgb[0]
113 px[i + 1] = rgb[1]
114 px[i + 2] = rgb[2]
115 px[i + 3] = alpha
116 }
117 }
118 return encodePng(SIZE, SIZE, px)
119}
120
121const GRAY = [144, 147, 153] // #909399
122const GREEN = [49, 194, 124] // #31c27c
123
124const outDir = path.join(__dirname, '..', 'static', 'tabbar')
125fs.mkdirSync(outDir, { recursive: true })
126
127const icons = [
128 ['search', GRAY, 'search.png'],
129 ['search', GREEN, 'search-active.png'],
130 ['my', GRAY, 'my.png'],
131 ['my', GREEN, 'my-active.png'],
132 ['setting', GRAY, 'setting.png'],
133 ['setting', GREEN, 'setting-active.png'],
134]
135for (const [name, rgb, file] of icons) {
136 fs.writeFileSync(path.join(outDir, file), render(name, rgb))
137 console.log('generated', file)
138}