limaodada commited on
Commit
6a5bf2b
·
verified ·
1 Parent(s): 0154c3a

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +120 -176
index.html CHANGED
@@ -2,7 +2,7 @@
2
  <html lang="zh-CN">
3
  <head>
4
  <meta charset="UTF-8">
5
- <title>BeadMaster Pro v5.0 - 1:1打印商业版</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, sans-serif; user-select: none; }
@@ -54,37 +54,56 @@
54
  .list-count { font-family: monospace; font-weight: bold; color: var(--accent); }
55
  #total-stats { padding: 12px; border-top: 1px solid var(--border); background: #18181b; font-size: 12px; display: flex; justify-content: space-between; font-weight: bold; }
56
 
57
- /* 裁剪弹窗 (保持不变) */
58
  .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; }
59
  .modal-content { background: var(--panel); padding: 20px; border-radius: 8px; width: 80vw; height: 80vh; display: flex; flex-direction: column; gap: 10px; border: 1px solid var(--border); }
60
  #crop-container { flex: 1; background: #000; position: relative; overflow: hidden; display: flex; justify-content: center; align-items: center; border: 1px solid #333; }
61
  #crop-img { max-width: 100%; max-height: 100%; display: block; box-shadow: 0 0 20px rgba(0,0,0,0.5); }
62
  #crop-box { position: absolute; border: 2px dashed #fff; box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5); cursor: move; display: none; }
63
  .resize-handle { position: absolute; right: -6px; bottom: -6px; width: 12px; height: 12px; background: var(--accent); cursor: nwse-resize; border: 1px solid white; }
 
 
 
 
 
 
 
64
  </style>
65
  </head>
66
  <body>
67
 
68
  <div class="modal-overlay" id="cropModal">
69
  <div class="modal-content">
70
- <div style="display:flex; justify-content:space-between; color:white; align-items: center;">
71
- <h3 style="margin:0;">✂️ 导入裁剪</h3>
72
- <div style="display:flex; gap:10px; align-items:center;">
73
- <label style="font-size:13px; cursor:pointer; display:flex; align-items:center; gap:5px;">
74
- <input type="checkbox" id="ditherCheck" checked>
75
- <span>色彩融合 (Dithering)</span>
76
- </label>
77
- <button class="action-btn primary" onclick="confirmCrop()">✅ 确认</button>
78
- <button class="action-btn" onclick="closeModal()">取消</button>
79
- </div>
80
- </div>
81
  <div id="crop-container" onmousedown="startCropDrag(event)">
82
  <img id="crop-img">
83
  <div id="crop-box">
84
  <div class="resize-handle" onmousedown="startCropResize(event)"></div>
85
  </div>
86
  </div>
87
- <div style="color:#888; font-size:12px;">提示:调整白色选框来选择区域。</div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  </div>
89
  </div>
90
 
@@ -108,7 +127,7 @@
108
  <div style="width:1px; height:20px; background:var(--border);"></div>
109
 
110
  <div>
111
- <div class="label">拼豆规格 (打印用)</div>
112
  <select id="beadSizeSelect">
113
  <option value="5">5mm (标准豆)</option>
114
  <option value="2.6">2.6mm (迷你豆)</option>
@@ -116,11 +135,11 @@
116
  </div>
117
 
118
  <div>
119
- <div class="label">画布格数</div>
120
  <div style="display:flex; align-items:center; gap:2px;">
121
- <input type="number" id="gridW" value="50" onchange="resizeGrid()" title="横向格数">
122
  <span style="font-size:10px; color:#666">x</span>
123
- <input type="number" id="gridH" value="50" onchange="resizeGrid()" title="纵向格数">
124
  </div>
125
  </div>
126
 
@@ -138,7 +157,7 @@
138
  <option value="square">⬛ 方块</option>
139
  </select>
140
 
141
- <button class="action-btn" onclick="exportImage()">💾 1:1 导出</button>
142
  <button class="action-btn" onclick="clearCanvas()">🗑️</button>
143
  </div>
144
 
