limaodada commited on
Commit
a78ebd7
·
verified ·
1 Parent(s): 8d30518

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +163 -225
index.html CHANGED
@@ -2,31 +2,34 @@
2
  <html lang="zh-CN">
3
  <head>
4
  <meta charset="UTF-8">
5
- <title>BeadMaster Pro v3.1 - 在线商业版</title>
6
  <style>
7
- :root { --bg: #1e1e20; --panel: #2b2b2e; --accent: #6366f1; --text: #e0e0e0; --border: #3f3f46; --ruler-bg: #252526; }
8
- body { margin: 0; overflow: hidden; background: var(--bg); color: var(--text); font-family: 'Segoe UI', sans-serif; user-select: none; }
9
 
10
  /* 布局框架 */
11
  #app { display: flex; width: 100vw; height: 100vh; }
12
- .sidebar { width: 64px; background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; align-items: center; padding-top: 15px; gap: 10px; z-index: 30; }
13
- .panels { width: 280px; background: var(--panel); border-left: 1px solid var(--border); display: flex; flex-direction: column; z-index: 30; }
14
  #canvas-wrapper { flex: 1; position: relative; overflow: hidden; background: #121214; display: flex; flex-direction: column; }
15
 
16
- /* 标尺区域 */
17
- #ruler-top { height: 30px; background: var(--ruler-bg); border-bottom: 1px solid var(--border); position: relative; overflow: hidden; margin-left: 30px; }
18
- #ruler-left { width: 30px; background: var(--ruler-bg); border-right: 1px solid var(--border); position: relative; overflow: hidden; float: left; height: 100%; }
19
- #corner-box { position: absolute; top: 49px; left: 64px; width: 30px; height: 30px; background: var(--ruler-bg); z-index: 25; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); color: #666; font-size: 10px; display: flex; justify-content: center; align-items: center; }
 
 
 
20
 
21
  /* 绘图可视区 */
22
- #viewport { flex: 1; position: relative; overflow: hidden; cursor: crosshair; }
23
  canvas { display: block; }
24
 
25
  /* 顶部栏 */
26
- .topbar { height: 48px; background: var(--panel); border-bottom: 1px solid var(--border); display: flex; align-items: center; padding: 0 20px; gap: 15px; z-index: 20; }
27
 
28
  /* 控件样式 */
29
- .tool-btn { width: 42px; height: 42px; border-radius: 6px; border: none; background: transparent; color: #aaa; cursor: pointer; font-size: 20px; transition: 0.2s; }
30
  .tool-btn:hover { background: #3e3e42; color: white; }
31
  .tool-btn.active { background: var(--accent); color: white; box-shadow: 0 2px 8px rgba(99,102,241,0.4); }
32
 
@@ -37,12 +40,16 @@
37
  input[type="number"] { background: #18181b; border: 1px solid #3f3f46; color: white; width: 50px; padding: 4px 8px; border-radius: 4px; text-align: center; }
38
  select { background: #18181b; border: 1px solid #3f3f46; color: white; padding: 5px; border-radius: 4px; outline: none; }
39
 
40
- /* 色板 */
41
- .color-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; padding: 10px; overflow-y: auto; flex: 1; }
42
- .swatch { aspect-ratio: 1; border-radius: 4px; cursor: pointer; border: 2px solid transparent; position: relative; font-size: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center; color: rgba(255,255,255,0.9); text-shadow: 0 1px 2px rgba(0,0,0,0.8); transition: transform 0.1s; }
43
- .swatch span { font-size: 9px; opacity: 0.8; }
44
- .swatch:hover { transform: scale(1.05); }
45
- .swatch.active { border-color: white; transform: scale(1.1); z-index: 2; box-shadow: 0 4px 12px rgba(0,0,0,0.5); }
 
 
 
 
46
 
47
  /* 裁剪弹窗 */
48
  .modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.85); z-index: 100; display: none; justify-content: center; align-items: center; }
@@ -60,7 +67,10 @@
60
  <div style="display:flex; justify-content:space-between; color:white; align-items: center;">
61
  <h3 style="margin:0;">✂️ 图片裁剪与取样</h3>
62
  <div style="display:flex; gap:10px; align-items:center;">
63
- <label style="font-size:13px; cursor:pointer;"><input type="checkbox" id="ditherCheck" checked> 开启色彩融合 (Dithering)</label>
 
 
 
64
  <button class="action-btn primary" onclick="confirmCrop()">✅ 确认转换</button>
65
  <button class="action-btn" onclick="closeModal()">取消</button>
66
  </div>
@@ -71,12 +81,13 @@
71
  <div class="resize-handle" onmousedown="startCropResize(event)"></div>
72
  </div>
73
  </div>
74
- <div style="color:#888; font-size:12px;">💡 提示:拖拽方框选择区域,拖拽右下角调整大小。转换时会自动匹配当前的豆子尺寸。</div>
75
  </div>
76
  </div>
77
 
78
  <div id="app">
79
  <div class="sidebar">
 
80
  <button class="tool-btn active" id="t-pencil" onclick="setTool('pencil')" title="画笔 (P)">✏️</button>
81
  <button class="tool-btn" id="t-eraser" onclick="setTool('eraser')" title="橡皮 (E)">🧹</button>
82
  <button class="tool-btn" id="t-bucket" onclick="setTool('bucket')" title="油漆桶 (G)">🪣</button>
@@ -110,14 +121,16 @@
110
  </label>
111
 
112
  <div style="flex:1;"></div>
113
- <button class="action-btn" onclick="exportImage()">💾 导出图片</button>
114
  <button class="action-btn" onclick="clearCanvas()">🗑️ 清空</button>
115
  </div>
116
 
117
  <div id="canvas-wrapper">
118
- <div id="corner-box">px</div>
119
- <div id="ruler-top"><canvas id="rulerTopCanvas"></canvas></div>
120
- <div style="flex:1; display:flex; overflow:hidden;">
 
 
121
  <div id="ruler-left"><canvas id="rulerLeftCanvas"></canvas></div>
122
  <div id="viewport">
123
  <canvas id="mainCanvas"></canvas>
@@ -127,21 +140,19 @@
127
  </div>
128
 
129
  <div class="panels">
130
- <div style="padding:15px; border-bottom:1px solid var(--border);">
131
- <div style="margin-bottom:10px; font-weight:bold; font-size:12px; color:#888;">当前选中</div>
132
- <div style="display:flex; gap:10px; align-items:center;">
133
- <div id="cur-color-box" style="width:48px; height:48px; border-radius:6px; background:red; border:1px solid white; box-shadow:0 2px 5px rgba(0,0,0,0.2);"></div>
134
- <div>
135
- <div id="cur-color-id" style="font-weight:bold; font-size:20px; font-family:monospace;">A-01</div>
136
- <div id="cur-color-hex" style="font-size:12px; color:#888;">#FF0000</div>
137
- </div>
138
- </div>
139
  </div>
140
- <div style="padding:10px; font-size:12px; color:#888; border-bottom:1px solid var(--border); display:flex; justify-content:space-between;">
141
- <span>色卡库 (字母+数字)</span>
142
- <span id="palette-count">20色</span>
 
 
 
 
143
  </div>
144
- <div class="color-grid" id="palette-container"></div>
145
  <div style="padding:10px; font-size:11px; color:#666; border-top:1px solid var(--border); background: #18181b;">
146
  <div id="info-coord">XY: (0, 0)</div>
147
  <div id="info-zoom">Zoom: 100%</div>
@@ -150,18 +161,20 @@
150
  </div>
151
 
152
  <script>
153
- // --- 升级版色卡数据 (字母+数字) ---
154
  const PALETTE = [
155
- {id:'A01', hex:'#111111', name:'夜黑'}, {id:'A02', hex:'#FFFFFF', name:'纯白'},
156
- {id:'B01', hex:'#FF0000', name:'正红'}, {id:'B02', hex:'#FF6B6B', name:'西瓜红'},
157
- {id:'C01', hex:'#00FF00', name:'鲜绿'}, {id:'C02', hex:'#2ECC71', name:'草绿'},
158
- {id:'D01', hex:'#0000FF', name:'深蓝'}, {id:'D02', hex:'#3498DB', name:'天蓝'},
159
- {id:'E01', hex:'#FFFF00', name:'柠檬黄'}, {id:'E02', hex:'#F1C40F', name:'金黄'},
160
- {id:'F01', hex:'#FFA500', name:'亮橙'}, {id:'F02', hex:'#E67E22', name:'焦糖'},
161
- {id:'G01', hex:'#9B59B6', name:'紫罗兰'}, {id:'G02', hex:'#8E44AD', name:'深紫'},
162
- {id:'H01', hex:'#FFC0CB', name:'浅粉'}, {id:'H02', hex:'#FF69B4', name:'热粉'},
163
- {id:'J01', hex:'#95A5A6', name:'水泥灰'}, {id:'J02', hex:'#7F8C8D', name:'深灰'},
164
- {id:'K01', hex:'#A52A2A', name:'红棕'}, {id:'K02', hex:'#D35400', name:'南瓜'}
 
 
165
  ];
166
 
167
  // --- 核心状态 ---
@@ -171,9 +184,9 @@ const state = {
171
  scale: 25,
172
  offsetX: 50, offsetY: 50,
173
  tool: 'pencil',
174
- curColorIdx: 2,
175
  isDrag: false, lastX: 0, lastY: 0,
176
- shape: 'circle' // 'circle' or 'square'
177
  };
178
 
179
  // DOM 元素
@@ -184,13 +197,15 @@ const rulerTop = document.getElementById('rulerTopCanvas');
184
  const ctxRTop = rulerTop.getContext('2d');
185
  const rulerLeft = document.getElementById('rulerLeftCanvas');
186
  const ctxRLeft = rulerLeft.getContext('2d');
 
 
187
 
188
  // --- 初始化 ---
189
  function init() {
190
  window.addEventListener('resize', resizeAll);
191
  resizeAll();
192
- initPalette();
193
- draw(); // 初始绘制
194
 
195
  // 滚轮缩放事件
196
  viewport.addEventListener('wheel', e => {
@@ -199,7 +214,6 @@ function init() {
199
  const delta = e.deltaY > 0 ? 0.9 : 1.1;
200
  state.scale = Math.max(5, Math.min(150, state.scale * delta));
201
 
202
- // 以鼠标为中心缩放
203
  const rect = viewport.getBoundingClientRect();
204
  const mx = e.clientX - rect.left;
205
  const my = e.clientY - rect.top;
@@ -211,63 +225,70 @@ function init() {
211
  }, {passive:false});
212
  }
213
 
214
- function initPalette() {
215
- const box = document.getElementById('palette-container');
216
- box.innerHTML = '';
217
- document.getElementById('palette-count').innerText = PALETTE.length + '色';
218
-
 
 
 
 
 
 
 
 
 
 
219
  PALETTE.forEach((p, idx) => {
220
- const div = document.createElement('div');
221
- div.className = `swatch ${idx===state.curColorIdx?'active':''}`;
222
- div.style.backgroundColor = p.hex;
223
-
224
- // 智能反色文字
225
- const rgb = parseInt(p.hex.slice(1), 16);
226
- const luma = 0.2126*((rgb>>16)&0xff) + 0.7152*((rgb>>8)&0xff) + 0.0722*(rgb&0xff);
227
- div.style.color = luma < 128 ? 'white' : 'black';
228
-
229
- div.innerHTML = `<b>${p.id}</b>`;
230
- div.onclick = () => selectColor(idx);
231
- box.appendChild(div);
232
  });
233
- updateCurColorUI();
234
  }
235
 
236
  function selectColor(idx) {
237
  state.curColorIdx = idx;
238
- initPalette();
239
- updateCurColorUI();
240
  }
241
 
242
- function updateCurColorUI() {
243
- const p = PALETTE[state.curColorIdx];
244
- document.getElementById('cur-color-box').style.backgroundColor = p.hex;
245
- document.getElementById('cur-color-id').innerText = p.id;
246
- document.getElementById('cur-color-hex').innerText = `${p.name} (${p.hex})`;
 
 
 
 
247
  }
248
 
 
249
  function resizeAll() {
250
- cvs.width = viewport.clientWidth;
251
- cvs.height = viewport.clientHeight;
252
-
253
- // 调整标尺Canvas大小
254
- rulerTop.width = viewport.clientWidth;
255
- rulerTop.height = 30;
256
- rulerLeft.width = 30;
257
- rulerLeft.height = viewport.clientHeight;
258
-
259
  draw();
260
  }
261
 
262
  // --- 核心渲染引擎 ---
263
  function draw() {
264
- // 0. 准备工作
265
  state.shape = document.getElementById('shapeSelect').value;
266
- const s = state.scale;
267
- const ox = state.offsetX;
268
- const oy = state.offsetY;
269
 
270
- // 清空画布
271
  ctx.fillStyle = '#121214';
272
  ctx.fillRect(0, 0, cvs.width, cvs.height);
273
 
@@ -275,12 +296,8 @@ function draw() {
275
  ctx.translate(ox, oy);
276
 
277
  // 1. 绘制网格背景 (细线)
278
- ctx.beginPath();
279
- ctx.lineWidth = 1;
280
- ctx.strokeStyle = '#2a2a2d'; // 很暗的灰
281
- // 垂直线
282
  for(let i=0; i<=state.gridW; i++) { ctx.moveTo(i*s, 0); ctx.lineTo(i*s, state.gridH*s); }
283
- // 水平线
284
  for(let i=0; i<=state.gridH; i++) { ctx.moveTo(0, i*s); ctx.lineTo(state.gridW*s, i*s); }
285
  ctx.stroke();
286
 
@@ -289,102 +306,60 @@ function draw() {
289
  const [x, y] = key.split(',').map(Number);
290
  const p = PALETTE[state.pixels[key]];
291
  if(!p) continue;
292
-
293
  ctx.fillStyle = p.hex;
294
-
295
  if (state.shape === 'circle') {
296
- // 圆形模式
297
- ctx.beginPath();
298
- ctx.arc(x*s + s/2, y*s + s/2, s/2 - 1, 0, Math.PI*2); // -1 留缝隙
299
- ctx.fill();
300
  } else {
301
- // 正方形模式 (填满格子,微留缝隙)
302
  ctx.fillRect(x*s + 0.5, y*s + 0.5, s - 1, s - 1);
303
  }
304
-
305
- // 显示色号 (如果开启)
306
- if(document.getElementById('showIds').checked && s > 20) {
307
  ctx.fillStyle = getContrastColor(p.hex);
308
- ctx.font = `bold ${Math.floor(s*0.35)}px Arial`;
309
- ctx.textAlign = 'center';
310
- ctx.textBaseline = 'middle';
311
  ctx.fillText(p.id, x*s + s/2, y*s + s/2);
312
  }
313
  }
314
 
315
- // 3. 绘制【每10格】的大粗线 (加强版)
316
- ctx.beginPath();
317
- ctx.lineWidth = 2; // 加粗
318
- ctx.strokeStyle = '#666'; // 更亮的灰,确保看得见
319
  for(let i=0; i<=state.gridW; i+=10) { ctx.moveTo(i*s, 0); ctx.lineTo(i*s, state.gridH*s); }
320
  for(let i=0; i<=state.gridH; i+=10) { ctx.moveTo(0, i*s); ctx.lineTo(state.gridW*s, i*s); }
321
  ctx.stroke();
322
 
323
- // 4. 绘制画布外边框
324
- ctx.strokeStyle = '#6366f1';
325
- ctx.lineWidth = 2;
326
  ctx.strokeRect(0, 0, state.gridW*s, state.gridH*s);
327
-
328
  ctx.restore();
329
 
330
- // 5. 绘制标尺 (独立Canvas)
331
  drawRulers();
 
 
332
  }
333
 
334
  function drawRulers() {
335
- const s = state.scale;
336
- const ox = state.offsetX;
337
- const oy = state.offsetY;
338
-
339
- // --- 顶部标尺 (X轴) ---
340
- const ctxT = ctxRTop;
341
- ctxT.fillStyle = '#252526'; ctxT.fillRect(0,0,rulerTop.width, 30);
342
- ctxT.fillStyle = '#999'; ctxT.font = '10px Arial'; ctxT.textAlign = 'center';
343
- ctxT.beginPath();
344
- ctxT.strokeStyle = '#555';
345
-
346
- // 计算可见范围优化性能
347
- const startX = Math.max(0, Math.floor(-ox / s));
348
- const endX = Math.min(state.gridW, Math.ceil((cvs.width - ox) / s));
349
-
350
  for(let i=startX; i<=endX; i++) {
351
- const screenX = ox + i * s;
352
- // 每10格画数字,每1格画小刻度
353
- if(i % 10 === 0) {
354
- ctxT.moveTo(screenX, 15); ctxT.lineTo(screenX, 30);
355
- ctxT.fillText(i, screenX, 12); // 数字
356
- } else if (i % 5 === 0) {
357
- ctxT.moveTo(screenX, 22); ctxT.lineTo(screenX, 30);
358
- } else if (s > 15) { // 缩放够大才显示小刻度
359
- ctxT.moveTo(screenX, 26); ctxT.lineTo(screenX, 30);
360
- }
361
  }
362
  ctxT.stroke();
363
-
364
- // --- 左侧标尺 (Y轴) ---
365
- const ctxL = ctxRLeft;
366
- ctxL.fillStyle = '#252526'; ctxL.fillRect(0,0,30, rulerLeft.height);
367
- ctxL.fillStyle = '#999'; ctxL.font = '10px Arial'; ctxL.textAlign = 'center'; ctxL.textBaseline = 'middle';
368
- ctxL.beginPath();
369
-
370
- const startY = Math.max(0, Math.floor(-oy / s));
371
- const endY = Math.min(state.gridH, Math.ceil((cvs.height - oy) / s));
372
-
373
  for(let i=startY; i<=endY; i++) {
374
- const screenY = oy + i * s;
375
  if(i % 10 === 0) {
376
- ctxL.moveTo(15, screenY); ctxL.lineTo(30, screenY);
377
- // 竖着写数字有点挤,我们把数字稍微转一下或者只写数字
378
- ctxL.save();
379
- ctxL.translate(12, screenY);
380
- ctxL.rotate(-Math.PI/2);
381
- ctxL.fillText(i, 0, 0);
382
- ctxL.restore();
383
- } else if (i % 5 === 0) {
384
- ctxL.moveTo(22, screenY); ctxL.lineTo(30, screenY);
385
- } else if (s > 15) {
386
- ctxL.moveTo(26, screenY); ctxL.lineTo(30, screenY);
387
- }
388
  }
389
  ctxL.stroke();
390
  }
@@ -397,19 +372,15 @@ function getContrastColor(hex) {
397
  // --- 交互逻辑 ---
398
  viewport.onmousedown = e => {
399
  if(state.tool === 'move' || e.button === 1) {
400
- state.isDrag = true; state.lastX = e.clientX; state.lastY = e.clientY;
401
- return;
402
  }
403
- paint(e);
404
- viewport.onmousemove = paint;
405
  };
406
  window.onmouseup = () => { state.isDrag = false; viewport.onmousemove = null; };
407
  window.onmousemove = e => {
408
  if(state.isDrag) {
409
- state.offsetX += e.clientX - state.lastX;
410
- state.offsetY += e.clientY - state.lastY;
411
- state.lastX = e.clientX; state.lastY = e.clientY;
412
- draw();
413
  }
414
  updateInfo(e);
415
  };
@@ -418,9 +389,7 @@ function paint(e) {
418
  const rect = viewport.getBoundingClientRect();
419
  const x = Math.floor((e.clientX - rect.left - state.offsetX) / state.scale);
420
  const y = Math.floor((e.clientY - rect.top - state.offsetY) / state.scale);
421
-
422
  if(x<0 || x>=state.gridW || y<0 || y>=state.gridH) return;
423
-
424
  const key = `${x},${y}`;
425
  if(state.tool === 'pencil') state.pixels[key] = state.curColorIdx;
426
  else if(state.tool === 'eraser') delete state.pixels[key];
@@ -435,13 +404,9 @@ function paint(e) {
435
  function floodFill(x, y, targetIdx) {
436
  const oldIdx = state.pixels[`${x},${y}`];
437
  if(oldIdx === targetIdx) return;
438
- const q = [[x,y]];
439
- // 防止死循环上限
440
- let count = 0;
441
  while(q.length && count < state.gridW * state.gridH) {
442
- count++;
443
- const [cx, cy] = q.pop();
444
- const k = `${cx},${cy}`;
445
  if(cx<0 || cx>=state.gridW || cy<0 || cy>=state.gridH) continue;
446
  if(state.pixels[k] !== oldIdx) continue;
447
  state.pixels[k] = targetIdx;
@@ -457,8 +422,8 @@ function setTool(t) {
457
  viewport.style.cursor = t==='move' ? 'grab' : 'crosshair';
458
  }
459
  function resizeGrid() {
460
- state.gridW = parseInt(document.getElementById('gridW').value);
461
- state.gridH = parseInt(document.getElementById('gridH').value);
462
  draw();
463
  }
464
  function updateInfo(e) {
@@ -467,66 +432,39 @@ function updateInfo(e) {
467
  const rect = viewport.getBoundingClientRect();
468
  const x = Math.floor((e.clientX - rect.left - state.offsetX) / state.scale);
469
  const y = Math.floor((e.clientY - rect.top - state.offsetY) / state.scale);
470
- if(x>=0 && x<state.gridW && y>=0 && y<state.gridH)
471
- document.getElementById('info-coord').innerText = `XY: (${x}, ${y})`;
472
  }
473
  }
474
  function clearCanvas() { if(confirm('确定清空画布吗?')) { state.pixels={}; draw(); } }
475
 
476
  function exportImage() {
477
- // 导出高清大图
478
  const expCvs = document.createElement('canvas');
479
- const unit = 40; // 导出时每格40像素,保证清晰
480
- expCvs.width = state.gridW * unit;
481
- expCvs.height = state.gridH * unit;
482
  const eCtx = expCvs.getContext('2d');
483
-
484
- // 白底
485
- eCtx.fillStyle = 'white';
486
- eCtx.fillRect(0, 0, expCvs.width, expCvs.height);
487
-
488
- // 绘制内容
489
  for(let key in state.pixels) {
490
  const [x, y] = key.split(',').map(Number);
491
  const p = PALETTE[state.pixels[key]];
492
  eCtx.fillStyle = p.hex;
493
-
494
  if(state.shape === 'circle') {
495
- eCtx.beginPath();
496
- eCtx.arc(x*unit + unit/2, y*unit + unit/2, unit/2 - 2, 0, Math.PI*2);
497
- eCtx.fill();
498
- // 拼豆孔
499
- eCtx.fillStyle = '#fff';
500
- eCtx.beginPath();
501
- eCtx.arc(x*unit + unit/2, y*unit + unit/2, unit/8, 0, Math.PI*2);
502
- eCtx.fill();
503
- } else {
504
- eCtx.fillRect(x*unit, y*unit, unit, unit);
505
- }
506
-
507
- // 导出时也画上编号,方便打印
508
  if(state.shape === 'circle') {
509
- eCtx.fillStyle = getContrastColor(p.hex) === '#fff' ? '#000' : '#fff'; // 孔上面用反色
510
- eCtx.font = 'bold 10px Arial';
511
- eCtx.textAlign = 'center';
512
  eCtx.fillText(p.id, x*unit + unit/2, y*unit + unit - 5);
513
  }
514
  }
515
-
516
- // 画网格线(导出图)
517
- eCtx.strokeStyle = '#ccc';
518
- eCtx.lineWidth = 1;
519
  for(let i=0; i<=state.gridW; i++) { eCtx.moveTo(i*unit, 0); eCtx.lineTo(i*unit, expCvs.height); }
520
  for(let i=0; i<=state.gridH; i++) { eCtx.moveTo(0, i*unit); eCtx.lineTo(expCvs.width, i*unit); }
521
  eCtx.stroke();
522
-
523
- const link = document.createElement('a');
524
- link.download = 'BeadDesign_v3.png';
525
- link.href = expCvs.toDataURL();
526
- link.click();
527
  }
528
 
529
- // --- 裁剪逻辑复用 (保持 v2.0 逻辑) ---
530
  const modal = document.getElementById('cropModal');
531
  const cropImg = document.getElementById('crop-img');
532
  const cropBox = document.getElementById('crop-box');
@@ -623,7 +561,7 @@ function confirmCrop() {
623
  }
624
  }
625
  closeModal();
626
- resizeAll(); // 强制刷新视图和标尺
627
  }
628
 
629
  init();
 
2
  <html lang="zh-CN">
3
  <head>
4
  <meta charset="UTF-8">
5
+ <title>BeadMaster Pro v4.0 - 商业终极版</title>
6
  <style>
7
+ :root { --bg: #1e1e20; --panel: #2b2b2e; --accent: #6366f1; --text: #e0e0e0; --border: #3f3f46; --ruler-bg: #252526; --list-hover: #3e3e42; }
8
+ body { margin: 0; overflow: hidden; background: var(--bg); color: var(--text); font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; user-select: none; }
9
 
10
  /* 布局框架 */
11
  #app { display: flex; width: 100vw; height: 100vh; }
12
+ .sidebar { width: 64px; background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; align-items: center; padding-top: 15px; gap: 10px; z-index: 40; }
13
+ .panels { width: 300px; background: var(--panel); border-left: 1px solid var(--border); display: flex; flex-direction: column; z-index: 40; }
14
  #canvas-wrapper { flex: 1; position: relative; overflow: hidden; background: #121214; display: flex; flex-direction: column; }
15
 
16
+ /* 标尺区域 (高层级) */
17
+ #ruler-container { flex: 1; display: flex; flex-direction: column; position: relative; }
18
+ #ruler-top-bar { height: 30px; background: var(--ruler-bg); border-bottom: 1px solid var(--border); display: flex; z-index: 35; }
19
+ #corner-box { width: 64px; height: 30px; background: var(--ruler-bg); border-right: 1px solid var(--border); color: #666; font-size: 10px; display: flex; justify-content: center; align-items: center; flex-shrink: 0; }
20
+ #ruler-top { flex: 1; position: relative; overflow: hidden; }
21
+ #main-area { flex: 1; display: flex; position: relative; overflow: hidden; }
22
+ #ruler-left { width: 30px; background: var(--ruler-bg); border-right: 1px solid var(--border); position: relative; overflow: hidden; z-index: 35; flex-shrink: 0; }
23
 
24
  /* 绘图可视区 */
25
+ #viewport { flex: 1; position: relative; overflow: hidden; cursor: crosshair; z-index: 10; }
26
  canvas { display: block; }
27
 
28
  /* 顶部栏 */
29
+ .topbar { height: 48px; background: var(--panel); border-bottom: 1px solid var(--border); display: flex; align-items: center; padding: 0 20px; gap: 15px; z-index: 30; }
30
 
31
  /* 控件样式 */
32
+ .tool-btn { width: 42px; height: 42px; border-radius: 6px; border: none; background: transparent; color: #aaa; cursor: pointer; font-size: 20px; transition: 0.2s; display:flex; justify-content:center; align-items:center; }
33
  .tool-btn:hover { background: #3e3e42; color: white; }
34
  .tool-btn.active { background: var(--accent); color: white; box-shadow: 0 2px 8px rgba(99,102,241,0.4); }
35
 
 
40
  input[type="number"] { background: #18181b; border: 1px solid #3f3f46; color: white; width: 50px; padding: 4px 8px; border-radius: 4px; text-align: center; }
41
  select { background: #18181b; border: 1px solid #3f3f46; color: white; padding: 5px; border-radius: 4px; outline: none; }
42
 
43
+ /* --- V4.0 新版右侧面板:材料清单 --- */
44
+ .panel-header { padding: 15px; border-bottom: 1px solid var(--border); font-weight: bold; font-size: 14px; display: flex; justify-content: space-between; align-items: center; }
45
+ #material-list { flex: 1; overflow-y: auto; padding: 10px 0; }
46
+ .material-item { display: flex; align-items: center; padding: 8px 15px; gap: 10px; border-bottom: 1px solid #333; font-size: 13px; cursor: pointer; transition: background 0.2s; }
47
+ .material-item:hover { background: var(--list-hover); }
48
+ .material-item.active { background: #3e3e42; border-left: 3px solid var(--accent); }
49
+ .list-swatch { width: 24px; height: 24px; border-radius: 4px; border: 1px solid rgba(255,255,255,0.2); flex-shrink: 0; }
50
+ .list-info { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
51
+ .list-count { font-family: monospace; font-weight: bold; color: var(--accent); }
52
+ #total-stats { padding: 15px; border-top: 1px solid var(--border); background: #18181b; font-size: 13px; display: flex; justify-content: space-between; font-weight: bold; }
53
 
54
  /* 裁剪弹窗 */
55
  .modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.85); z-index: 100; display: none; justify-content: center; align-items: center; }
 
67
  <div style="display:flex; justify-content:space-between; color:white; align-items: center;">
68
  <h3 style="margin:0;">✂️ 图片裁剪与取样</h3>
69
  <div style="display:flex; gap:10px; align-items:center;">
70
+ <label style="font-size:13px; cursor:pointer; display:flex; align-items:center; gap:5px;">
71
+ <input type="checkbox" id="ditherCheck" checked>
72
+ <span>开启色彩融合 (Dithering)</span>
73
+ </label>
74
  <button class="action-btn primary" onclick="confirmCrop()">✅ 确认转换</button>
75
  <button class="action-btn" onclick="closeModal()">取消</button>
76
  </div>
 
81
  <div class="resize-handle" onmousedown="startCropResize(event)"></div>
82
  </div>
83
  </div>
84
+ <div style="color:#888; font-size:12px;">💡 提示:拖拽方框选择区域,拖拽右下角调整大小。转换时会自动匹配当前的自定义大小。</div>
85
  </div>
86
  </div>
87
 
88
  <div id="app">
89
  <div class="sidebar">
90
+ <div style="height: 48px; display: flex; align-items: center; font-weight: bold; color: var(--accent);">BMP</div>
91
  <button class="tool-btn active" id="t-pencil" onclick="setTool('pencil')" title="画笔 (P)">✏️</button>
92
  <button class="tool-btn" id="t-eraser" onclick="setTool('eraser')" title="橡皮 (E)">🧹</button>
93
  <button class="tool-btn" id="t-bucket" onclick="setTool('bucket')" title="油漆桶 (G)">🪣</button>
 
121
  </label>
122
 
123
  <div style="flex:1;"></div>
124
+ <button class="action-btn" onclick="exportImage()">💾 导出图纸</button>
125
  <button class="action-btn" onclick="clearCanvas()">🗑️ 清空</button>
126
  </div>
127
 
128
  <div id="canvas-wrapper">
129
+ <div id="ruler-top-bar">
130
+ <div id="corner-box">px</div>
131
+ <div id="ruler-top"><canvas id="rulerTopCanvas"></canvas></div>
132
+ </div>
133
+ <div id="main-area">
134
  <div id="ruler-left"><canvas id="rulerLeftCanvas"></canvas></div>
135
  <div id="viewport">
136
  <canvas id="mainCanvas"></canvas>
 
140
  </div>
141
 
142
  <div class="panels">
143
+ <div class="panel-header">
144
+ <span>📦 材料清单 (Shopping List)</span>
145
+ <button class="action-btn" style="padding: 4px 8px; font-size: 11px;" onclick="copyList()">复制清单</button>
 
 
 
 
 
 
146
  </div>
147
+
148
+ <div id="material-list">
149
+ </div>
150
+
151
+ <div id="total-stats">
152
+ <span>总颗粒数:</span>
153
+ <span id="total-beads-count">0 颗</span>
154
  </div>
155
+
156
  <div style="padding:10px; font-size:11px; color:#666; border-top:1px solid var(--border); background: #18181b;">
157
  <div id="info-coord">XY: (0, 0)</div>
158
  <div id="info-zoom">Zoom: 100%</div>
 
161
  </div>
162
 
163
  <script>
164
+ // --- 色卡数据 (模拟国际标准色号) ---
165
  const PALETTE = [
166
+ {id:'A01', hex:'#111111', name:'夜黑(Black)'}, {id:'A02', hex:'#FFFFFF', name:'纯白(White)'},
167
+ {id:'B01', hex:'#FF0000', name:'正红(Red)'}, {id:'B02', hex:'#FF6B6B', name:'西瓜红(Melon)'},
168
+ {id:'C01', hex:'#00FF00', name:'鲜绿(Green)'}, {id:'C02', hex:'#2ECC71', name:'草绿(Lime)'},
169
+ {id:'D01', hex:'#0000FF', name:'深蓝(D.Blue)'}, {id:'D02', hex:'#3498DB', name:'天蓝(Sky)'},
170
+ {id:'E01', hex:'#FFFF00', name:'柠檬黄(Yellow)'}, {id:'E02', hex:'#F1C40F', name:'金黄(Gold)'},
171
+ {id:'F01', hex:'#FFA500', name:'亮橙(Orange)'}, {id:'F02', hex:'#E67E22', name:'焦糖(Spice)'},
172
+ {id:'G01', hex:'#9B59B6', name:'紫罗兰(Violet)'}, {id:'G02', hex:'#8E44AD', name:'深紫(Purple)'},
173
+ {id:'H01', hex:'#FFC0CB', name:'浅粉(Pink)'}, {id:'H02', hex:'#FF69B4', name:'热粉(Hot Pink)'},
174
+ {id:'J01', hex:'#95A5A6', name:'水泥灰(Grey)'}, {id:'J02', hex:'#7F8C8D', name:'深灰(D.Grey)'},
175
+ {id:'K01', hex:'#A52A2A', name:'红棕(Brown)'}, {id:'K02', hex:'#D35400', name:'南瓜(Pumpkin)'},
176
+ {id:'L01', hex:'#F0E68C', name:'卡其(Khaki)'}, {id:'L02', hex:'#E6E6FA', name:'薰衣草(Lavender)'},
177
+ {id:'M01', hex:'#40E0D0', name:'绿松石(Turquoise)'},{id:'M02', hex:'#FFD700', name:'金色(Metallic Gold)'}
178
  ];
179
 
180
  // --- 核心状态 ---
 
184
  scale: 25,
185
  offsetX: 50, offsetY: 50,
186
  tool: 'pencil',
187
+ curColorIdx: 2, // 默认红色
188
  isDrag: false, lastX: 0, lastY: 0,
189
+ shape: 'circle'
190
  };
191
 
192
  // DOM 元素
 
197
  const ctxRTop = rulerTop.getContext('2d');
198
  const rulerLeft = document.getElementById('rulerLeftCanvas');
199
  const ctxRLeft = rulerLeft.getContext('2d');
200
+ const materialListEl = document.getElementById('material-list');
201
+ const totalBeadsEl = document.getElementById('total-beads-count');
202
 
203
  // --- 初始化 ---
204
  function init() {
205
  window.addEventListener('resize', resizeAll);
206
  resizeAll();
207
+ updateMaterialList(); // 初始化材料清单
208
+ draw();
209
 
210
  // 滚轮缩放事件
211
  viewport.addEventListener('wheel', e => {
 
214
  const delta = e.deltaY > 0 ? 0.9 : 1.1;
215
  state.scale = Math.max(5, Math.min(150, state.scale * delta));
216
 
 
217
  const rect = viewport.getBoundingClientRect();
218
  const mx = e.clientX - rect.left;
219
  const my = e.clientY - rect.top;
 
225
  }, {passive:false});
226
  }
227
 
228
+ // --- V4.0 核心功能:更新材料清单 ---
229
+ function updateMaterialList() {
230
+ const counts = {};
231
+ let total = 0;
232
+ // 统计画布上每种颜色的数量
233
+ for(let key in state.pixels) {
234
+ const idx = state.pixels[key];
235
+ counts[idx] = (counts[idx] || 0) + 1;
236
+ total++;
237
+ }
238
+
239
+ materialListEl.innerHTML = '';
240
+ totalBeadsEl.innerText = `${total} 颗`;
241
+
242
+ // 生成列表项
243
  PALETTE.forEach((p, idx) => {
244
+ const count = counts[idx] || 0;
245
+ if(count > 0 || idx === state.curColorIdx) { // 显示已用颜色 和 当前选中颜色
246
+ const item = document.createElement('div');
247
+ item.className = `material-item ${idx === state.curColorIdx ? 'active' : ''}`;
248
+ item.onclick = () => selectColor(idx);
249
+ item.innerHTML = `
250
+ <div class="list-swatch" style="background-color: ${p.hex}"></div>
251
+ <div class="list-info"><b>${p.id}</b> - ${p.name}</div>
252
+ <div class="list-count">x${count}</div>
253
+ `;
254
+ materialListEl.appendChild(item);
255
+ }
256
  });
 
257
  }
258
 
259
  function selectColor(idx) {
260
  state.curColorIdx = idx;
261
+ updateMaterialList(); // 刷新选中状态
262
+ setTool('pencil');
263
  }
264
 
265
+ function copyList() {
266
+ let text = "--- 拼豆材料清单 ---\n";
267
+ const counts = {};
268
+ for(let key in state.pixels) counts[state.pixels[key]] = (counts[state.pixels[key]] || 0) + 1;
269
+ PALETTE.forEach((p, idx) => {
270
+ if(counts[idx]) text += `[${p.id}] ${p.name}: ${counts[idx]}颗\n`;
271
+ });
272
+ text += `\n总计: ${Object.values(counts).reduce((a,b)=>a+b, 0)} 颗`;
273
+ navigator.clipboard.writeText(text).then(() => alert('清单已复制到剪贴板!'));
274
  }
275
 
276
+
277
  function resizeAll() {
278
+ // 强制同步容器大小
279
+ const vw = viewport.clientWidth;
280
+ const vh = viewport.clientHeight;
281
+ cvs.width = vw; cvs.height = vh;
282
+ rulerTop.width = vw; rulerTop.height = 30;
283
+ rulerLeft.width = 30; rulerLeft.height = vh;
 
 
 
284
  draw();
285
  }
286
 
287
  // --- 核心渲染引擎 ---
288
  function draw() {
 
289
  state.shape = document.getElementById('shapeSelect').value;
290
+ const s = state.scale; const ox = state.offsetX; const oy = state.offsetY;
 
 
291
 
 
292
  ctx.fillStyle = '#121214';
293
  ctx.fillRect(0, 0, cvs.width, cvs.height);
294
 
 
296
  ctx.translate(ox, oy);
297
 
298
  // 1. 绘制网格背景 (细线)
299
+ ctx.beginPath(); ctx.lineWidth = 1; ctx.strokeStyle = '#2a2a2d';
 
 
 
300
  for(let i=0; i<=state.gridW; i++) { ctx.moveTo(i*s, 0); ctx.lineTo(i*s, state.gridH*s); }
 
301
  for(let i=0; i<=state.gridH; i++) { ctx.moveTo(0, i*s); ctx.lineTo(state.gridW*s, i*s); }
302
  ctx.stroke();
303
 
 
306
  const [x, y] = key.split(',').map(Number);
307
  const p = PALETTE[state.pixels[key]];
308
  if(!p) continue;
 
309
  ctx.fillStyle = p.hex;
 
310
  if (state.shape === 'circle') {
311
+ ctx.beginPath(); ctx.arc(x*s + s/2, y*s + s/2, s/2 - 1, 0, Math.PI*2); ctx.fill();
 
 
 
312
  } else {
 
313
  ctx.fillRect(x*s + 0.5, y*s + 0.5, s - 1, s - 1);
314
  }
315
+ // 显示色号
316
+ if(document.getElementById('showIds').checked && s > 22) {
 
317
  ctx.fillStyle = getContrastColor(p.hex);
318
+ ctx.font = `bold ${Math.floor(s*0.3)}px Arial`;
319
+ ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
 
320
  ctx.fillText(p.id, x*s + s/2, y*s + s/2);
321
  }
322
  }
323
 
324
+ // 3. 绘制大粗线 (每10格)
325
+ ctx.beginPath(); ctx.lineWidth = 2; ctx.strokeStyle = '#555';
 
 
326
  for(let i=0; i<=state.gridW; i+=10) { ctx.moveTo(i*s, 0); ctx.lineTo(i*s, state.gridH*s); }
327
  for(let i=0; i<=state.gridH; i+=10) { ctx.moveTo(0, i*s); ctx.lineTo(state.gridW*s, i*s); }
328
  ctx.stroke();
329
 
330
+ // 4. 绘制边界
331
+ ctx.strokeStyle = '#6366f1'; ctx.lineWidth = 2;
 
332
  ctx.strokeRect(0, 0, state.gridW*s, state.gridH*s);
 
333
  ctx.restore();
334
 
335
+ // 5. 绘制标尺
336
  drawRulers();
337
+ // 6. 每次绘图后更新材料清单
338
+ updateMaterialList();
339
  }
340
 
341
  function drawRulers() {
342
+ const s = state.scale; const ox = state.offsetX; const oy = state.offsetY;
343
+ // Top Ruler
344
+ const ctxT = ctxRTop; ctxT.clearRect(0,0,rulerTop.width,30);
345
+ ctxT.fillStyle = '#999'; ctxT.font = '10px Arial'; ctxT.textAlign = 'center'; ctxT.beginPath(); ctxT.strokeStyle = '#555';
346
+ const startX = Math.max(0, Math.floor(-ox / s)); const endX = Math.min(state.gridW, Math.ceil((cvs.width - ox) / s));
 
 
 
 
 
 
 
 
 
 
347
  for(let i=startX; i<=endX; i++) {
348
+ const sx = ox + i * s;
349
+ if(i % 10 === 0) { ctxT.moveTo(sx, 15); ctxT.lineTo(sx, 30); ctxT.fillText(i, sx, 12); }
350
+ else if (i % 5 === 0 && s>10) { ctxT.moveTo(sx, 22); ctxT.lineTo(sx, 30); }
 
 
 
 
 
 
 
351
  }
352
  ctxT.stroke();
353
+ // Left Ruler
354
+ const ctxL = ctxRLeft; ctxL.clearRect(0,0,30,rulerLeft.height);
355
+ ctxL.fillStyle = '#999'; ctxL.font = '10px Arial'; ctxL.textAlign = 'center'; ctxL.textBaseline = 'middle'; ctxL.beginPath(); ctxL.strokeStyle = '#555';
356
+ const startY = Math.max(0, Math.floor(-oy / s)); const endY = Math.min(state.gridH, Math.ceil((cvs.height - oy) / s));
 
 
 
 
 
 
357
  for(let i=startY; i<=endY; i++) {
358
+ const sy = oy + i * s;
359
  if(i % 10 === 0) {
360
+ ctxL.moveTo(15, sy); ctxL.lineTo(30, sy);
361
+ ctxL.save(); ctxL.translate(12, sy); ctxL.rotate(-Math.PI/2); ctxL.fillText(i, 0, 0); ctxL.restore();
362
+ } else if (i % 5 === 0 && s>10) { ctxL.moveTo(22, sy); ctxL.lineTo(30, sy); }
 
 
 
 
 
 
 
 
 
363
  }
364
  ctxL.stroke();
365
  }
 
372
  // --- 交互逻辑 ---
373
  viewport.onmousedown = e => {
374
  if(state.tool === 'move' || e.button === 1) {
375
+ state.isDrag = true; state.lastX = e.clientX; state.lastY = e.clientY; return;
 
376
  }
377
+ paint(e); viewport.onmousemove = paint;
 
378
  };
379
  window.onmouseup = () => { state.isDrag = false; viewport.onmousemove = null; };
380
  window.onmousemove = e => {
381
  if(state.isDrag) {
382
+ state.offsetX += e.clientX - state.lastX; state.offsetY += e.clientY - state.lastY;
383
+ state.lastX = e.clientX; state.lastY = e.clientY; draw();
 
 
384
  }
385
  updateInfo(e);
386
  };
 
389
  const rect = viewport.getBoundingClientRect();
390
  const x = Math.floor((e.clientX - rect.left - state.offsetX) / state.scale);
391
  const y = Math.floor((e.clientY - rect.top - state.offsetY) / state.scale);
 
392
  if(x<0 || x>=state.gridW || y<0 || y>=state.gridH) return;
 
393
  const key = `${x},${y}`;
394
  if(state.tool === 'pencil') state.pixels[key] = state.curColorIdx;
395
  else if(state.tool === 'eraser') delete state.pixels[key];
 
404
  function floodFill(x, y, targetIdx) {
405
  const oldIdx = state.pixels[`${x},${y}`];
406
  if(oldIdx === targetIdx) return;
407
+ const q = [[x,y]]; let count = 0;
 
 
408
  while(q.length && count < state.gridW * state.gridH) {
409
+ count++; const [cx, cy] = q.pop(); const k = `${cx},${cy}`;
 
 
410
  if(cx<0 || cx>=state.gridW || cy<0 || cy>=state.gridH) continue;
411
  if(state.pixels[k] !== oldIdx) continue;
412
  state.pixels[k] = targetIdx;
 
422
  viewport.style.cursor = t==='move' ? 'grab' : 'crosshair';
423
  }
424
  function resizeGrid() {
425
+ state.gridW = parseInt(document.getElementById('gridW').value) || 50;
426
+ state.gridH = parseInt(document.getElementById('gridH').value) || 50;
427
  draw();
428
  }
429
  function updateInfo(e) {
 
432
  const rect = viewport.getBoundingClientRect();
433
  const x = Math.floor((e.clientX - rect.left - state.offsetX) / state.scale);
434
  const y = Math.floor((e.clientY - rect.top - state.offsetY) / state.scale);
435
+ if(x>=0 && x<state.gridW && y>=0 && y<state.gridH) document.getElementById('info-coord').innerText = `XY: (${x}, ${y})`;
 
436
  }
437
  }
438
  function clearCanvas() { if(confirm('确定清空画布吗?')) { state.pixels={}; draw(); } }
439
 
440
  function exportImage() {
 
441
  const expCvs = document.createElement('canvas');
442
+ const unit = 40;
443
+ expCvs.width = state.gridW * unit; expCvs.height = state.gridH * unit;
 
444
  const eCtx = expCvs.getContext('2d');
445
+ eCtx.fillStyle = 'white'; eCtx.fillRect(0, 0, expCvs.width, expCvs.height);
 
 
 
 
 
446
  for(let key in state.pixels) {
447
  const [x, y] = key.split(',').map(Number);
448
  const p = PALETTE[state.pixels[key]];
449
  eCtx.fillStyle = p.hex;
 
450
  if(state.shape === 'circle') {
451
+ eCtx.beginPath(); eCtx.arc(x*unit + unit/2, y*unit + unit/2, unit/2 - 2, 0, Math.PI*2); eCtx.fill();
452
+ eCtx.fillStyle = '#fff'; eCtx.beginPath(); eCtx.arc(x*unit + unit/2, y*unit + unit/2, unit/8, 0, Math.PI*2); eCtx.fill();
453
+ } else { eCtx.fillRect(x*unit, y*unit, unit, unit); }
 
 
 
 
 
 
 
 
 
 
454
  if(state.shape === 'circle') {
455
+ eCtx.fillStyle = getContrastColor(p.hex) === '#fff' ? '#000' : '#fff';
456
+ eCtx.font = 'bold 10px Arial'; eCtx.textAlign = 'center';
 
457
  eCtx.fillText(p.id, x*unit + unit/2, y*unit + unit - 5);
458
  }
459
  }
460
+ eCtx.strokeStyle = '#ccc'; eCtx.lineWidth = 1;
 
 
 
461
  for(let i=0; i<=state.gridW; i++) { eCtx.moveTo(i*unit, 0); eCtx.lineTo(i*unit, expCvs.height); }
462
  for(let i=0; i<=state.gridH; i++) { eCtx.moveTo(0, i*unit); eCtx.lineTo(expCvs.width, i*unit); }
463
  eCtx.stroke();
464
+ const link = document.createElement('a'); link.download = '拼豆图纸_v4.png'; link.href = expCvs.toDataURL(); link.click();
 
 
 
 
465
  }
466
 
467
+ // --- 裁剪逻辑复用 (v4.0 包含色彩融合) ---
468
  const modal = document.getElementById('cropModal');
469
  const cropImg = document.getElementById('crop-img');
470
  const cropBox = document.getElementById('crop-box');
 
561
  }
562
  }
563
  closeModal();
564
+ resizeAll();
565
  }
566
 
567
  init();