-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpic.html
More file actions
288 lines (254 loc) · 12.5 KB
/
pic.html
File metadata and controls
288 lines (254 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>名字图片生成器</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
.preview-container {
width: 300px;
height: 300px;
position: relative;
cursor: pointer;
}
.preview-text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
font-weight: bold;
text-align: center;
width: 80%;
}
</style>
</head>
<body class="bg-gray-100 min-h-screen">
<div class="container mx-auto px-4 py-8">
<h1 class="text-3xl font-bold text-center mb-8">名字图片生成器</h1>
<div class="max-w-4xl mx-auto bg-white rounded-lg shadow-lg p-6">
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<!-- 左侧控制面板 -->
<div class="space-y-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">文字内容</label>
<input type="text" id="nameInput" placeholder="请输入要显示的文字"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">背景颜色</label>
<div class="flex space-x-2">
<input type="color" id="bgColorPicker"
class="h-10 w-20 border border-gray-300 rounded cursor-pointer">
<input type="text" id="hexColorInput" placeholder="#000000"
class="flex-1 px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
</div>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">圆角弧度</label>
<input type="range" id="radiusInput" min="0" max="150" value="60"
class="w-full">
<div class="text-sm text-gray-500 mt-1">当前值:<span id="radiusValue">60</span></div>
</div>
<div class="grid grid-cols-1 gap-2">
<button onclick="updatePreview('normal')" class="inline-flex justify-center items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 transition-all duration-200">
预览普通图片
</button>
<button onclick="updatePreview('circle')" class="inline-flex justify-center items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-purple-600 hover:bg-purple-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-purple-500 transition-all duration-200">
预览圆形图片
</button>
<button onclick="updatePreview('rounded')" class="inline-flex justify-center items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-pink-600 hover:bg-pink-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-pink-500 transition-all duration-200">
预览圆角图片
</button>
</div>
<div class="grid grid-cols-1 gap-2 mt-4">
<button onclick="downloadImage('normal')" class="inline-flex justify-center items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500 transition-all duration-200">
下载普通图片
</button>
<button onclick="downloadImage('circle')" class="inline-flex justify-center items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-teal-600 hover:bg-teal-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-teal-500 transition-all duration-200">
下载圆形图片
</button>
<button onclick="downloadImage('rounded')" class="inline-flex justify-center items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-cyan-600 hover:bg-cyan-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 transition-all duration-200">
下载圆角图片
</button>
</div>
</div>
<!-- 右侧预览区域 -->
<div class="flex items-center justify-center">
<div id="imageContainer" class="preview-container rounded-lg overflow-hidden">
<div id="nameText" class="preview-text"></div>
</div>
</div>
</div>
</div>
</div>
<script>
const nameInput = document.getElementById('nameInput');
const bgColorPicker = document.getElementById('bgColorPicker');
const hexColorInput = document.getElementById('hexColorInput');
const radiusInput = document.getElementById('radiusInput');
const radiusValue = document.getElementById('radiusValue');
const imageContainer = document.getElementById('imageContainer');
const nameText = document.getElementById('nameText');
let currentPreviewType = 'normal';
// 生成随机颜色
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function updatePreview(type) {
currentPreviewType = type;
const name = nameInput.value || '示例';
const bgColor = bgColorPicker.value;
nameText.textContent = name;
adjustTextSize();
imageContainer.style.backgroundColor = bgColor;
if (type === 'circle') {
imageContainer.style.borderRadius = '50%';
} else if (type === 'rounded') {
const radius = radiusInput.value;
imageContainer.style.borderRadius = `${radius}px`;
} else {
imageContainer.style.borderRadius = '0';
}
}
function adjustTextSize() {
const container = imageContainer;
const text = nameText;
text.style.width = '95%';
let fontSize = 100;
text.style.fontSize = fontSize + 'px';
text.style.lineHeight = '1';
const maxWidth = container.offsetWidth * 0.95;
const maxHeight = container.offsetHeight * 0.95;
let minSize = 1;
let maxSize = fontSize;
let bestSize = 1;
while (minSize <= maxSize) {
fontSize = Math.floor((minSize + maxSize) / 2);
text.style.fontSize = `${fontSize}px`;
if (text.offsetWidth <= maxWidth && text.offsetHeight <= maxHeight) {
bestSize = fontSize;
minSize = fontSize + 1;
} else {
maxSize = fontSize - 1;
}
}
bestSize = Math.floor(bestSize * 0.95);
text.style.fontSize = `${bestSize}px`;
}
function downloadImage(type) {
// 创建一个更高分辨率的 canvas (2倍大小)
const scale = 2;
const canvas = document.createElement('canvas');
const size = 300 * scale;
canvas.width = size;
canvas.height = size;
const ctx = canvas.getContext('2d');
// 缩放所有绘制操作
ctx.scale(scale, scale);
// 绘制背景
ctx.fillStyle = bgColorPicker.value;
if (type === 'circle') {
ctx.beginPath();
ctx.arc(150, 150, 150, 0, Math.PI * 2);
ctx.fill();
} else if (type === 'rounded') {
const radius = parseInt(radiusInput.value);
ctx.beginPath();
ctx.moveTo(radius, 0);
ctx.lineTo(300 - radius, 0);
ctx.quadraticCurveTo(300, 0, 300, radius);
ctx.lineTo(300, 300 - radius);
ctx.quadraticCurveTo(300, 300, 300 - radius, 300);
ctx.lineTo(radius, 300);
ctx.quadraticCurveTo(0, 300, 0, 300 - radius);
ctx.lineTo(0, radius);
ctx.quadraticCurveTo(0, 0, radius, 0);
ctx.fill();
} else {
ctx.fillRect(0, 0, 300, 300);
}
// 绘制文本
const text = nameInput.value || '示例';
ctx.fillStyle = 'white';
const fontSize = parseInt(nameText.style.fontSize);
ctx.font = `bold ${fontSize}px Arial, "Microsoft YaHei", "微软雅黑"`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
// 文本分行处理
const maxWidth = 300 * 0.9; // 使用未缩放的宽度进行计算
const lines = [];
let currentLine = '';
let currentWidth = 0;
// 优化的文本分行算法
for (let char of text) {
const testLine = currentLine + char;
const metrics = ctx.measureText(testLine);
if (metrics.width > maxWidth) {
lines.push(currentLine);
currentLine = char;
} else {
currentLine = testLine;
}
}
if (currentLine) {
lines.push(currentLine);
}
// 绘制多行文本
const lineHeight = fontSize * 1.2;
const totalHeight = lineHeight * lines.length;
const startY = (300 - totalHeight) / 2 + lineHeight / 2;
// 应用文本平滑
ctx.textRendering = 'optimizeLegibility';
ctx.imageSmoothingEnabled = true;
ctx.imageSmoothingQuality = 'high';
lines.forEach((line, index) => {
ctx.fillText(line, 150, startY + index * lineHeight, maxWidth);
});
// 创建下载链接
const link = document.createElement('a');
link.download = `${text}_${type}.png`;
link.href = canvas.toDataURL('image/png', 1.0); // 使用最高质量
link.click();
}
// 点击图片时更换随机颜色
imageContainer.addEventListener('click', function() {
const randomColor = getRandomColor();
bgColorPicker.value = randomColor;
hexColorInput.value = randomColor;
updatePreview(currentPreviewType);
});
// 其他事件监听器
nameInput.addEventListener('input', () => updatePreview(currentPreviewType));
bgColorPicker.addEventListener('input', function() {
hexColorInput.value = this.value;
updatePreview(currentPreviewType);
});
hexColorInput.addEventListener('input', function() {
if (/^#[0-9A-F]{6}$/i.test(this.value)) {
bgColorPicker.value = this.value;
updatePreview(currentPreviewType);
}
});
radiusInput.addEventListener('input', function() {
radiusValue.textContent = this.value;
if (currentPreviewType === 'rounded') {
updatePreview('rounded');
}
});
// 初始化
const initialColor = getRandomColor();
bgColorPicker.value = initialColor;
hexColorInput.value = initialColor;
updatePreview('normal');
// 窗口大小改变时调整文字大小
window.addEventListener('resize', () => adjustTextSize());
</script>
</body>
</html>