dapaijizhang3/generate-icons.html

197 lines
6.3 KiB
HTML
Raw Normal View History

2025-11-20 16:42:59 +08:00
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>生成TabBar图标</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
background: #f5f5f5;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
padding: 20px;
border-radius: 8px;
}
h1 {
color: #333;
}
.icon-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin-top: 20px;
}
.icon-item {
border: 1px solid #ddd;
padding: 15px;
border-radius: 8px;
text-align: center;
}
.icon-item h3 {
margin: 0 0 10px 0;
font-size: 14px;
}
canvas {
border: 1px solid #eee;
margin: 5px;
}
.download-btn {
display: inline-block;
margin: 5px;
padding: 5px 10px;
background: #07c160;
color: white;
text-decoration: none;
border-radius: 4px;
font-size: 12px;
}
.download-all {
margin-top: 20px;
padding: 10px 20px;
background: #07c160;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
.instructions {
background: #f0f8ff;
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>打牌记账小程序 - TabBar图标生成器</h1>
<div class="instructions">
<h3>使用说明:</h3>
<ol>
<li>点击每个图标下方的"下载"按钮保存单个图标</li>
<li>或点击"下载所有图标"按钮一次性下载全部</li>
<li>将下载的图片保存到 <code>miniapp/images/</code> 目录</li>
<li>图标尺寸81x81像素适合小程序使用</li>
</ol>
</div>
<div class="icon-grid" id="iconGrid"></div>
<button class="download-all" onclick="downloadAll()">下载所有图标</button>
</div>
<script>
// 图标配置
const icons = [
{ name: 'home', label: '首页图标(灰色)', color: '#999999' },
{ name: 'home-active', label: '首页图标(绿色)', color: '#07c160' },
{ name: 'stats', label: '统计图标(灰色)', color: '#999999' },
{ name: 'stats-active', label: '统计图标(绿色)', color: '#07c160' },
{ name: 'profile', label: '个人中心(灰色)', color: '#999999' },
{ name: 'profile-active', label: '个人中心(绿色)', color: '#07c160' }
];
// 绘制图标函数
function drawIcon(ctx, type, color) {
ctx.clearRect(0, 0, 81, 81);
ctx.strokeStyle = color;
ctx.fillStyle = color;
ctx.lineWidth = 3;
switch(type) {
case 'home':
// 绘制房子图标
ctx.beginPath();
// 屋顶
ctx.moveTo(40.5, 15);
ctx.lineTo(20, 35);
ctx.lineTo(61, 35);
ctx.closePath();
ctx.fill();
// 房子主体
ctx.fillRect(25, 35, 31, 30);
// 门
ctx.fillStyle = 'white';
ctx.fillRect(35, 50, 11, 15);
break;
case 'stats':
// 绘制统计图标(柱状图)
ctx.fillRect(20, 45, 12, 20);
ctx.fillRect(35, 35, 12, 30);
ctx.fillRect(50, 25, 12, 40);
break;
case 'profile':
// 绘制个人图标
// 头部
ctx.beginPath();
ctx.arc(40.5, 25, 10, 0, 2 * Math.PI);
ctx.fill();
// 身体
ctx.beginPath();
ctx.arc(40.5, 55, 18, Math.PI, 0, true);
ctx.fill();
break;
}
}
// 创建图标
function createIcons() {
const container = document.getElementById('iconGrid');
icons.forEach(icon => {
const div = document.createElement('div');
div.className = 'icon-item';
const canvas = document.createElement('canvas');
canvas.width = 81;
canvas.height = 81;
canvas.id = icon.name;
const ctx = canvas.getContext('2d');
const type = icon.name.replace('-active', '');
drawIcon(ctx, type, icon.color);
const title = document.createElement('h3');
title.textContent = icon.label;
const downloadBtn = document.createElement('a');
downloadBtn.className = 'download-btn';
downloadBtn.textContent = '下载';
downloadBtn.download = icon.name + '.png';
downloadBtn.href = canvas.toDataURL('image/png');
div.appendChild(title);
div.appendChild(canvas);
div.appendChild(document.createElement('br'));
div.appendChild(downloadBtn);
container.appendChild(div);
});
}
// 下载所有图标
function downloadAll() {
icons.forEach((icon, index) => {
setTimeout(() => {
const canvas = document.getElementById(icon.name);
const link = document.createElement('a');
link.download = icon.name + '.png';
link.href = canvas.toDataURL('image/png');
link.click();
}, index * 200); // 延迟下载,避免浏览器阻止
});
}
// 页面加载完成后创建图标
window.onload = createIcons;
</script>
</body>
</html>