Spaces:
Running
Running
Update index.html
Browse files- index.html +61 -32
index.html
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
<html lang="zh-CN">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
-
<title>BeadMaster Pro v10.
|
| 6 |
<style>
|
| 7 |
:root { --bg: #18181b; --panel: #27272a; --accent: #8b5cf6; --success: #10b981; --warn: #f59e0b; --text: #f4f4f5; --border: #3f3f46; --ruler-bg: #202022; }
|
| 8 |
body { margin: 0; overflow: hidden; background: var(--bg); color: var(--text); font-family: 'Segoe UI', Roboto, sans-serif; user-select: none; }
|
|
@@ -40,7 +40,7 @@
|
|
| 40 |
|
| 41 |
input[type="number"] { background: #18181b; border: 1px solid #52525b; color: white; width: 60px; padding: 4px; border-radius: 4px; text-align: center; font-size: 14px; font-weight: bold; height: 20px; }
|
| 42 |
select { background: #18181b; border: 1px solid #52525b; color: white; padding: 4px; border-radius: 4px; outline: none; font-size: 12px; height: 30px; }
|
| 43 |
-
input[type=range] { width:
|
| 44 |
|
| 45 |
/* 右侧面板 */
|
| 46 |
.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; background: #202022; }
|
|
@@ -90,7 +90,7 @@
|
|
| 90 |
</label>
|
| 91 |
</div>
|
| 92 |
<div style="display: flex; gap: 10px;">
|
| 93 |
-
<button class="action-btn btn-green" onclick="confirmCrop()">✅ 确认导入</button>
|
| 94 |
<button class="action-btn" onclick="closeModal()">取消</button>
|
| 95 |
</div>
|
| 96 |
</div>
|
|
@@ -99,7 +99,7 @@
|
|
| 99 |
|
| 100 |
<div id="app">
|
| 101 |
<div class="sidebar">
|
| 102 |
-
<div style="height: 56px; display: flex; align-items: center; font-weight: 900; color: var(--accent); font-size: 14px;">v10.
|
| 103 |
<button class="tool-btn active" id="t-pencil" onclick="setTool('pencil')" title="画笔">✏️</button>
|
| 104 |
<button class="tool-btn" id="t-eraser" onclick="setTool('eraser')" title="橡皮">🧼</button>
|
| 105 |
<button class="tool-btn" id="t-bucket" onclick="setTool('bucket')" title="油漆桶">🪣</button>
|
|
@@ -387,25 +387,21 @@ function draw() {
|
|
| 387 |
const explodeY = state.explodeVal * (s * 0.05); // 爆炸力度
|
| 388 |
const singleMode = document.getElementById('singleLayerCheck').checked;
|
| 389 |
|
| 390 |
-
// 绘制基底网格
|
| 391 |
ctx.strokeStyle = '#333'; ctx.lineWidth = 1; ctx.beginPath();
|
| 392 |
for(let i=0; i<=state.gridW; i++) { ctx.moveTo(i*s, 0); ctx.lineTo(i*s, state.gridH*s); }
|
| 393 |
for(let i=0; i<=state.gridH; i++) { ctx.moveTo(0, i*s); ctx.lineTo(state.gridW*s, i*s); }
|
| 394 |
ctx.stroke();
|
| 395 |
|
| 396 |
-
// 3D 渲染循环
|
| 397 |
const startL = singleMode ? state.viewLayer : 1;
|
| 398 |
const endL = state.viewLayer;
|
| 399 |
|
| 400 |
for(let l=startL; l<=endL; l++) {
|
| 401 |
-
// 计算当前层的偏移 (爆炸 + 伪3D视角)
|
| 402 |
const layerOffsetX = -offsetPerLayer * (l-1);
|
| 403 |
const layerOffsetY = -offsetPerLayer * (l-1) - (explodeY * (l-1));
|
| 404 |
|
| 405 |
-
// 阴影层 (仅在非单层模式且有间隔时绘制)
|
| 406 |
if(!singleMode && state.explodeVal > 5 && l > 1) {
|
| 407 |
ctx.save();
|
| 408 |
-
ctx.translate(layerOffsetX, layerOffsetY + explodeY * 0.5);
|
| 409 |
ctx.fillStyle = 'rgba(0,0,0,0.3)';
|
| 410 |
for(let key in state.pixels) {
|
| 411 |
if ((state.heightMap[key] || 1) >= l) {
|
|
@@ -422,13 +418,11 @@ function draw() {
|
|
| 422 |
|
| 423 |
for(let key in state.pixels) {
|
| 424 |
const h = state.heightMap[key] || 1;
|
| 425 |
-
// 只有当高度够,且属于该层时才画
|
| 426 |
if (h >= l) {
|
| 427 |
const [x, y] = key.split(',').map(Number);
|
| 428 |
const p = PALETTE[state.pixels[key]];
|
| 429 |
const drawX = x*s; const drawY = y*s;
|
| 430 |
|
| 431 |
-
// 伪3D侧面 (仅最顶层不需要,或者单层模式不需要)
|
| 432 |
if(l < h && !singleMode && state.explodeVal < 10) {
|
| 433 |
ctx.fillStyle = '#000';
|
| 434 |
if(shape==='circle') { ctx.beginPath(); ctx.arc(drawX + s/2 + 2, drawY + s/2 + 2, s/2, 0, Math.PI*2); ctx.fill(); }
|
|
@@ -438,17 +432,11 @@ function draw() {
|
|
| 438 |
ctx.fillStyle = p.hex;
|
| 439 |
if(shape === 'circle') {
|
| 440 |
ctx.beginPath(); ctx.arc(drawX + s/2, drawY + s/2, s/2 - 1, 0, Math.PI*2); ctx.fill();
|
| 441 |
-
//
|
| 442 |
-
ctx.fillStyle = 'rgba(
|
| 443 |
-
ctx.beginPath(); ctx.arc(drawX + s/2 - s/6, drawY + s/2 - s/6, s/6, 0, Math.PI*2); ctx.fill();
|
| 444 |
-
// 3D模式下不画孔,为了视觉整体性,或者画个深色孔
|
| 445 |
-
ctx.fillStyle = 'rgba(0,0,0,0.2)';
|
| 446 |
-
ctx.beginPath(); ctx.arc(drawX + s/2, drawY + s/2, s/8, 0, Math.PI*2); ctx.fill();
|
| 447 |
} else {
|
| 448 |
ctx.fillRect(drawX + 0.5, drawY + 0.5, s - 1, s - 1);
|
| 449 |
-
|
| 450 |
-
ctx.fillStyle = 'rgba(255,255,255,0.1)';
|
| 451 |
-
ctx.fillRect(drawX, drawY, s, s/3);
|
| 452 |
}
|
| 453 |
}
|
| 454 |
}
|
|
@@ -456,7 +444,6 @@ function draw() {
|
|
| 456 |
}
|
| 457 |
|
| 458 |
} else {
|
| 459 |
-
// --- 2D 渲染 ---
|
| 460 |
ctx.beginPath(); ctx.lineWidth = 1; ctx.strokeStyle = '#27272a';
|
| 461 |
for(let i=0; i<=state.gridW; i++) { ctx.moveTo(i*s, 0); ctx.lineTo(i*s, state.gridH*s); }
|
| 462 |
for(let i=0; i<=state.gridH; i++) { ctx.moveTo(0, i*s); ctx.lineTo(state.gridW*s, i*s); }
|
|
@@ -474,7 +461,6 @@ function draw() {
|
|
| 474 |
} else {
|
| 475 |
ctx.fillRect(x*s + 0.5, y*s + 0.5, s - 1, s - 1);
|
| 476 |
}
|
| 477 |
-
|
| 478 |
if(showId) {
|
| 479 |
ctx.fillStyle = getContrastColor(p.hex) === '#fff' ? '#fff' : '#000';
|
| 480 |
ctx.font = `bold ${Math.floor(s*0.35)}px Arial`; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
|
|
@@ -493,7 +479,7 @@ function draw() {
|
|
| 493 |
drawRulers(); updateMaterialList();
|
| 494 |
}
|
| 495 |
|
| 496 |
-
//
|
| 497 |
function drawRulers() {
|
| 498 |
const s = state.scale; const ox = state.offsetX; const oy = state.offsetY;
|
| 499 |
const ctxT = ctxRTop; ctxT.clearRect(0,0,rulerTop.width,32);
|
|
@@ -517,6 +503,7 @@ function drawRulers() {
|
|
| 517 |
}
|
| 518 |
ctxL.stroke();
|
| 519 |
}
|
|
|
|
| 520 |
function getContrastColor(hex) { const r = parseInt(hex.substr(1,2),16), g = parseInt(hex.substr(3,2),16), b = parseInt(hex.substr(5,2),16); return (r*0.299 + g*0.587 + b*0.114) > 128 ? '#000' : '#fff'; }
|
| 521 |
function resizeAll() { const vw = viewport.clientWidth; const vh = viewport.clientHeight; cvs.width = vw; cvs.height = vh; rulerTop.width = vw; rulerTop.height = 32; rulerLeft.width = 32; rulerLeft.height = vh; draw(); }
|
| 522 |
function mergeSimilarColors() {
|
|
@@ -569,26 +556,63 @@ function exportImageWithRuler() {
|
|
| 569 |
eCtx.strokeStyle = '#999'; eCtx.lineWidth = 3; for(let i=0; i<=state.gridW; i++) { eCtx.moveTo(i*unit, 0); eCtx.lineTo(i*unit, state.gridH*unit); } for(let i=0; i<=state.gridH; i++) { eCtx.moveTo(0, i*unit); eCtx.lineTo(state.gridW*unit, i*unit); } eCtx.stroke();
|
| 570 |
const link = document.createElement('a'); link.download = `拼豆工程图.png`; link.href = expCvs.toDataURL(); link.click();
|
| 571 |
}
|
| 572 |
-
|
|
|
|
| 573 |
const modal = document.getElementById('cropModal'); const cropImg = document.getElementById('crop-img'); const cropBox = document.getElementById('crop-box'); let cropState = { startX:0, startY:0 };
|
| 574 |
-
function handleFile(input) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
function closeModal() { modal.style.display = 'none'; }
|
| 576 |
function startCropDrag(e) { if(e.target.classList.contains('resize-handle')) return; const rect = cropBox.getBoundingClientRect(); if(e.clientX>rect.left && e.clientX<rect.right && e.clientY>rect.top && e.clientY<rect.bottom) { cropState.isDrag = true; cropState.startX = e.clientX - cropBox.offsetLeft; cropState.startY = e.clientY - cropBox.offsetTop; document.onmousemove = e => { cropBox.style.left = (e.clientX - cropState.startX) + 'px'; cropBox.style.top = (e.clientY - cropState.startY) + 'px'; }; document.onmouseup = () => { document.onmousemove=null; }; } }
|
| 577 |
function startCropResize(e) { e.stopPropagation(); const startX = e.clientX; const startY = e.clientY; const startW = cropBox.offsetWidth; const startH = cropBox.offsetHeight; document.onmousemove = e => { cropBox.style.width = (startW + e.clientX - startX) + 'px'; cropBox.style.height = (startH + e.clientY - startY) + 'px'; }; document.onmouseup = () => { document.onmousemove=null; }; }
|
|
|
|
| 578 |
function confirmCrop() {
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 583 |
}
|
|
|
|
| 584 |
// 通用交互
|
| 585 |
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; };
|
| 586 |
window.onmouseup = () => { state.isDrag = false; viewport.onmousemove = null; };
|
| 587 |
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(); } };
|
| 588 |
function paint(e) {
|
| 589 |
-
const rect = viewport.getBoundingClientRect();
|
| 590 |
-
const x = Math.floor((e.clientX - rect.left - state.offsetX) / state.scale);
|
| 591 |
-
const y = Math.floor((e.clientY - rect.top - state.offsetY) / state.scale);
|
| 592 |
if(x<0 || x>=state.gridW || y<0 || y>=state.gridH) return;
|
| 593 |
const key = `${x},${y}`;
|
| 594 |
if(state.tool === 'pencil') state.pixels[key] = state.curColorIdx;
|
|
@@ -608,6 +632,11 @@ function floodFill(x, y, targetIdx) {
|
|
| 608 |
}
|
| 609 |
draw();
|
| 610 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
init();
|
| 612 |
</script>
|
| 613 |
</body>
|
|
|
|
| 2 |
<html lang="zh-CN">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
+
<title>BeadMaster Pro v10.1 - 稳定修复版</title>
|
| 6 |
<style>
|
| 7 |
:root { --bg: #18181b; --panel: #27272a; --accent: #8b5cf6; --success: #10b981; --warn: #f59e0b; --text: #f4f4f5; --border: #3f3f46; --ruler-bg: #202022; }
|
| 8 |
body { margin: 0; overflow: hidden; background: var(--bg); color: var(--text); font-family: 'Segoe UI', Roboto, sans-serif; user-select: none; }
|
|
|
|
| 40 |
|
| 41 |
input[type="number"] { background: #18181b; border: 1px solid #52525b; color: white; width: 60px; padding: 4px; border-radius: 4px; text-align: center; font-size: 14px; font-weight: bold; height: 20px; }
|
| 42 |
select { background: #18181b; border: 1px solid #52525b; color: white; padding: 4px; border-radius: 4px; outline: none; font-size: 12px; height: 30px; }
|
| 43 |
+
input[type=range] { width: 80px; accent-color: var(--accent); cursor: pointer; }
|
| 44 |
|
| 45 |
/* 右侧面板 */
|
| 46 |
.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; background: #202022; }
|
|
|
|
| 90 |
</label>
|
| 91 |
</div>
|
| 92 |
<div style="display: flex; gap: 10px;">
|
| 93 |
+
<button class="action-btn btn-green" id="btn-confirm-import" onclick="confirmCrop()">✅ 确认导入</button>
|
| 94 |
<button class="action-btn" onclick="closeModal()">取消</button>
|
| 95 |
</div>
|
| 96 |
</div>
|
|
|
|
| 99 |
|
| 100 |
<div id="app">
|
| 101 |
<div class="sidebar">
|
| 102 |
+
<div style="height: 56px; display: flex; align-items: center; font-weight: 900; color: var(--accent); font-size: 14px;">v10.1</div>
|
| 103 |
<button class="tool-btn active" id="t-pencil" onclick="setTool('pencil')" title="画笔">✏️</button>
|
| 104 |
<button class="tool-btn" id="t-eraser" onclick="setTool('eraser')" title="橡皮">🧼</button>
|
| 105 |
<button class="tool-btn" id="t-bucket" onclick="setTool('bucket')" title="油漆桶">🪣</button>
|
|
|
|
| 387 |
const explodeY = state.explodeVal * (s * 0.05); // 爆炸力度
|
| 388 |
const singleMode = document.getElementById('singleLayerCheck').checked;
|
| 389 |
|
|
|
|
| 390 |
ctx.strokeStyle = '#333'; ctx.lineWidth = 1; ctx.beginPath();
|
| 391 |
for(let i=0; i<=state.gridW; i++) { ctx.moveTo(i*s, 0); ctx.lineTo(i*s, state.gridH*s); }
|
| 392 |
for(let i=0; i<=state.gridH; i++) { ctx.moveTo(0, i*s); ctx.lineTo(state.gridW*s, i*s); }
|
| 393 |
ctx.stroke();
|
| 394 |
|
|
|
|
| 395 |
const startL = singleMode ? state.viewLayer : 1;
|
| 396 |
const endL = state.viewLayer;
|
| 397 |
|
| 398 |
for(let l=startL; l<=endL; l++) {
|
|
|
|
| 399 |
const layerOffsetX = -offsetPerLayer * (l-1);
|
| 400 |
const layerOffsetY = -offsetPerLayer * (l-1) - (explodeY * (l-1));
|
| 401 |
|
|
|
|
| 402 |
if(!singleMode && state.explodeVal > 5 && l > 1) {
|
| 403 |
ctx.save();
|
| 404 |
+
ctx.translate(layerOffsetX, layerOffsetY + explodeY * 0.5);
|
| 405 |
ctx.fillStyle = 'rgba(0,0,0,0.3)';
|
| 406 |
for(let key in state.pixels) {
|
| 407 |
if ((state.heightMap[key] || 1) >= l) {
|
|
|
|
| 418 |
|
| 419 |
for(let key in state.pixels) {
|
| 420 |
const h = state.heightMap[key] || 1;
|
|
|
|
| 421 |
if (h >= l) {
|
| 422 |
const [x, y] = key.split(',').map(Number);
|
| 423 |
const p = PALETTE[state.pixels[key]];
|
| 424 |
const drawX = x*s; const drawY = y*s;
|
| 425 |
|
|
|
|
| 426 |
if(l < h && !singleMode && state.explodeVal < 10) {
|
| 427 |
ctx.fillStyle = '#000';
|
| 428 |
if(shape==='circle') { ctx.beginPath(); ctx.arc(drawX + s/2 + 2, drawY + s/2 + 2, s/2, 0, Math.PI*2); ctx.fill(); }
|
|
|
|
| 432 |
ctx.fillStyle = p.hex;
|
| 433 |
if(shape === 'circle') {
|
| 434 |
ctx.beginPath(); ctx.arc(drawX + s/2, drawY + s/2, s/2 - 1, 0, Math.PI*2); ctx.fill();
|
| 435 |
+
ctx.fillStyle = 'rgba(255,255,255,0.15)'; ctx.beginPath(); ctx.arc(drawX + s/2 - s/6, drawY + s/2 - s/6, s/6, 0, Math.PI*2); ctx.fill();
|
| 436 |
+
ctx.fillStyle = 'rgba(0,0,0,0.2)'; ctx.beginPath(); ctx.arc(drawX + s/2, drawY + s/2, s/8, 0, Math.PI*2); ctx.fill();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
} else {
|
| 438 |
ctx.fillRect(drawX + 0.5, drawY + 0.5, s - 1, s - 1);
|
| 439 |
+
ctx.fillStyle = 'rgba(255,255,255,0.1)'; ctx.fillRect(drawX, drawY, s, s/3);
|
|
|
|
|
|
|
| 440 |
}
|
| 441 |
}
|
| 442 |
}
|
|
|
|
| 444 |
}
|
| 445 |
|
| 446 |
} else {
|
|
|
|
| 447 |
ctx.beginPath(); ctx.lineWidth = 1; ctx.strokeStyle = '#27272a';
|
| 448 |
for(let i=0; i<=state.gridW; i++) { ctx.moveTo(i*s, 0); ctx.lineTo(i*s, state.gridH*s); }
|
| 449 |
for(let i=0; i<=state.gridH; i++) { ctx.moveTo(0, i*s); ctx.lineTo(state.gridW*s, i*s); }
|
|
|
|
| 461 |
} else {
|
| 462 |
ctx.fillRect(x*s + 0.5, y*s + 0.5, s - 1, s - 1);
|
| 463 |
}
|
|
|
|
| 464 |
if(showId) {
|
| 465 |
ctx.fillStyle = getContrastColor(p.hex) === '#fff' ? '#fff' : '#000';
|
| 466 |
ctx.font = `bold ${Math.floor(s*0.35)}px Arial`; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
|
|
|
|
| 479 |
drawRulers(); updateMaterialList();
|
| 480 |
}
|
| 481 |
|
| 482 |
+
// 标尺绘制
|
| 483 |
function drawRulers() {
|
| 484 |
const s = state.scale; const ox = state.offsetX; const oy = state.offsetY;
|
| 485 |
const ctxT = ctxRTop; ctxT.clearRect(0,0,rulerTop.width,32);
|
|
|
|
| 503 |
}
|
| 504 |
ctxL.stroke();
|
| 505 |
}
|
| 506 |
+
|
| 507 |
function getContrastColor(hex) { const r = parseInt(hex.substr(1,2),16), g = parseInt(hex.substr(3,2),16), b = parseInt(hex.substr(5,2),16); return (r*0.299 + g*0.587 + b*0.114) > 128 ? '#000' : '#fff'; }
|
| 508 |
function resizeAll() { const vw = viewport.clientWidth; const vh = viewport.clientHeight; cvs.width = vw; cvs.height = vh; rulerTop.width = vw; rulerTop.height = 32; rulerLeft.width = 32; rulerLeft.height = vh; draw(); }
|
| 509 |
function mergeSimilarColors() {
|
|
|
|
| 556 |
eCtx.strokeStyle = '#999'; eCtx.lineWidth = 3; for(let i=0; i<=state.gridW; i++) { eCtx.moveTo(i*unit, 0); eCtx.lineTo(i*unit, state.gridH*unit); } for(let i=0; i<=state.gridH; i++) { eCtx.moveTo(0, i*unit); eCtx.lineTo(state.gridW*unit, i*unit); } eCtx.stroke();
|
| 557 |
const link = document.createElement('a'); link.download = `拼豆工程图.png`; link.href = expCvs.toDataURL(); link.click();
|
| 558 |
}
|
| 559 |
+
|
| 560 |
+
// --- v10.1 修复: 整数坐标逻辑 + 错误处理 ---
|
| 561 |
const modal = document.getElementById('cropModal'); const cropImg = document.getElementById('crop-img'); const cropBox = document.getElementById('crop-box'); let cropState = { startX:0, startY:0 };
|
| 562 |
+
function handleFile(input) {
|
| 563 |
+
if(!input.files[0]) return; const reader = new FileReader();
|
| 564 |
+
reader.onload = e => { cropImg.src = e.target.result; cropImg.onload = () => { modal.style.display = 'flex'; cropBox.style.display = 'block'; cropBox.style.left = '50px'; cropBox.style.top = '50px'; cropBox.style.width = '100px'; cropBox.style.height = '100px'; } };
|
| 565 |
+
reader.readAsDataURL(input.files[0]); input.value = '';
|
| 566 |
+
}
|
| 567 |
function closeModal() { modal.style.display = 'none'; }
|
| 568 |
function startCropDrag(e) { if(e.target.classList.contains('resize-handle')) return; const rect = cropBox.getBoundingClientRect(); if(e.clientX>rect.left && e.clientX<rect.right && e.clientY>rect.top && e.clientY<rect.bottom) { cropState.isDrag = true; cropState.startX = e.clientX - cropBox.offsetLeft; cropState.startY = e.clientY - cropBox.offsetTop; document.onmousemove = e => { cropBox.style.left = (e.clientX - cropState.startX) + 'px'; cropBox.style.top = (e.clientY - cropState.startY) + 'px'; }; document.onmouseup = () => { document.onmousemove=null; }; } }
|
| 569 |
function startCropResize(e) { e.stopPropagation(); const startX = e.clientX; const startY = e.clientY; const startW = cropBox.offsetWidth; const startH = cropBox.offsetHeight; document.onmousemove = e => { cropBox.style.width = (startW + e.clientX - startX) + 'px'; cropBox.style.height = (startH + e.clientY - startY) + 'px'; }; document.onmouseup = () => { document.onmousemove=null; }; }
|
| 570 |
+
|
| 571 |
function confirmCrop() {
|
| 572 |
+
try {
|
| 573 |
+
const btn = document.getElementById('btn-confirm-import');
|
| 574 |
+
btn.innerText = "处理中...";
|
| 575 |
+
|
| 576 |
+
// 1. 获取缩放比例
|
| 577 |
+
const factor = cropImg.naturalWidth / cropImg.width;
|
| 578 |
+
|
| 579 |
+
// 2. 强制取整 (修复 float crash bug)
|
| 580 |
+
const cx = Math.floor((cropBox.offsetLeft - cropImg.offsetLeft) * factor);
|
| 581 |
+
const cy = Math.floor((cropBox.offsetTop - cropImg.offsetTop) * factor);
|
| 582 |
+
const cw = Math.floor(cropBox.offsetWidth * factor);
|
| 583 |
+
const ch = Math.floor(cropBox.offsetHeight * factor);
|
| 584 |
+
|
| 585 |
+
if (cw <= 0 || ch <= 0) throw new Error("选区太小");
|
| 586 |
+
|
| 587 |
+
const targetW = parseInt(document.getElementById('targetRes').value) || 60;
|
| 588 |
+
const targetH = Math.floor(targetW * (ch / cw));
|
| 589 |
+
document.getElementById('gridW').value = targetW; document.getElementById('gridH').value = targetH;
|
| 590 |
+
state.gridW = targetW; state.gridH = targetH;
|
| 591 |
+
|
| 592 |
+
// 3. 安全绘制
|
| 593 |
+
const tmpCv = document.createElement('canvas'); tmpCv.width = cw; tmpCv.height = ch;
|
| 594 |
+
const tmpCtx = tmpCv.getContext('2d');
|
| 595 |
+
tmpCtx.drawImage(cropImg, cx, cy, cw, ch, 0, 0, cw, ch);
|
| 596 |
+
|
| 597 |
+
state.sourceImgData = tmpCtx.getImageData(0, 0, cw, ch);
|
| 598 |
+
state.sourceW = cw; state.sourceH = ch;
|
| 599 |
+
|
| 600 |
+
reprocessCurrentImg();
|
| 601 |
+
closeModal();
|
| 602 |
+
btn.innerText = "✅ 确认导入";
|
| 603 |
+
} catch(e) {
|
| 604 |
+
alert("导入出错,请刷新重试: " + e.message);
|
| 605 |
+
console.error(e);
|
| 606 |
+
document.getElementById('btn-confirm-import').innerText = "✅ 确认导入";
|
| 607 |
+
}
|
| 608 |
}
|
| 609 |
+
|
| 610 |
// 通用交互
|
| 611 |
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; };
|
| 612 |
window.onmouseup = () => { state.isDrag = false; viewport.onmousemove = null; };
|
| 613 |
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(); } };
|
| 614 |
function paint(e) {
|
| 615 |
+
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);
|
|
|
|
|
|
|
| 616 |
if(x<0 || x>=state.gridW || y<0 || y>=state.gridH) return;
|
| 617 |
const key = `${x},${y}`;
|
| 618 |
if(state.tool === 'pencil') state.pixels[key] = state.curColorIdx;
|
|
|
|
| 632 |
}
|
| 633 |
draw();
|
| 634 |
}
|
| 635 |
+
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'; }
|
| 636 |
+
function selectColor(idx) { state.curColorIdx = idx; updateMaterialList(); setTool('pencil'); }
|
| 637 |
+
function resizeGrid() { state.gridW = parseInt(document.getElementById('gridW').value) || 60; state.gridH = parseInt(document.getElementById('gridH').value) || 60; draw(); }
|
| 638 |
+
function clearCanvas() { if(confirm('确定清空画布吗?')) { state.pixels={}; draw(); } }
|
| 639 |
+
|
| 640 |
init();
|
| 641 |
</script>
|
| 642 |
</body>
|