/** * 生成 uni-app 原生 tabBar 图标(81x81 PNG,RGBA) * 输出: * src/static/icon/home.png 首页-未选中 #555555 * src/static/icon/home_a.png 首页-选中 #24afd6 * src/static/icon/apps.png 应用-未选中 #555555 * src/static/icon/apps_a.png 应用-选中 #24afd6 * * 用法:node scripts/gen-tabbar-icons.cjs */ const fs = require('fs'); const path = require('path'); const zlib = require('zlib'); const SIZE = 81; const SS = 4; // 超采样倍数,用于抗锯齿 /* ---------------- PNG 编码 ---------------- */ const CRC_TABLE = (() => { const table = new Int32Array(256); for (let n = 0; n < 256; n++) { let c = n; for (let k = 0; k < 8; k++) { c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; } table[n] = c; } return table; })(); function crc32(buf) { let crc = -1; for (let i = 0; i < buf.length; i++) { crc = CRC_TABLE[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8); } return (crc ^ -1) >>> 0; } function chunk(type, data) { const typeBuf = Buffer.from(type, 'ascii'); const lenBuf = Buffer.alloc(4); lenBuf.writeUInt32BE(data.length, 0); const crcBuf = Buffer.alloc(4); crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])), 0); return Buffer.concat([lenBuf, typeBuf, data, crcBuf]); } function encodePNG(width, height, rgba) { const sig = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(width, 0); ihdr.writeUInt32BE(height, 4); ihdr[8] = 8; // bit depth ihdr[9] = 6; // color type: RGBA ihdr[10] = 0; // compression ihdr[11] = 0; // filter ihdr[12] = 0; // interlace const stride = width * 4; const raw = Buffer.alloc((stride + 1) * height); for (let y = 0; y < height; y++) { raw[y * (stride + 1)] = 0; // filter: None rgba.copy(raw, y * (stride + 1) + 1, y * stride, (y + 1) * stride); } const idat = zlib.deflateSync(raw, { level: 9 }); return Buffer.concat([sig, chunk('IHDR', ihdr), chunk('IDAT', idat), chunk('IEND', Buffer.alloc(0))]); } /* ---------------- 图形绘制(线条风格) ---------------- */ const STROKE_HALF = 3; // 线宽 6px 的一半(相对 7px 更柔和) function roundedRectSDF(px, py, cx, cy, halfW, halfH, radius) { const qx = Math.abs(px - cx) - (halfW - radius); const qy = Math.abs(py - cy) - (halfH - radius); const outside = Math.hypot(Math.max(qx, 0), Math.max(qy, 0)); const inside = Math.min(Math.max(qx, qy), 0); return outside + inside - radius; } // 圆角矩形描边(line style) function roundedRectStroke(px, py, cx, cy, halfW, halfH, radius) { return Math.abs(roundedRectSDF(px, py, cx, cy, halfW, halfH, radius)) <= STROKE_HALF; } // 点到线段的距离(带圆头端点) function segmentHit(px, py, x1, y1, x2, y2) { const dx = x2 - x1; const dy = y2 - y1; const lenSq = dx * dx + dy * dy; let t = lenSq === 0 ? 0 : ((px - x1) * dx + (py - y1) * dy) / lenSq; t = Math.max(0, Math.min(1, t)); const nx = x1 + t * dx; const ny = y1 + t * dy; return Math.hypot(px - nx, py - ny) <= STROKE_HALF; } function polylineHit(x, y, points, closed = false) { for (let i = 0; i < points.length - 1; i++) { const [x1, y1] = points[i]; const [x2, y2] = points[i + 1]; if (segmentHit(x, y, x1, y1, x2, y2)) return true; } if (closed && points.length > 2) { const [x1, y1] = points[points.length - 1]; const [x2, y2] = points[0]; if (segmentHit(x, y, x1, y1, x2, y2)) return true; } return false; } // 首页:带飞檐的屋顶 + 墙体 + 拱形门(线条风格) function homeHit(x, y) { // 屋顶:两侧檐口略宽于墙体 const apex = [40.5, 13]; const eaveLeft = [11, 41]; const eaveRight = [70, 41]; const roof = [eaveLeft, apex, eaveRight]; // 墙体顶部落在屋顶斜线上,避免多出一条横线 const wallLeft = 19; const wallRight = 62; const wallBottom = 73; const slope = (apex[1] - eaveLeft[1]) / (apex[0] - eaveLeft[0]); const wallTopY = apex[1] + (wallLeft - apex[0]) * slope; const walls = [ [wallLeft, wallTopY], [wallLeft, wallBottom], [wallRight, wallBottom], [wallRight, wallTopY] ]; // 拱形门:两侧竖线 + 上方半圆 const doorHalf = 6.5; const doorCenterX = (wallLeft + wallRight) / 2; const doorTopY = 61.5; const archSteps = 14; const arch = []; for (let i = 0; i <= archSteps; i++) { const t = Math.PI - (Math.PI * i) / archSteps; arch.push([doorCenterX + doorHalf * Math.cos(t), doorTopY - doorHalf * Math.sin(t)]); } const door = [[doorCenterX - doorHalf, wallBottom], ...arch, [doorCenterX + doorHalf, wallBottom]]; return polylineHit(x, y, roof) || polylineHit(x, y, walls) || polylineHit(x, y, door); } // 应用:2x2 空心圆角方块(描边间留 3px 空隙,外框与首页图标对齐) function appsHit(x, y) { const half = 11.5; const r = 5.5; const centers = [ [24.5, 27], [56.5, 27], [24.5, 59], [56.5, 59] ]; return centers.some(([cx, cy]) => roundedRectStroke(x, y, cx, cy, half, half, r)); } // 我的:头部圆形 + 肩部拱形(线条风格) function mimeHit(x, y) { // 头部圆环 const headR = 10; const headCY = 23; const headDist = Math.hypot(x - 40.5, y - headCY); if (Math.abs(headDist - headR) <= STROKE_HALF) return true; // 肩部:两侧竖线 + 上方椭圆拱(比正圆更贴合人像比例) const shoulderCY = 59; const rx = 24; const ry = 15; const leftX = 40.5 - rx; const rightX = 40.5 + rx; const bottomY = 73; if (segmentHit(x, y, leftX, bottomY, leftX, shoulderCY)) return true; if (segmentHit(x, y, rightX, shoulderCY, rightX, bottomY)) return true; const steps = 18; const arch = []; for (let i = 0; i <= steps; i++) { const t = Math.PI - (Math.PI * i) / steps; arch.push([40.5 + rx * Math.cos(t), shoulderCY - ry * Math.sin(t)]); } return polylineHit(x, y, arch); } function render(hitTest, hexColor) { const r = parseInt(hexColor.slice(1, 3), 16); const g = parseInt(hexColor.slice(3, 5), 16); const b = parseInt(hexColor.slice(5, 7), 16); const rgba = Buffer.alloc(SIZE * SIZE * 4, 0); const step = 1 / SS; for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { let hits = 0; for (let sy = 0; sy < SS; sy++) { for (let sx = 0; sx < SS; sx++) { const px = x + (sx + 0.5) * step; const py = y + (sy + 0.5) * step; if (hitTest(px, py)) hits++; } } if (!hits) continue; const coverage = hits / (SS * SS); const idx = (y * SIZE + x) * 4; rgba[idx] = r; rgba[idx + 1] = g; rgba[idx + 2] = b; rgba[idx + 3] = Math.round(coverage * 255); } } return encodePNG(SIZE, SIZE, rgba); } /* ---------------- 输出 ---------------- */ const outputDir = path.join(__dirname, '..', 'src', 'static', 'icon'); // 与“我的”图标保持一致(mime.png / mime_a.png 的主色) const GRAY = '#999999'; const ACTIVE = '#106eff'; const targets = [ ['home.png', homeHit, GRAY], ['home_a.png', homeHit, ACTIVE], ['apps.png', appsHit, GRAY], ['apps_a.png', appsHit, ACTIVE], ['mime.png', mimeHit, GRAY], ['mime_a.png', mimeHit, ACTIVE] ]; for (const [name, hitTest, color] of targets) { const filePath = path.join(outputDir, name); fs.writeFileSync(filePath, render(hitTest, color)); console.log('generated:', filePath); }