@@ -193,7 +212,7 @@ const PALETTE = [
193
  const state = {
194
  gridW: 50, gridH: 50,
195
  pixels: {},
196
- scale: 25, // 视觉大小 (px per bead)
197
  offsetX: 50, offsetY: 50,
198
  tool: 'pencil',
199
  curColorIdx: 2,
@@ -219,8 +238,6 @@ function init() {
219
  resizeAll();
220
  updateMaterialList();
221
  draw();
222
-
223
- // 滚轮缩放
224
  viewport.addEventListener('wheel', e => {
225
  e.preventDefault();
226
  const delta = e.deltaY > 0 ? 0.9 : 1.1;
@@ -229,32 +246,23 @@ function init() {
229
  }
230
 
231
  function handleZoomSlider(val) {
232
- // 滑块控制缩放 (中心点缩放)
233
  const rect = viewport.getBoundingClientRect();
234
- const cx = rect.left + rect.width/2;
235
- const cy = rect.top + rect.height/2;
236
- applyZoom(parseInt(val), cx, cy);
237
  }
238
 
239
- function applyZoom(newScale, centerClientX, centerClientY) {
240
  const oldScale = state.scale;
241
  state.scale = Math.max(5, Math.min(100, newScale));
242
-
243
- // 更新滑块UI
244
  zoomSlider.value = state.scale;
245
-
246
- // 维持鼠标/中心点位置
247
  const rect = viewport.getBoundingClientRect();
248
- const mx = centerClientX - rect.left;
249
- const my = centerClientY - rect.top;
250
  state.offsetX = mx - (mx - state.offsetX) * (state.scale / oldScale);
251
  state.offsetY = my - (my - state.offsetY) * (state.scale / oldScale);
252
-
253
  updateInfo();
254
  draw();
255
  }
256
 
257
- // --- 列表逻辑 ---
258
  function updateMaterialList() {
259
  const counts = {};
260
  let total = 0;
@@ -265,7 +273,6 @@ function updateMaterialList() {
265
  }
266
  materialListEl.innerHTML = '';
267
  totalBeadsEl.innerText = `${total} 颗`;
268
-
269
  PALETTE.forEach((p, idx) => {
270
  const count = counts[idx] || 0;
271
  if(count > 0 || idx === state.curColorIdx) {
@@ -283,26 +290,20 @@ function updateMaterialList() {
283
  }
284
 
285
  function selectColor(idx) {
286
- state.curColorIdx = idx;
287
- updateMaterialList();
288
- setTool('pencil');
289
  }
290
 
291
  function copyList() {
292
  let text = "--- 拼豆材料清单 ---\n";
293
  const counts = {};
294
  for(let key in state.pixels) counts[state.pixels[key]] = (counts[state.pixels[key]] || 0) + 1;
295
- PALETTE.forEach((p, idx) => {
296
- if(counts[idx]) text += `[${p.id}] ${p.name}: ${counts[idx]}颗\n`;
297
- });
298
  text += `总计: ${Object.values(counts).reduce((a,b)=>a+b, 0)} 颗`;
299
  navigator.clipboard.writeText(text).then(() => alert('已复制!'));
300
  }
301
 
302
- // --- 绘图 ---
303
  function resizeAll() {
304
- const vw = viewport.clientWidth;
305
- const vh = viewport.clientHeight;
306
  cvs.width = vw; cvs.height = vh;
307
  rulerTop.width = vw; rulerTop.height = 30;
308
  rulerLeft.width = 30; rulerLeft.height = vh;
@@ -313,11 +314,8 @@ function draw() {
313
  state.shape = document.getElementById('shapeSelect').value;
314
  const s = state.scale; const ox = state.offsetX; const oy = state.offsetY;
315
 
316
- ctx.fillStyle = '#121214';
317
- ctx.fillRect(0, 0, cvs.width, cvs.height);
318
-
319
- ctx.save();
320
- ctx.translate(ox, oy);
321
 
322
  // 网格
323
  ctx.beginPath(); ctx.lineWidth = 1; ctx.strokeStyle = '#2a2a2d';
@@ -333,42 +331,31 @@ function draw() {
333
  if(!p) continue;
334
  ctx.fillStyle = p.hex;
335
  if (state.shape === 'circle') {
336
- ctx.beginPath(); ctx.arc(x*s + s/2, y*s + s/2, s/2 - 1, 0, Math.PI*2); ctx.fill();
337
- if(showId) {
338
- // 孔
339
- ctx.fillStyle = 'rgba(255,255,255,0.3)';
340
- ctx.beginPath(); ctx.arc(x*s + s/2, y*s + s/2, s/6, 0, Math.PI*2); ctx.fill();
341
- }
342
- } else {
343
- ctx.fillRect(x*s + 0.5, y*s + 0.5, s - 1, s - 1);
344
- }
345
 
346
  if(showId) {
347
- ctx.fillStyle = getContrastColor(p.hex);
348
- ctx.font = `bold ${Math.floor(s*0.35)}px Arial`;
349
- ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
350
  ctx.fillText(p.id, x*s + s/2, y*s + s/2);
351
  }
352
  }
353
 
354
- // 大格线
355
  ctx.beginPath(); ctx.lineWidth = 2; ctx.strokeStyle = '#555';
356
  for(let i=0; i<=state.gridW; i+=10) { ctx.moveTo(i*s, 0); ctx.lineTo(i*s, state.gridH*s); }
357
  for(let i=0; i<=state.gridH; i+=10) { ctx.moveTo(0, i*s); ctx.lineTo(state.gridW*s, i*s); }
358
  ctx.stroke();
359
 
360
- // 边界
361
  ctx.strokeStyle = '#6366f1'; ctx.lineWidth = 2;
362
  ctx.strokeRect(0, 0, state.gridW*s, state.gridH*s);
363
  ctx.restore();
364
-
365
- drawRulers();
366
- updateMaterialList();
367
  }
368
 
369
  function drawRulers() {
370
  const s = state.scale; const ox = state.offsetX; const oy = state.offsetY;
371
- // Top
372
  const ctxT = ctxRTop; ctxT.clearRect(0,0,rulerTop.width,30);
373
  ctxT.fillStyle = '#999'; ctxT.font = '10px Arial'; ctxT.textAlign = 'center'; ctxT.beginPath(); ctxT.strokeStyle = '#555';
374
  const startX = Math.max(0, Math.floor(-ox / s)); const endX = Math.min(state.gridW, Math.ceil((cvs.width - ox) / s));
@@ -378,7 +365,6 @@ function drawRulers() {
378
  else if (i % 5 === 0 && s>10) { ctxT.moveTo(sx, 22); ctxT.lineTo(sx, 30); }
379
  }
380
  ctxT.stroke();
381
- // Left
382
  const ctxL = ctxRLeft; ctxL.clearRect(0,0,30,rulerLeft.height);
383
  ctxL.fillStyle = '#999'; ctxL.font = '10px Arial'; ctxL.textAlign = 'center'; ctxL.textBaseline = 'middle'; ctxL.beginPath(); ctxL.strokeStyle = '#555';
384
  const startY = Math.max(0, Math.floor(-oy / s)); const endY = Math.min(state.gridH, Math.ceil((cvs.height - oy) / s));
@@ -397,22 +383,9 @@ function getContrastColor(hex) {
397
  return (r*0.299 + g*0.587 + b*0.114) > 128 ? '#000' : '#fff';
398
  }
399
 
400
- // --- 交互 ---
401
- viewport.onmousedown = e => {
402
- if(state.tool === 'move' || e.button === 1) {
403
- state.isDrag = true; state.lastX = e.clientX; state.lastY = e.clientY; return;
404
- }
405
- paint(e); viewport.onmousemove = paint;
406
- };
407
  window.onmouseup = () => { state.isDrag = false; viewport.onmousemove = null; };
408
- window.onmousemove = e => {
409
- if(state.isDrag) {
410
- state.offsetX += e.clientX - state.lastX; state.offsetY += e.clientY - state.lastY;
411
- state.lastX = e.clientX; state.lastY = e.clientY; draw();
412
- }
413
- updateInfo(e);
414
- };
415
-
416
  function paint(e) {
417
  const rect = viewport.getBoundingClientRect();
418
  const x = Math.floor((e.clientX - rect.left - state.offsetX) / state.scale);
@@ -422,74 +395,64 @@ function paint(e) {
422
  if(state.tool === 'pencil') state.pixels[key] = state.curColorIdx;
423
  else if(state.tool === 'eraser') delete state.pixels[key];
424
  else if(state.tool === 'bucket') floodFill(x, y, state.curColorIdx);
425
- else if(state.tool === 'picker') {
426
- if(state.pixels[key] !== undefined) selectColor(state.pixels[key]);
427
- setTool('pencil');
428
- }
429
  draw();
430
  }
431
-
432
  function floodFill(x, y, targetIdx) {
433
- const oldIdx = state.pixels[`${x},${y}`];
434
- if(oldIdx === targetIdx) return;
435
  const q = [[x,y]]; let count = 0;
436
  while(q.length && count < state.gridW * state.gridH) {
437
  count++; const [cx, cy] = q.pop(); const k = `${cx},${cy}`;
438
  if(cx<0 || cx>=state.gridW || cy<0 || cy>=state.gridH) continue;
439
  if(state.pixels[k] !== oldIdx) continue;
440
- state.pixels[k] = targetIdx;
441
- q.push([cx+1,cy], [cx-1,cy], [cx,cy+1], [cx,cy-1]);
442
  }
443
  draw();
444
  }
445
-
446
- function setTool(t) {
447
- state.tool = t;
448
- document.querySelectorAll('.tool-btn').forEach(b => b.classList.remove('active'));
449
- document.getElementById(`t-${t}`).classList.add('active');
450
- viewport.style.cursor = t==='move' ? 'grab' : 'crosshair';
451
- }
452
- function resizeGrid() {
453
- state.gridW = parseInt(document.getElementById('gridW').value) || 50;
454
- state.gridH = parseInt(document.getElementById('gridH').value) || 50;
455
- draw();
456
- }
457
- function updateInfo(e) {
458
- document.getElementById('info-zoom').innerText = `Zoom: ${state.scale}px`;
459
- if(e) {
460
- const rect = viewport.getBoundingClientRect();
461
- const x = Math.floor((e.clientX - rect.left - state.offsetX) / state.scale);
462
- const y = Math.floor((e.clientY - rect.top - state.offsetY) / state.scale);
463
- if(x>=0 && x<state.gridW && y>=0 && y<state.gridH) document.getElementById('info-coord').innerText = `XY: (${x}, ${y})`;
464
- }
465
- }
466
  function clearCanvas() { if(confirm('确定清空画布吗?')) { state.pixels={}; draw(); } }
467
 
468
- // --- 1:1 导出逻辑 ---
469
- function exportImage() {
470
  const beadSizeMM = parseFloat(document.getElementById('beadSizeSelect').value);
471
-
472
- // 假设打印分辨率为 72 DPI (Web标准)
473
- // 1 inch = 25.4 mm
474
- // 1 px = 25.4 / 72 = 0.3527 mm
475
- // 所以 1 mm = 2.835 px
476
- // 5mm 豆子 = 5 * 2.835 = 14.17 px (这是 72DPI 下的尺寸,如果为了高清打印,我们通常放大倍数)
477
-
478
- // 为了保证打印清晰,我们设定导出为 300 DPI 级别
479
- // 300 DPI 下,1mm ≈ 11.8 px
480
- const pxPerMM = 11.8;
481
- const unit = Math.round(beadSizeMM * pxPerMM); // 每个豆子的像素大小
482
-
483
  const expCvs = document.createElement('canvas');
484
- expCvs.width = state.gridW * unit;
485
- expCvs.height = state.gridH * unit;
486
  const eCtx = expCvs.getContext('2d');
487
 
488
- // 白底
489
- eCtx.fillStyle = 'white';
490
- eCtx.fillRect(0, 0, expCvs.width, expCvs.height);
491
 
492
- // 绘制内容
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
493
  for(let key in state.pixels) {
494
  const [x, y] = key.split(',').map(Number);
495
  const p = PALETTE[state.pixels[key]];
@@ -498,31 +461,27 @@ function exportImage() {
498
  if(state.shape === 'circle') {
499
  eCtx.beginPath(); eCtx.arc(x*unit + unit/2, y*unit + unit/2, unit/2 - unit*0.05, 0, Math.PI*2); eCtx.fill();
500
  eCtx.fillStyle = '#fff'; eCtx.beginPath(); eCtx.arc(x*unit + unit/2, y*unit + unit/2, unit*0.15, 0, Math.PI*2); eCtx.fill();
501
- } else {
502
- eCtx.fillRect(x*unit, y*unit, unit, unit);
503
- }
504
 
505
- // 导出文字 (颜色编号)
506
- eCtx.fillStyle = getContrastColor(p.hex) === '#fff' ? '#000' : '#fff';
507
  eCtx.font = `bold ${unit*0.35}px Arial`; eCtx.textAlign = 'center'; eCtx.textBaseline = 'middle';
508
  eCtx.fillText(p.id, x*unit + unit/2, y*unit + unit/2);
509
  }
510
 
511
  // 网格线
512
- eCtx.strokeStyle = '#ddd'; eCtx.lineWidth = 2;
513
- for(let i=0; i<=state.gridW; i++) { eCtx.moveTo(i*unit, 0); eCtx.lineTo(i*unit, expCvs.height); }
514
- for(let i=0; i<=state.gridH; i++) { eCtx.moveTo(0, i*unit); eCtx.lineTo(expCvs.width, i*unit); }
515
  eCtx.stroke();
516
 
517
  const link = document.createElement('a');
518
- link.download = `拼豆图纸_${beadSizeMM}mm规格.png`;
519
  link.href = expCvs.toDataURL();
520
  link.click();
521
-
522
- alert(`已生成适合 300DPI 打印的 ${beadSizeMM}mm 规格图纸。\n请在打印时选择“原图大小”或 100% 缩放。`);
523
  }
524
 
525
- // --- 裁剪逻辑复用 (保持) ---
526
  const modal = document.getElementById('cropModal');
527
  const cropImg = document.getElementById('crop-img');
528
  const cropBox = document.getElementById('crop-box');
@@ -538,6 +497,8 @@ function handleFile(input) {
538
  cropBox.style.display = 'block';
539
  cropBox.style.left = '50px'; cropBox.style.top = '50px';
540
  cropBox.style.width = '100px'; cropBox.style.height = '100px';
 
 
541
  }
542
  };
543
  reader.readAsDataURL(input.files[0]);
@@ -548,24 +509,14 @@ function startCropDrag(e) {
548
  if(e.target.classList.contains('resize-handle')) return;
549
  const rect = cropBox.getBoundingClientRect();
550
  if(e.clientX>rect.left && e.clientX<rect.right && e.clientY>rect.top && e.clientY<rect.bottom) {
551
- cropState.isDrag = true;
552
- cropState.startX = e.clientX - cropBox.offsetLeft;
553
- cropState.startY = e.clientY - cropBox.offsetTop;
554
- document.onmousemove = e => {
555
- cropBox.style.left = (e.clientX - cropState.startX) + 'px';
556
- cropBox.style.top = (e.clientY - cropState.startY) + 'px';
557
- };
558
  document.onmouseup = () => { document.onmousemove=null; };
559
  }
560
  }
561
  function startCropResize(e) {
562
- e.stopPropagation();
563
- const startX = e.clientX; const startY = e.clientY;
564
- const startW = cropBox.offsetWidth; const startH = cropBox.offsetHeight;
565
- document.onmousemove = e => {
566
- cropBox.style.width = (startW + e.clientX - startX) + 'px';
567
- cropBox.style.height = (startH + e.clientY - startY) + 'px';
568
- };
569
  document.onmouseup = () => { document.onmousemove=null; };
570
  }
571
  function confirmCrop() {
@@ -575,22 +526,21 @@ function confirmCrop() {
575
  const cw = cropBox.offsetWidth * factor;
576
  const ch = cropBox.offsetHeight * factor;
577
 
578
- const targetW = parseInt(document.getElementById('gridW').value);
 
579
  const targetH = Math.floor(targetW * (ch / cw));
 
 
 
580
  document.getElementById('gridH').value = targetH;
581
  state.gridW = targetW; state.gridH = targetH;
582
 
583
- const tmpCv = document.createElement('canvas');
584
- tmpCv.width = targetW; tmpCv.height = targetH;
585
  const tmpCtx = tmpCv.getContext('2d');
586
  tmpCtx.drawImage(cropImg, cx, cy, cw, ch, 0, 0, targetW, targetH);
587
  const data = tmpCtx.getImageData(0, 0, targetW, targetH).data;
588
 
589
- const paletteRGB = PALETTE.map(p => {
590
- const v = parseInt(p.hex.slice(1), 16);
591
- return [(v>>16)&0xff, (v>>8)&0xff, v&0xff];
592
- });
593
-
594
  state.pixels = {};
595
  const useDither = document.getElementById('ditherCheck').checked;
596
 
@@ -608,18 +558,12 @@ function confirmCrop() {
608
  if(useDither) {
609
  const [nr, ng, nb] = paletteRGB[bestP];
610
  const er = oldR - nr, eg = oldG - ng, eb = oldB - nb;
611
- function addErr(dx, dy, f) {
612
- if(x+dx>=0 && x+dx<targetW && y+dy<targetH) {
613
- const nIdx = ((y+dy)*targetW + (x+dx))*4;
614
- data[nIdx] += er * f; data[nIdx+1] += eg * f; data[nIdx+2] += eb * f;
615
- }
616
- }
617
  addErr(1, 0, 7/16); addErr(-1, 1, 3/16); addErr(0, 1, 5/16); addErr(1, 1, 1/16);
618
  }
619
  }
620
  }
621
- closeModal();
622
- resizeAll();
623
  }
624
 
625
  init();
 
2
  <html lang="zh-CN">
3
  <head>
4
  <meta charset="UTF-8">
5
+ <title>BeadMaster Pro v6.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, sans-serif; user-select: none; }
 
54
  .list-count { font-family: monospace; font-weight: bold; color: var(--accent); }
55
  #total-stats { padding: 12px; border-top: 1px solid var(--border); background: #18181b; font-size: 12px; display: flex; justify-content: space-between; font-weight: bold; }
56
 
57
+ /* 裁剪弹窗 */
58
  .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; }
59
  .modal-content { background: var(--panel); padding: 20px; border-radius: 8px; width: 80vw; height: 80vh; display: flex; flex-direction: column; gap: 10px; border: 1px solid var(--border); }
60
  #crop-container { flex: 1; background: #000; position: relative; overflow: hidden; display: flex; justify-content: center; align-items: center; border: 1px solid #333; }
61
  #crop-img { max-width: 100%; max-height: 100%; display: block; box-shadow: 0 0 20px rgba(0,0,0,0.5); }
62
  #crop-box { position: absolute; border: 2px dashed #fff; box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5); cursor: move; display: none; }
63
  .resize-handle { position: absolute; right: -6px; bottom: -6px; width: 12px; height: 12px; background: var(--accent); cursor: nwse-resize; border: 1px solid white; }
64
+
65
+ /* 弹窗底部控制栏 */
66
+ .modal-footer { display:flex; justify-content:space-between; color:white; align-items: center; background: #2b2b2e; padding: 10px; border-radius: 6px; }
67
+ .control-group { display: flex; gap: 15px; align-items: center; }
68
+ .input-group { display: flex; flex-direction: column; gap: 2px; }
69
+ .input-label { font-size: 11px; color: #aaa; }
70
+ .input-field { background: #18181b; border: 1px solid #555; color: white; padding: 4px 8px; border-radius: 4px; width: 60px; text-align: center; }
71
  </style>
72
  </head>
73
  <body>
74
 
75
  <div class="modal-overlay" id="cropModal">
76
  <div class="modal-content">
77
+ <h3 style="margin:0; color: white;">✂️ 图片导入与采样设置</h3>
78
+
 
 
 
 
 
 
 
 
 
79
  <div id="crop-container" onmousedown="startCropDrag(event)">
80
  <img id="crop-img">
81
  <div id="crop-box">
82
  <div class="resize-handle" onmousedown="startCropResize(event)"></div>
83
  </div>
84
  </div>
85
+
86
+ <div class="modal-footer">
87
+ <div class="control-group">
88
+ <div class="input-group">
89
+ <span class="input-label">生成宽度 (珠子数)</span>
90
+ <input type="number" id="targetWidthInput" class="input-field" value="50" min="10" max="200" title="数值越大,细节越清晰,豆子越多">
91
+ </div>
92
+ <div class="input-group">
93
+ <span class="input-label">色彩处理</span>
94
+ <label style="font-size:13px; cursor:pointer; display:flex; align-items:center; gap:5px; margin-top:5px;">
95
+ <input type="checkbox" id="ditherCheck" checked>
96
+ <span>开启融合 (Dithering)</span>
97
+ </label>
98
+ </div>
99
+ </div>
100
+
101
+ <div class="control-group">
102
+ <div style="font-size:12px; color:#aaa; max-width: 200px;">💡 提示: 觉得转换后太粗?请调大"生成宽度"。拖拽白框选择区域。</div>
103
+ <button class="action-btn primary" onclick="confirmCrop()" style="padding: 8px 20px; font-size: 14px;">✅ 开始转换</button>
104
+ <button class="action-btn" onclick="closeModal()">取消</button>
105
+ </div>
106
+ </div>
107
  </div>
108
  </div>
109
 
 
127
  <div style="width:1px; height:20px; background:var(--border);"></div>
128
 
129
  <div>
130
+ <div class="label">打印规格</div>
131
  <select id="beadSizeSelect">
132
  <option value="5">5mm (标准豆)</option>
133
  <option value="2.6">2.6mm (迷你豆)</option>
 
135
  </div>
136
 
137
  <div>
138
+ <div class="label">画布尺寸 (珠)</div>
139
  <div style="display:flex; align-items:center; gap:2px;">
140
+ <input type="number" id="gridW" value="50" onchange="resizeGrid()">
141
  <span style="font-size:10px; color:#666">x</span>
142
+ <input type="number" id="gridH" value="50" onchange="resizeGrid()">
143
  </div>
144
  </div>
145
 
 
157
  <option value="square">⬛ 方块</option>
158
  </select>
159
 
160
+ <button class="action-btn" onclick="exportImageWithRuler()">💾 导出带标尺图纸</button>
161
  <button class="action-btn" onclick="clearCanvas()">🗑️</button>
162
  </div>
163
 
 
212
  const state = {
213
  gridW: 50, gridH: 50,
214
  pixels: {},
215
+ scale: 25,
216
  offsetX: 50, offsetY: 50,
217
  tool: 'pencil',
218
  curColorIdx: 2,
 
238
  resizeAll();
239
  updateMaterialList();
240
  draw();
 
 
241
  viewport.addEventListener('wheel', e => {
242
  e.preventDefault();
243
  const delta = e.deltaY > 0 ? 0.9 : 1.1;
 
246
  }
247
 
248
  function handleZoomSlider(val) {
 
249
  const rect = viewport.getBoundingClientRect();
250
+ applyZoom(parseInt(val), rect.left + rect.width/2, rect.top + rect.height/2);
 
 
251
  }
252
 
253
+ function applyZoom(newScale, cx, cy) {
254
  const oldScale = state.scale;
255
  state.scale = Math.max(5, Math.min(100, newScale));
 
 
256
  zoomSlider.value = state.scale;
 
 
257
  const rect = viewport.getBoundingClientRect();
258
+ const mx = cx - rect.left;
259
+ const my = cy - rect.top;
260
  state.offsetX = mx - (mx - state.offsetX) * (state.scale / oldScale);
261
  state.offsetY = my - (my - state.offsetY) * (state.scale / oldScale);
 
262
  updateInfo();
263
  draw();
264
  }
265
 
 
266
  function updateMaterialList() {
267
  const counts = {};
268
  let total = 0;
 
273
  }
274
  materialListEl.innerHTML = '';
275
  totalBeadsEl.innerText = `${total} 颗`;
 
276
  PALETTE.forEach((p, idx) => {
277
  const count = counts[idx] || 0;
278
  if(count > 0 || idx === state.curColorIdx) {
 
290
  }
291
 
292
  function selectColor(idx) {
293
+ state.curColorIdx = idx; updateMaterialList(); setTool('pencil');
 
 
294
  }
295
 
296
  function copyList() {
297
  let text = "--- 拼豆材料清单 ---\n";
298
  const counts = {};
299
  for(let key in state.pixels) counts[state.pixels[key]] = (counts[state.pixels[key]] || 0) + 1;
300
+ PALETTE.forEach((p, idx) => { if(counts[idx]) text += `[${p.id}] ${p.name}: ${counts[idx]}颗\n`; });
 
 
301
  text += `总计: ${Object.values(counts).reduce((a,b)=>a+b, 0)} 颗`;
302
  navigator.clipboard.writeText(text).then(() => alert('已复制!'));
303
  }
304
 
 
305
  function resizeAll() {
306
+ const vw = viewport.clientWidth; const vh = viewport.clientHeight;
 
307
  cvs.width = vw; cvs.height = vh;
308
  rulerTop.width = vw; rulerTop.height = 30;
309
  rulerLeft.width = 30; rulerLeft.height = vh;
 
314
  state.shape = document.getElementById('shapeSelect').value;
315
  const s = state.scale; const ox = state.offsetX; const oy = state.offsetY;
316
 
317
+ ctx.fillStyle = '#121214'; ctx.fillRect(0, 0, cvs.width, cvs.height);
318
+ ctx.save(); ctx.translate(ox, oy);
 
 
 
319
 
320
  // 网格
321
  ctx.beginPath(); ctx.lineWidth = 1; ctx.strokeStyle = '#2a2a2d';
 
331
  if(!p) continue;
332
  ctx.fillStyle = p.hex;
333
  if (state.shape === 'circle') {
334
+ ctx.beginPath(); ctx.arc(x*s + s/2, y*s + s/2, s/2 - s*0.05, 0, Math.PI*2); ctx.fill();
335
+ if(showId) { ctx.fillStyle = 'rgba(255,255,255,0.3)'; ctx.beginPath(); ctx.arc(x*s + s/2, y*s + s/2, s/6, 0, Math.PI*2); ctx.fill(); }
336
+ } else { ctx.fillRect(x*s + 0.5, y*s + 0.5, s - 1, s - 1); }
 
 
 
 
 
 
337
 
338
  if(showId) {
339
+ ctx.fillStyle = getContrastColor(p.hex) === '#fff' ? '#fff' : '#000';
340
+ ctx.font = `bold ${Math.floor(s*0.35)}px Arial`; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
 
341
  ctx.fillText(p.id, x*s + s/2, y*s + s/2);
342
  }
343
  }
344
 
345
+ // 粗线
346
  ctx.beginPath(); ctx.lineWidth = 2; ctx.strokeStyle = '#555';
347
  for(let i=0; i<=state.gridW; i+=10) { ctx.moveTo(i*s, 0); ctx.lineTo(i*s, state.gridH*s); }
348
  for(let i=0; i<=state.gridH; i+=10) { ctx.moveTo(0, i*s); ctx.lineTo(state.gridW*s, i*s); }
349
  ctx.stroke();
350
 
 
351
  ctx.strokeStyle = '#6366f1'; ctx.lineWidth = 2;
352
  ctx.strokeRect(0, 0, state.gridW*s, state.gridH*s);
353
  ctx.restore();
354
+ drawRulers(); updateMaterialList();
 
 
355
  }
356
 
357
  function drawRulers() {
358
  const s = state.scale; const ox = state.offsetX; const oy = state.offsetY;
 
359
  const ctxT = ctxRTop; ctxT.clearRect(0,0,rulerTop.width,30);
360
  ctxT.fillStyle = '#999'; ctxT.font = '10px Arial'; ctxT.textAlign = 'center'; ctxT.beginPath(); ctxT.strokeStyle = '#555';
361
  const startX = Math.max(0, Math.floor(-ox / s)); const endX = Math.min(state.gridW, Math.ceil((cvs.width - ox) / s));
 
365
  else if (i % 5 === 0 && s>10) { ctxT.moveTo(sx, 22); ctxT.lineTo(sx, 30); }
366
  }
367
  ctxT.stroke();
 
368
  const ctxL = ctxRLeft; ctxL.clearRect(0,0,30,rulerLeft.height);
369
  ctxL.fillStyle = '#999'; ctxL.font = '10px Arial'; ctxL.textAlign = 'center'; ctxL.textBaseline = 'middle'; ctxL.beginPath(); ctxL.strokeStyle = '#555';
370
  const startY = Math.max(0, Math.floor(-oy / s)); const endY = Math.min(state.gridH, Math.ceil((cvs.height - oy) / s));
 
383
  return (r*0.299 + g*0.587 + b*0.114) > 128 ? '#000' : '#fff';
384
  }
385
 
386
+ viewport.onmousedown = e => { if(state.tool === 'move' || e.button === 1) { state.isDrag = true; state.lastX = e.clientX; state.lastY = e.clientY; return; } paint(e); viewport.onmousemove = paint; };
 
 
 
 
 
 
387
  window.onmouseup = () => { state.isDrag = false; viewport.onmousemove = null; };
388
+ window.onmousemove = e => { if(state.isDrag) { state.offsetX += e.clientX - state.lastX; state.offsetY += e.clientY - state.lastY; state.lastX = e.clientX; state.lastY = e.clientY; draw(); } updateInfo(e); };
 
 
 
 
 
 
 
389
  function paint(e) {
390
  const rect = viewport.getBoundingClientRect();
391
  const x = Math.floor((e.clientX - rect.left - state.offsetX) / state.scale);
 
395
  if(state.tool === 'pencil') state.pixels[key] = state.curColorIdx;
396
  else if(state.tool === 'eraser') delete state.pixels[key];
397
  else if(state.tool === 'bucket') floodFill(x, y, state.curColorIdx);
398
+ else if(state.tool === 'picker') { if(state.pixels[key] !== undefined) selectColor(state.pixels[key]); setTool('pencil'); }
 
 
 
399
  draw();
400
  }
 
401
  function floodFill(x, y, targetIdx) {
402
+ const oldIdx = state.pixels[`${x},${y}`]; if(oldIdx === targetIdx) return;
 
403
  const q = [[x,y]]; let count = 0;
404
  while(q.length && count < state.gridW * state.gridH) {
405
  count++; const [cx, cy] = q.pop(); const k = `${cx},${cy}`;
406
  if(cx<0 || cx>=state.gridW || cy<0 || cy>=state.gridH) continue;
407
  if(state.pixels[k] !== oldIdx) continue;
408
+ state.pixels[k] = targetIdx; q.push([cx+1,cy], [cx-1,cy], [cx,cy+1], [cx,cy-1]);
 
409
  }
410
  draw();
411
  }
412
+ function setTool(t) { state.tool = t; document.querySelectorAll('.tool-btn').forEach(b => b.classList.remove('active')); document.getElementById(`t-${t}`).classList.add('active'); viewport.style.cursor = t==='move' ? 'grab' : 'crosshair'; }
413
+ function resizeGrid() { state.gridW = parseInt(document.getElementById('gridW').value) || 50; state.gridH = parseInt(document.getElementById('gridH').value) || 50; draw(); }
414
+ function updateInfo(e) { document.getElementById('info-zoom').innerText = `Zoom: ${state.scale}px`; if(e) { const rect = viewport.getBoundingClientRect(); const x = Math.floor((e.clientX - rect.left - state.offsetX) / state.scale); const y = Math.floor((e.clientY - rect.top - state.offsetY) / state.scale); if(x>=0 && x<state.gridW && y>=0 && y<state.gridH) document.getElementById('info-coord').innerText = `XY: (${x}, ${y})`; } }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
415
  function clearCanvas() { if(confirm('确定清空画布吗?')) { state.pixels={}; draw(); } }
416
 
417
+ // --- V6.0 核心修复:导出带标尺 ---
418
+ function exportImageWithRuler() {
419
  const beadSizeMM = parseFloat(document.getElementById('beadSizeSelect').value);
420
+ const pxPerMM = 11.8; // 300 DPI
421
+ const unit = Math.round(beadSizeMM * pxPerMM);
422
+ const margin = unit * 2; // 留出边缘画标尺
423
+
 
 
 
 
 
 
 
 
424
  const expCvs = document.createElement('canvas');
425
+ expCvs.width = state.gridW * unit + margin;
426
+ expCvs.height = state.gridH * unit + margin;
427
  const eCtx = expCvs.getContext('2d');
428
 
429
+ // 背景
430
+ eCtx.fillStyle = 'white'; eCtx.fillRect(0, 0, expCvs.width, expCvs.height);
 
431
 
432
+ // 绘制标尺文字
433
+ eCtx.fillStyle = '#000'; eCtx.font = `${unit*0.5}px Arial`; eCtx.textAlign = 'center'; eCtx.textBaseline = 'middle';
434
+
435
+ // 顶标尺
436
+ for(let i=0; i<=state.gridW; i++) {
437
+ if(i%5===0) {
438
+ const x = margin + i*unit;
439
+ eCtx.fillText(i, x, margin/2);
440
+ eCtx.fillRect(x, margin-unit/2, 2, unit/2);
441
+ }
442
+ }
443
+ // 左标尺
444
+ for(let i=0; i<=state.gridH; i++) {
445
+ if(i%5===0) {
446
+ const y = margin + i*unit;
447
+ eCtx.fillText(i, margin/2, y);
448
+ eCtx.fillRect(margin-unit/2, y, unit/2, 2);
449
+ }
450
+ }
451
+
452
+ // 偏移绘图区域
453
+ eCtx.translate(margin, margin);
454
+
455
+ // 绘制豆子
456
  for(let key in state.pixels) {
457
  const [x, y] = key.split(',').map(Number);
458
  const p = PALETTE[state.pixels[key]];
 
461
  if(state.shape === 'circle') {
462
  eCtx.beginPath(); eCtx.arc(x*unit + unit/2, y*unit + unit/2, unit/2 - unit*0.05, 0, Math.PI*2); eCtx.fill();
463
  eCtx.fillStyle = '#fff'; eCtx.beginPath(); eCtx.arc(x*unit + unit/2, y*unit + unit/2, unit*0.15, 0, Math.PI*2); eCtx.fill();
464
+ } else { eCtx.fillRect(x*unit, y*unit, unit, unit); }
 
 
465
 
466
+ // 文字
467
+ eCtx.fillStyle = getContrastColor(p.hex) === '#fff' ? '#fff' : '#000';
468
  eCtx.font = `bold ${unit*0.35}px Arial`; eCtx.textAlign = 'center'; eCtx.textBaseline = 'middle';
469
  eCtx.fillText(p.id, x*unit + unit/2, y*unit + unit/2);
470
  }
471
 
472
  // 网格线
473
+ eCtx.strokeStyle = '#ccc'; eCtx.lineWidth = 2;
474
+ for(let i=0; i<=state.gridW; i++) { eCtx.moveTo(i*unit, 0); eCtx.lineTo(i*unit, state.gridH*unit); }
475
+ for(let i=0; i<=state.gridH; i++) { eCtx.moveTo(0, i*unit); eCtx.lineTo(state.gridW*unit, i*unit); }
476
  eCtx.stroke();
477
 
478
  const link = document.createElement('a');
479
+ link.download = `拼豆图纸_${beadSizeMM}mm_带标尺.png`;
480
  link.href = expCvs.toDataURL();
481
  link.click();
 
 
482
  }
483
 
484
+ // --- 裁剪与导入逻辑 (V6.0 支持自定义精度) ---
485
  const modal = document.getElementById('cropModal');
486
  const cropImg = document.getElementById('crop-img');
487
  const cropBox = document.getElementById('crop-box');
 
497
  cropBox.style.display = 'block';
498
  cropBox.style.left = '50px'; cropBox.style.top = '50px';
499
  cropBox.style.width = '100px'; cropBox.style.height = '100px';
500
+ // 重置输入建议
501
+ document.getElementById('targetWidthInput').value = 50;
502
  }
503
  };
504
  reader.readAsDataURL(input.files[0]);
 
509
  if(e.target.classList.contains('resize-handle')) return;
510
  const rect = cropBox.getBoundingClientRect();
511
  if(e.clientX>rect.left && e.clientX<rect.right && e.clientY>rect.top && e.clientY<rect.bottom) {
512
+ cropState.isDrag = true; cropState.startX = e.clientX - cropBox.offsetLeft; cropState.startY = e.clientY - cropBox.offsetTop;
513
+ document.onmousemove = e => { cropBox.style.left = (e.clientX - cropState.startX) + 'px'; cropBox.style.top = (e.clientY - cropState.startY) + 'px'; };
 
 
 
 
 
514
  document.onmouseup = () => { document.onmousemove=null; };
515
  }
516
  }
517
  function startCropResize(e) {
518
+ e.stopPropagation(); const startX = e.clientX; const startY = e.clientY; const startW = cropBox.offsetWidth; const startH = cropBox.offsetHeight;
519
+ document.onmousemove = e => { cropBox.style.width = (startW + e.clientX - startX) + 'px'; cropBox.style.height = (startH + e.clientY - startY) + 'px'; };
 
 
 
 
 
520
  document.onmouseup = () => { document.onmousemove=null; };
521
  }
522
  function confirmCrop() {
 
526
  const cw = cropBox.offsetWidth * factor;
527
  const ch = cropBox.offsetHeight * factor;
528
 
529
+ // V6.0 核心逻辑:使用用户输入的生成宽度
530
+ const targetW = parseInt(document.getElementById('targetWidthInput').value) || 50;
531
  const targetH = Math.floor(targetW * (ch / cw));
532
+
533
+ // 更新全局画布尺寸
534
+ document.getElementById('gridW').value = targetW;
535
  document.getElementById('gridH').value = targetH;
536
  state.gridW = targetW; state.gridH = targetH;
537
 
538
+ const tmpCv = document.createElement('canvas'); tmpCv.width = targetW; tmpCv.height = targetH;
 
539
  const tmpCtx = tmpCv.getContext('2d');
540
  tmpCtx.drawImage(cropImg, cx, cy, cw, ch, 0, 0, targetW, targetH);
541
  const data = tmpCtx.getImageData(0, 0, targetW, targetH).data;
542
 
543
+ const paletteRGB = PALETTE.map(p => { const v = parseInt(p.hex.slice(1), 16); return [(v>>16)&0xff, (v>>8)&0xff, v&0xff]; });
 
 
 
 
544
  state.pixels = {};
545
  const useDither = document.getElementById('ditherCheck').checked;
546
 
 
558
  if(useDither) {
559
  const [nr, ng, nb] = paletteRGB[bestP];
560
  const er = oldR - nr, eg = oldG - ng, eb = oldB - nb;
561
+ function addErr(dx, dy, f) { if(x+dx>=0 && x+dx<targetW && y+dy<targetH) { const nIdx = ((y+dy)*targetW + (x+dx))*4; data[nIdx] += er * f; data[nIdx+1] += eg * f; data[nIdx+2] += eb * f; } }
 
 
 
 
 
562
  addErr(1, 0, 7/16); addErr(-1, 1, 3/16); addErr(0, 1, 5/16); addErr(1, 1, 1/16);
563
  }
564
  }
565
  }
566
+ closeModal(); resizeAll();
 
567
  }
568
 
569
  init();