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

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +419 -1
index.html CHANGED
@@ -211,4 +211,422 @@ function init() {
211
  }, {passive:false});
212
  }
213
 
214
- function initPalette
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
274
+ ctx.save();
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
+
287
+ // 2. 绘制豆子/像素
288
+ for(let key in state.pixels) {
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
+ }
391
+
392
+ function getContrastColor(hex) {
393
+ const r = parseInt(hex.substr(1,2),16), g = parseInt(hex.substr(3,2),16), b = parseInt(hex.substr(5,2),16);
394
+ return (r*0.299 + g*0.587 + b*0.114) > 128 ? '#000' : '#fff';
395
+ }
396
+
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
+ };
416
+
417
+ 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];
427
+ else if(state.tool === 'bucket') floodFill(x, y, state.curColorIdx);
428
+ else if(state.tool === 'picker') {
429
+ if(state.pixels[key] !== undefined) selectColor(state.pixels[key]);
430
+ setTool('pencil');
431
+ }
432
+ draw();
433
+ }
434
+
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;
448
+ q.push([cx+1,cy], [cx-1,cy], [cx,cy+1], [cx,cy-1]);
449
+ }
450
+ draw();
451
+ }
452
+
453
+ function setTool(t) {
454
+ state.tool = t;
455
+ document.querySelectorAll('.tool-btn').forEach(b => b.classList.remove('active'));
456
+ document.getElementById(`t-${t}`).classList.add('active');
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) {
465
+ document.getElementById('info-zoom').innerText = `Zoom: ${Math.round(state.scale)}%`;
466
+ if(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');
533
+ let cropState = { startX:0, startY:0, isResizing:false };
534
+
535
+ function handleFile(input) {
536
+ if(!input.files[0]) return;
537
+ const reader = new FileReader();
538
+ reader.onload = e => {
539
+ cropImg.src = e.target.result;
540
+ cropImg.onload = () => {
541
+ modal.style.display = 'flex';
542
+ cropBox.style.display = 'block';
543
+ cropBox.style.left = '50px'; cropBox.style.top = '50px';
544
+ cropBox.style.width = '100px'; cropBox.style.height = '100px';
545
+ }
546
+ };
547
+ reader.readAsDataURL(input.files[0]);
548
+ input.value = '';
549
+ }
550
+ function closeModal() { modal.style.display = 'none'; }
551
+ function startCropDrag(e) {
552
+ if(e.target.classList.contains('resize-handle')) return;
553
+ const rect = cropBox.getBoundingClientRect();
554
+ if(e.clientX>rect.left && e.clientX<rect.right && e.clientY>rect.top && e.clientY<rect.bottom) {
555
+ cropState.isDrag = true;
556
+ cropState.startX = e.clientX - cropBox.offsetLeft;
557
+ cropState.startY = e.clientY - cropBox.offsetTop;
558
+ document.onmousemove = e => {
559
+ cropBox.style.left = (e.clientX - cropState.startX) + 'px';
560
+ cropBox.style.top = (e.clientY - cropState.startY) + 'px';
561
+ };
562
+ document.onmouseup = () => { document.onmousemove=null; };
563
+ }
564
+ }
565
+ function startCropResize(e) {
566
+ e.stopPropagation();
567
+ const startX = e.clientX; const startY = e.clientY;
568
+ const startW = cropBox.offsetWidth; const startH = cropBox.offsetHeight;
569
+ document.onmousemove = e => {
570
+ cropBox.style.width = (startW + e.clientX - startX) + 'px';
571
+ cropBox.style.height = (startH + e.clientY - startY) + 'px';
572
+ };
573
+ document.onmouseup = () => { document.onmousemove=null; };
574
+ }
575
+ function confirmCrop() {
576
+ const factor = cropImg.naturalWidth / cropImg.width;
577
+ const cx = (cropBox.offsetLeft - cropImg.offsetLeft) * factor;
578
+ const cy = (cropBox.offsetTop - cropImg.offsetTop) * factor;
579
+ const cw = cropBox.offsetWidth * factor;
580
+ const ch = cropBox.offsetHeight * factor;
581
+
582
+ const targetW = parseInt(document.getElementById('gridW').value);
583
+ const targetH = Math.floor(targetW * (ch / cw));
584
+ document.getElementById('gridH').value = targetH;
585
+ state.gridW = targetW; state.gridH = targetH;
586
+
587
+ const tmpCv = document.createElement('canvas');
588
+ tmpCv.width = targetW; tmpCv.height = targetH;
589
+ const tmpCtx = tmpCv.getContext('2d');
590
+ tmpCtx.drawImage(cropImg, cx, cy, cw, ch, 0, 0, targetW, targetH);
591
+ const data = tmpCtx.getImageData(0, 0, targetW, targetH).data;
592
+
593
+ const paletteRGB = PALETTE.map(p => {
594
+ const v = parseInt(p.hex.slice(1), 16);
595
+ return [(v>>16)&0xff, (v>>8)&0xff, v&0xff];
596
+ });
597
+
598
+ state.pixels = {};
599
+ const useDither = document.getElementById('ditherCheck').checked;
600
+
601
+ for(let y=0; y<targetH; y++) {
602
+ for(let x=0; x<targetW; x++) {
603
+ const idx = (y*targetW + x)*4;
604
+ const oldR = data[idx], oldG = data[idx+1], oldB = data[idx+2];
605
+ let bestDist = Infinity, bestP = 0;
606
+ for(let i=0; i<paletteRGB.length; i++) {
607
+ const [pr, pg, pb] = paletteRGB[i];
608
+ const dist = (oldR-pr)**2 + (oldG-pg)**2 + (oldB-pb)**2;
609
+ if(dist < bestDist) { bestDist=dist; bestP=i; }
610
+ }
611
+ state.pixels[`${x},${y}`] = bestP;
612
+ if(useDither) {
613
+ const [nr, ng, nb] = paletteRGB[bestP];
614
+ const er = oldR - nr, eg = oldG - ng, eb = oldB - nb;
615
+ function addErr(dx, dy, f) {
616
+ if(x+dx>=0 && x+dx<targetW && y+dy<targetH) {
617
+ const nIdx = ((y+dy)*targetW + (x+dx))*4;
618
+ data[nIdx] += er * f; data[nIdx+1] += eg * f; data[nIdx+2] += eb * f;
619
+ }
620
+ }
621
+ addErr(1, 0, 7/16); addErr(-1, 1, 3/16); addErr(0, 1, 5/16); addErr(1, 1, 1/16);
622
+ }
623
+ }
624
+ }
625
+ closeModal();
626
+ resizeAll(); // 强制刷新视图和标尺
627
+ }
628
+
629
+ init();
630
+ </script>
631
+ </body>
632
+ </html>