Spaces:
Running
Running
Update index.html
Browse files- index.html +201 -223
index.html
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
<html lang="zh-CN">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
-
<title>BeadMaster Pro
|
| 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; }
|
|
@@ -13,7 +13,7 @@
|
|
| 13 |
.panels { width: 320px; background: var(--panel); border-left: 1px solid var(--border); display: flex; flex-direction: column; z-index: 50; }
|
| 14 |
#canvas-wrapper { flex: 1; position: relative; overflow: hidden; background: #09090b; display: flex; flex-direction: column; }
|
| 15 |
|
| 16 |
-
/* 标尺
|
| 17 |
#ruler-top-bar { height: 32px; background: var(--ruler-bg); border-bottom: 1px solid var(--border); display: flex; z-index: 40; }
|
| 18 |
#corner-box { width: 32px; height: 32px; background: var(--ruler-bg); border-right: 1px solid var(--border); color: #888; font-size: 10px; display: flex; justify-content: center; align-items: center; flex-shrink: 0; }
|
| 19 |
#ruler-top { flex: 1; position: relative; overflow: hidden; }
|
|
@@ -40,29 +40,31 @@
|
|
| 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; }
|
| 47 |
#material-list { flex: 1; overflow-y: auto; padding: 5px 0; }
|
| 48 |
.material-item { display: flex; align-items: center; padding: 8px 15px; gap: 10px; border-bottom: 1px solid #333; font-size: 13px; cursor: pointer; transition: 0.2s; }
|
| 49 |
.material-item:hover { background: #3f3f46; }
|
| 50 |
-
.material-item.active { background: #
|
| 51 |
.list-swatch { width: 22px; height: 22px; border-radius: 4px; border: 1px solid rgba(255,255,255,0.2); flex-shrink: 0; }
|
| 52 |
.list-info { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
| 53 |
.list-count { font-family: monospace; font-weight: bold; color: var(--accent); font-size: 14px; }
|
| 54 |
#total-stats { padding: 15px; border-top: 1px solid var(--border); background: #202022; font-size: 13px; display: flex; justify-content: space-between; font-weight: bold; color: var(--success); }
|
| 55 |
|
| 56 |
-
/* 3D
|
| 57 |
-
.mode-switch { display: flex; background: #18181b; padding: 4px; border-radius: 6px; border: 1px solid var(--border); }
|
| 58 |
-
.mode-btn { flex: 1; padding: 6px; text-align: center; font-size: 12px; cursor: pointer; border-radius: 4px; color: #888; }
|
| 59 |
-
.mode-btn.active { background: var(--accent); color: white; font-weight: bold; }
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
/* 弹窗
|
| 64 |
.modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.9); z-index: 100; display: none; justify-content: center; align-items: center; backdrop-filter: blur(5px); }
|
| 65 |
-
.modal-content { background: var(--panel); padding: 20px; border-radius: 12px; width: 80vw; height: 85vh; display: flex; flex-direction: column; gap: 15px; border: 1px solid var(--border); }
|
| 66 |
#crop-container { flex: 1; background: #000; position: relative; overflow: hidden; display: flex; justify-content: center; align-items: center; border: 1px solid #3f3f46; border-radius: 4px; }
|
| 67 |
#crop-img { max-width: 100%; max-height: 100%; display: block; }
|
| 68 |
#crop-box { position: absolute; border: 2px dashed #fff; box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.6); cursor: move; display: none; }
|
|
@@ -74,7 +76,7 @@
|
|
| 74 |
|
| 75 |
<div class="modal-overlay" id="cropModal">
|
| 76 |
<div class="modal-content">
|
| 77 |
-
<h3 style="margin:0; color: white;">✂️
|
| 78 |
<div id="crop-container" onmousedown="startCropDrag(event)">
|
| 79 |
<img id="crop-img">
|
| 80 |
<div id="crop-box">
|
|
@@ -84,7 +86,7 @@
|
|
| 84 |
<div class="modal-footer">
|
| 85 |
<div style="display: flex; gap: 20px; align-items: center;">
|
| 86 |
<label style="display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer;">
|
| 87 |
-
<input type="checkbox" id="ditherCheck" checked> <span>色彩融合</span>
|
| 88 |
</label>
|
| 89 |
</div>
|
| 90 |
<div style="display: flex; gap: 10px;">
|
|
@@ -97,7 +99,7 @@
|
|
| 97 |
|
| 98 |
<div id="app">
|
| 99 |
<div class="sidebar">
|
| 100 |
-
<div style="height: 56px; display: flex; align-items: center; font-weight: 900; color: var(--accent); font-size: 14px;">
|
| 101 |
<button class="tool-btn active" id="t-pencil" onclick="setTool('pencil')" title="画笔">✏️</button>
|
| 102 |
<button class="tool-btn" id="t-eraser" onclick="setTool('eraser')" title="橡皮">🧼</button>
|
| 103 |
<button class="tool-btn" id="t-bucket" onclick="setTool('bucket')" title="油漆桶">🪣</button>
|
|
@@ -114,7 +116,7 @@
|
|
| 114 |
<div style="width:1px; height:24px; background:var(--border);"></div>
|
| 115 |
|
| 116 |
<div class="control-group">
|
| 117 |
-
<div class="control-label" style="color: var(--success);">🎯 精度
|
| 118 |
<input type="number" id="targetRes" value="60" min="10" max="300" onchange="reprocessCurrentImg()" onkeydown="if(event.key==='Enter') reprocessCurrentImg()">
|
| 119 |
</div>
|
| 120 |
|
|
@@ -131,13 +133,21 @@
|
|
| 131 |
|
| 132 |
<div class="mode-switch">
|
| 133 |
<div class="mode-btn active" id="mode-2d" onclick="setMode('2d')">2D 平面</div>
|
| 134 |
-
<div class="mode-btn" id="mode-3d" onclick="setMode('3d')">3D
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
</div>
|
| 136 |
|
| 137 |
<div style="flex:1;"></div>
|
| 138 |
|
| 139 |
-
<button class="action-btn btn-purple" id="btn-export-2d" onclick="exportImageWithRuler()">💾 导出平面
|
| 140 |
-
<button class="action-btn btn-purple" id="btn-export-3d" onclick="exportSlices()" style="display:none;">📑 导出分层
|
| 141 |
<button class="action-btn" onclick="clearCanvas()">🗑️</button>
|
| 142 |
</div>
|
| 143 |
|
|
@@ -156,23 +166,32 @@
|
|
| 156 |
</div>
|
| 157 |
|
| 158 |
<div class="panels">
|
| 159 |
-
<div id="panel-3d-controls"
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
</
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
| 168 |
</div>
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
</div>
|
| 172 |
-
<hr style="border:0; border-top:1px solid #333; margin:15px 0;">
|
| 173 |
-
<div class="control-label">查看层级 (Layer View)</div>
|
| 174 |
-
<input type="range" id="layerSlider" min="1" max="1" value="1" style="width:100%; margin-top:5px;" oninput="updateLayerView(this.value)">
|
| 175 |
-
<div style="text-align:center; font-size:12px; margin-top:5px; color:#fff;">当前显示: 第 <span id="current-layer-num">1</span> 层</div>
|
| 176 |
</div>
|
| 177 |
|
| 178 |
<div class="panel-header">
|
|
@@ -207,17 +226,14 @@ const PALETTE = [
|
|
| 207 |
|
| 208 |
const state = {
|
| 209 |
gridW: 60, gridH: 60,
|
| 210 |
-
pixels: {},
|
| 211 |
-
heightMap: {}, // V9.0: Map<"x,y", height> (1 to maxLayers)
|
| 212 |
scale: 25,
|
| 213 |
offsetX: 50, offsetY: 50,
|
| 214 |
-
tool: 'pencil',
|
| 215 |
-
curColorIdx: 2,
|
| 216 |
isDrag: false, lastX: 0, lastY: 0,
|
| 217 |
sourceImgData: null, sourceW: 0, sourceH: 0,
|
| 218 |
-
mode: '2d',
|
| 219 |
-
maxLayers: 1,
|
| 220 |
-
viewLayer: 1 // 当前查看的层数 (用于切片预览)
|
| 221 |
};
|
| 222 |
|
| 223 |
// DOM
|
|
@@ -233,23 +249,19 @@ const totalBeadsEl = document.getElementById('total-beads-count');
|
|
| 233 |
|
| 234 |
function init() {
|
| 235 |
window.addEventListener('resize', resizeAll);
|
| 236 |
-
resizeAll();
|
| 237 |
-
updateMaterialList();
|
| 238 |
-
draw();
|
| 239 |
viewport.addEventListener('wheel', e => {
|
| 240 |
-
e.preventDefault();
|
| 241 |
-
const delta = e.deltaY > 0 ? 0.9 : 1.1;
|
| 242 |
applyZoom(state.scale * delta, e.clientX, e.clientY);
|
| 243 |
}, {passive:false});
|
| 244 |
}
|
| 245 |
|
| 246 |
-
// ---
|
| 247 |
function setMode(mode) {
|
| 248 |
state.mode = mode;
|
| 249 |
document.querySelectorAll('.mode-btn').forEach(b => b.classList.remove('active'));
|
| 250 |
document.getElementById(`mode-${mode}`).classList.add('active');
|
| 251 |
|
| 252 |
-
// UI 切换
|
| 253 |
const controls3d = document.getElementById('panel-3d-controls');
|
| 254 |
const btnExp2d = document.getElementById('btn-export-2d');
|
| 255 |
const btnExp3d = document.getElementById('btn-export-3d');
|
|
@@ -258,8 +270,7 @@ function setMode(mode) {
|
|
| 258 |
controls3d.style.display = 'block';
|
| 259 |
btnExp2d.style.display = 'none';
|
| 260 |
btnExp3d.style.display = 'flex';
|
| 261 |
-
|
| 262 |
-
if(state.maxLayers === 1) autoGenerate3D(true); // true = silent init
|
| 263 |
} else {
|
| 264 |
controls3d.style.display = 'none';
|
| 265 |
btnExp2d.style.display = 'flex';
|
|
@@ -269,16 +280,8 @@ function setMode(mode) {
|
|
| 269 |
}
|
| 270 |
|
| 271 |
function autoGenerate3D(silent = false) {
|
| 272 |
-
if(!state.sourceImgData) {
|
| 273 |
-
|
| 274 |
-
return;
|
| 275 |
-
}
|
| 276 |
-
|
| 277 |
-
// 1. 重新取样,这次我们需要亮度信息
|
| 278 |
-
const targetW = state.gridW;
|
| 279 |
-
const targetH = state.gridH;
|
| 280 |
-
|
| 281 |
-
// 创建临时Canvas分析亮度
|
| 282 |
const tmpCv = document.createElement('canvas'); tmpCv.width = targetW; tmpCv.height = targetH;
|
| 283 |
const tmpCtx = tmpCv.getContext('2d');
|
| 284 |
const srcCv = document.createElement('canvas'); srcCv.width = state.sourceW; srcCv.height = state.sourceH;
|
|
@@ -286,102 +289,36 @@ function autoGenerate3D(silent = false) {
|
|
| 286 |
tmpCtx.drawImage(srcCv, 0, 0, targetW, targetH);
|
| 287 |
const data = tmpCtx.getImageData(0, 0, targetW, targetH).data;
|
| 288 |
|
| 289 |
-
// 2. 根据亮度生成高度 (Luma -> Height)
|
| 290 |
state.heightMap = {};
|
| 291 |
-
const layers = 6;
|
| 292 |
-
state.maxLayers = layers;
|
| 293 |
-
|
| 294 |
for(let y=0; y<targetH; y++) {
|
| 295 |
for(let x=0; x<targetW; x++) {
|
| 296 |
const idx = (y*targetW + x)*4;
|
| 297 |
const r = data[idx], g = data[idx+1], b = data[idx+2], a = data[idx+3];
|
| 298 |
if(a < 128) continue;
|
| 299 |
-
|
| 300 |
-
// 计算亮度 (0-255)
|
| 301 |
const luma = 0.299*r + 0.587*g + 0.114*b;
|
| 302 |
-
|
| 303 |
-
// 也可以反过来: 越暗越高 (适合人像阴影)。这里默认越亮越高。
|
| 304 |
-
let h = Math.ceil((luma / 255) * layers);
|
| 305 |
-
if(h < 1) h = 1;
|
| 306 |
state.heightMap[`${x},${y}`] = h;
|
| 307 |
}
|
| 308 |
}
|
| 309 |
|
| 310 |
-
// 更新 UI
|
| 311 |
document.getElementById('layerSlider').max = layers;
|
| 312 |
document.getElementById('layerSlider').value = layers;
|
| 313 |
state.viewLayer = layers;
|
| 314 |
-
document.getElementById('layer-
|
| 315 |
|
| 316 |
if(!silent) draw();
|
| 317 |
}
|
| 318 |
|
| 319 |
function updateLayerView(val) {
|
| 320 |
state.viewLayer = parseInt(val);
|
| 321 |
-
document.getElementById('
|
| 322 |
draw();
|
| 323 |
}
|
| 324 |
|
| 325 |
-
function
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
const unit = 20; // 导出像素大小
|
| 329 |
-
const w = state.gridW * unit;
|
| 330 |
-
const h = state.gridH * unit;
|
| 331 |
-
const margin = 40;
|
| 332 |
-
|
| 333 |
-
const expCv = document.createElement('canvas');
|
| 334 |
-
expCv.width = w + margin*2;
|
| 335 |
-
expCv.height = (h + margin) * layers + margin;
|
| 336 |
-
const ctx = expCv.getContext('2d');
|
| 337 |
-
|
| 338 |
-
ctx.fillStyle = 'white'; ctx.fillRect(0,0,expCv.width, expCv.height);
|
| 339 |
-
|
| 340 |
-
for(let l=1; l<=layers; l++) {
|
| 341 |
-
const startY = (l-1)*(h+margin) + margin;
|
| 342 |
-
|
| 343 |
-
// 绘制标题
|
| 344 |
-
ctx.fillStyle = '#000'; ctx.font = '20px Arial'; ctx.textAlign = 'left';
|
| 345 |
-
ctx.fillText(`第 ${l} 层 (Layer ${l})`, margin, startY - 10);
|
| 346 |
-
|
| 347 |
-
ctx.translate(margin, startY);
|
| 348 |
-
|
| 349 |
-
// 绘制该层的豆子
|
| 350 |
-
for(let key in state.pixels) {
|
| 351 |
-
const hVal = state.heightMap[key] || 1;
|
| 352 |
-
// 只有当该位置的高度 >= 当前层数时,这一层才需要放豆子
|
| 353 |
-
// (这是“堆叠”逻辑:如果一个柱子高5层,那么第1,2,3,4,5层都要放豆子)
|
| 354 |
-
if(hVal >= l) {
|
| 355 |
-
const [x, y] = key.split(',').map(Number);
|
| 356 |
-
const p = PALETTE[state.pixels[key]];
|
| 357 |
-
ctx.fillStyle = p.hex;
|
| 358 |
-
ctx.beginPath(); ctx.arc(x*unit + unit/2, y*unit + unit/2, unit/2 - 1, 0, Math.PI*2); ctx.fill();
|
| 359 |
-
// 洞
|
| 360 |
-
ctx.fillStyle = '#fff'; ctx.beginPath(); ctx.arc(x*unit + unit/2, y*unit + unit/2, unit/6, 0, Math.PI*2); ctx.fill();
|
| 361 |
-
}
|
| 362 |
-
}
|
| 363 |
-
|
| 364 |
-
// 网格
|
| 365 |
-
ctx.strokeStyle = '#ddd'; ctx.lineWidth=1;
|
| 366 |
-
for(let i=0; i<=state.gridW; i++) { ctx.moveTo(i*unit, 0); ctx.lineTo(i*unit, h); }
|
| 367 |
-
for(let i=0; i<=state.gridH; i++) { ctx.moveTo(0, i*unit); ctx.lineTo(w, i*unit); }
|
| 368 |
-
ctx.stroke();
|
| 369 |
-
|
| 370 |
-
ctx.translate(-margin, -startY);
|
| 371 |
-
}
|
| 372 |
-
|
| 373 |
-
const link = document.createElement('a');
|
| 374 |
-
link.download = `立体拼豆_分层图纸_${layers}层.png`;
|
| 375 |
-
link.href = expCv.toDataURL();
|
| 376 |
-
link.click();
|
| 377 |
-
}
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
// --- 原有逻辑 ---
|
| 381 |
-
function applyZoom(newScale, cx, cy) {
|
| 382 |
-
const oldScale = state.scale; state.scale = Math.max(5, Math.min(150, newScale));
|
| 383 |
-
const rect = viewport.getBoundingClientRect(); const mx = cx - rect.left; const my = cy - rect.top;
|
| 384 |
-
state.offsetX = mx - (mx - state.offsetX) * (state.scale / oldScale); state.offsetY = my - (my - state.offsetY) * (state.scale / oldScale);
|
| 385 |
draw();
|
| 386 |
}
|
| 387 |
|
|
@@ -394,7 +331,6 @@ function reprocessCurrentImg() {
|
|
| 394 |
processPixelation(state.sourceImgData, state.sourceW, state.sourceH, newWidth, newHeight);
|
| 395 |
resizeAll();
|
| 396 |
state.offsetX = (viewport.clientWidth - newWidth*state.scale)/2; state.offsetY = (viewport.clientHeight - newHeight*state.scale)/2;
|
| 397 |
-
// 如果在3D模式,重新生成3D
|
| 398 |
if(state.mode === '3d') autoGenerate3D(true);
|
| 399 |
draw();
|
| 400 |
}
|
|
@@ -421,7 +357,7 @@ function processPixelation(srcData, srcW, srcH, targetW, targetH) {
|
|
| 421 |
if(dist < bestDist) { bestDist=dist; bestP=i; }
|
| 422 |
}
|
| 423 |
state.pixels[`${x},${y}`] = bestP;
|
| 424 |
-
state.heightMap[`${x},${y}`] = 1;
|
| 425 |
if(useDither) {
|
| 426 |
const [nr, ng, nb] = paletteRGB[bestP];
|
| 427 |
const er = oldR - nr, eg = oldG - ng, eb = oldB - nb;
|
|
@@ -432,104 +368,95 @@ function processPixelation(srcData, srcW, srcH, targetW, targetH) {
|
|
| 432 |
}
|
| 433 |
}
|
| 434 |
|
| 435 |
-
function
|
| 436 |
-
const
|
| 437 |
-
const
|
| 438 |
-
|
| 439 |
-
let minDiff = Infinity, mergePair = [-1, -1];
|
| 440 |
-
const rgbs = {}; usedArr.forEach(idx => { const v = parseInt(PALETTE[idx].hex.slice(1), 16); rgbs[idx] = [(v>>16)&0xff, (v>>8)&0xff, v&0xff]; });
|
| 441 |
-
for(let i=0; i<usedArr.length; i++) {
|
| 442 |
-
for(let j=i+1; j<usedArr.length; j++) {
|
| 443 |
-
const c1 = rgbs[usedArr[i]], c2 = rgbs[usedArr[j]];
|
| 444 |
-
const dist = Math.sqrt((c1[0]-c2[0])**2 + (c1[1]-c2[1])**2 + (c1[2]-c2[2])**2);
|
| 445 |
-
if(dist < minDiff) { minDiff = dist; mergePair = [usedArr[i], usedArr[j]]; }
|
| 446 |
-
}
|
| 447 |
-
}
|
| 448 |
-
const [keep, remove] = mergePair;
|
| 449 |
-
let count = 0;
|
| 450 |
-
for(let k in state.pixels) { if(state.pixels[k] === remove) { state.pixels[k] = keep; count++; } }
|
| 451 |
-
draw(); alert(`已合并 [${PALETTE[remove].name}] 到 [${PALETTE[keep].name}]`);
|
| 452 |
-
}
|
| 453 |
-
|
| 454 |
-
function updateMaterialList() {
|
| 455 |
-
const counts = {}; let total = 0;
|
| 456 |
-
for(let key in state.pixels) { const idx = state.pixels[key]; counts[idx] = (counts[idx] || 0) + 1; total++; }
|
| 457 |
-
materialListEl.innerHTML = ''; totalBeadsEl.innerText = `${total} 颗`;
|
| 458 |
-
PALETTE.forEach((p, idx) => {
|
| 459 |
-
const count = counts[idx] || 0;
|
| 460 |
-
if(count > 0 || idx === state.curColorIdx) {
|
| 461 |
-
const item = document.createElement('div');
|
| 462 |
-
item.className = `material-item ${idx === state.curColorIdx ? 'active' : ''}`;
|
| 463 |
-
item.onclick = () => { state.curColorIdx = idx; setTool('pencil'); updateMaterialList(); };
|
| 464 |
-
item.innerHTML = `<div class="list-swatch" style="background-color: ${p.hex}"></div><div class="list-info"><b>${p.id}</b> ${p.name}</div><div class="list-count">${count}</div>`;
|
| 465 |
-
materialListEl.appendChild(item);
|
| 466 |
-
}
|
| 467 |
-
});
|
| 468 |
-
}
|
| 469 |
-
|
| 470 |
-
function resizeAll() {
|
| 471 |
-
const vw = viewport.clientWidth; const vh = viewport.clientHeight;
|
| 472 |
-
cvs.width = vw; cvs.height = vh; rulerTop.width = vw; rulerTop.height = 32; rulerLeft.width = 32; rulerLeft.height = vh;
|
| 473 |
draw();
|
| 474 |
}
|
| 475 |
|
| 476 |
function draw() {
|
| 477 |
const s = state.scale; const ox = state.offsetX; const oy = state.offsetY;
|
|
|
|
| 478 |
|
| 479 |
ctx.fillStyle = '#09090b'; ctx.fillRect(0, 0, cvs.width, cvs.height);
|
| 480 |
ctx.save(); ctx.translate(ox, oy);
|
| 481 |
|
| 482 |
-
// V9.0: 3D 渲染逻辑
|
| 483 |
if (state.mode === '3d') {
|
| 484 |
-
|
| 485 |
-
//
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
ctx.beginPath();
|
| 490 |
for(let i=0; i<=state.gridW; i++) { ctx.moveTo(i*s, 0); ctx.lineTo(i*s, state.gridH*s); }
|
| 491 |
for(let i=0; i<=state.gridH; i++) { ctx.moveTo(0, i*s); ctx.lineTo(state.gridW*s, i*s); }
|
| 492 |
ctx.stroke();
|
| 493 |
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
//
|
| 500 |
-
// 为了简化,我们直接全量绘制,但加上位移
|
| 501 |
const layerOffsetX = -offsetPerLayer * (l-1);
|
| 502 |
-
const layerOffsetY = -offsetPerLayer * (l-1);
|
| 503 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
for(let key in state.pixels) {
|
| 505 |
-
const [x, y] = key.split(',').map(Number);
|
| 506 |
const h = state.heightMap[key] || 1;
|
| 507 |
-
|
| 508 |
-
// 如果这个位置的高度 >= 当前层 l,则绘制豆子
|
| 509 |
if (h >= l) {
|
|
|
|
| 510 |
const p = PALETTE[state.pixels[key]];
|
| 511 |
-
const drawX = x*s
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
if(l < h || l < state.viewLayer) {
|
| 516 |
ctx.fillStyle = '#000';
|
| 517 |
-
ctx.beginPath(); ctx.arc(drawX + s/2 + 2, drawY + s/2 + 2, s/2, 0, Math.PI*2); ctx.fill();
|
|
|
|
| 518 |
}
|
| 519 |
|
| 520 |
-
// 实体
|
| 521 |
ctx.fillStyle = p.hex;
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
}
|
| 528 |
}
|
|
|
|
| 529 |
}
|
| 530 |
|
| 531 |
} else {
|
| 532 |
-
// --- 2D 渲染
|
| 533 |
ctx.beginPath(); ctx.lineWidth = 1; ctx.strokeStyle = '#27272a';
|
| 534 |
for(let i=0; i<=state.gridW; i++) { ctx.moveTo(i*s, 0); ctx.lineTo(i*s, state.gridH*s); }
|
| 535 |
for(let i=0; i<=state.gridH; i++) { ctx.moveTo(0, i*s); ctx.lineTo(state.gridW*s, i*s); }
|
|
@@ -541,8 +468,12 @@ function draw() {
|
|
| 541 |
const p = PALETTE[state.pixels[key]];
|
| 542 |
if(!p) continue;
|
| 543 |
ctx.fillStyle = p.hex;
|
| 544 |
-
|
| 545 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
|
| 547 |
if(showId) {
|
| 548 |
ctx.fillStyle = getContrastColor(p.hex) === '#fff' ? '#fff' : '#000';
|
|
@@ -562,6 +493,7 @@ function draw() {
|
|
| 562 |
drawRulers(); updateMaterialList();
|
| 563 |
}
|
| 564 |
|
|
|
|
| 565 |
function drawRulers() {
|
| 566 |
const s = state.scale; const ox = state.offsetX; const oy = state.offsetY;
|
| 567 |
const ctxT = ctxRTop; ctxT.clearRect(0,0,rulerTop.width,32);
|
|
@@ -585,35 +517,59 @@ function drawRulers() {
|
|
| 585 |
}
|
| 586 |
ctxL.stroke();
|
| 587 |
}
|
| 588 |
-
|
| 589 |
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'; }
|
| 590 |
-
function
|
| 591 |
-
function
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 595 |
function exportImageWithRuler() {
|
| 596 |
-
const beadSizeMM =
|
| 597 |
-
const pxPerMM = 11.8; const unit = Math.round(beadSizeMM * pxPerMM); const margin = unit * 3;
|
| 598 |
const expCvs = document.createElement('canvas'); expCvs.width = state.gridW * unit + margin; expCvs.height = state.gridH * unit + margin;
|
| 599 |
-
const eCtx = expCvs.getContext('2d');
|
| 600 |
-
eCtx.fillStyle = 'white'; eCtx.fillRect(0, 0, expCvs.width, expCvs.height);
|
| 601 |
eCtx.fillStyle = '#000'; eCtx.font = `bold ${unit*0.8}px Arial`; eCtx.textAlign = 'center'; eCtx.textBaseline = 'middle';
|
| 602 |
for(let i=0; i<=state.gridW; i++) { if(i%5===0) { const x = margin + i*unit; eCtx.fillText(i, x, margin/2); eCtx.fillRect(x, margin-unit, 4, unit); } }
|
| 603 |
for(let i=0; i<=state.gridH; i++) { if(i%5===0) { const y = margin + i*unit; eCtx.fillText(i, margin/2, y); eCtx.fillRect(margin-unit, y, unit, 4); } }
|
| 604 |
eCtx.translate(margin, margin);
|
| 605 |
for(let key in state.pixels) {
|
| 606 |
-
const [x, y] = key.split(',').map(Number); const p = PALETTE[state.pixels[key]];
|
| 607 |
-
eCtx.
|
| 608 |
eCtx.fillStyle = '#fff'; eCtx.beginPath(); eCtx.arc(x*unit + unit/2, y*unit + unit/2, unit*0.15, 0, Math.PI*2); eCtx.fill();
|
| 609 |
eCtx.fillStyle = getContrastColor(p.hex) === '#fff' ? '#fff' : '#000'; eCtx.font = `bold ${unit*0.35}px Arial`; eCtx.fillText(p.id, x*unit + unit/2, y*unit + unit/2);
|
| 610 |
}
|
| 611 |
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();
|
| 612 |
-
|
| 613 |
-
const link = document.createElement('a'); link.download = `拼豆图纸_2D.png`; link.href = expCvs.toDataURL(); link.click();
|
| 614 |
}
|
| 615 |
-
|
| 616 |
-
// 导入相关 (复用)
|
| 617 |
const modal = document.getElementById('cropModal'); const cropImg = document.getElementById('crop-img'); const cropBox = document.getElementById('crop-box'); let cropState = { startX:0, startY:0 };
|
| 618 |
function handleFile(input) { if(!input.files[0]) return; const reader = new FileReader(); 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'; } }; reader.readAsDataURL(input.files[0]); input.value = ''; }
|
| 619 |
function closeModal() { modal.style.display = 'none'; }
|
|
@@ -625,11 +581,33 @@ function confirmCrop() {
|
|
| 625 |
state.sourceImgData = tmpCtx.getImageData(0, 0, cw, ch); state.sourceW = cw; state.sourceH = ch;
|
| 626 |
reprocessCurrentImg(); closeModal();
|
| 627 |
}
|
| 628 |
-
|
| 629 |
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; };
|
| 630 |
window.onmouseup = () => { state.isDrag = false; viewport.onmousemove = null; };
|
| 631 |
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(); } };
|
| 632 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 633 |
init();
|
| 634 |
</script>
|
| 635 |
</body>
|
|
|
|
| 2 |
<html lang="zh-CN">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
+
<title>BeadMaster Pro v10.0 - 立体爆炸图</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; }
|
|
|
|
| 13 |
.panels { width: 320px; background: var(--panel); border-left: 1px solid var(--border); display: flex; flex-direction: column; z-index: 50; }
|
| 14 |
#canvas-wrapper { flex: 1; position: relative; overflow: hidden; background: #09090b; display: flex; flex-direction: column; }
|
| 15 |
|
| 16 |
+
/* 标尺 */
|
| 17 |
#ruler-top-bar { height: 32px; background: var(--ruler-bg); border-bottom: 1px solid var(--border); display: flex; z-index: 40; }
|
| 18 |
#corner-box { width: 32px; height: 32px; background: var(--ruler-bg); border-right: 1px solid var(--border); color: #888; font-size: 10px; display: flex; justify-content: center; align-items: center; flex-shrink: 0; }
|
| 19 |
#ruler-top { flex: 1; position: relative; overflow: hidden; }
|
|
|
|
| 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: 100%; 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; }
|
| 47 |
#material-list { flex: 1; overflow-y: auto; padding: 5px 0; }
|
| 48 |
.material-item { display: flex; align-items: center; padding: 8px 15px; gap: 10px; border-bottom: 1px solid #333; font-size: 13px; cursor: pointer; transition: 0.2s; }
|
| 49 |
.material-item:hover { background: #3f3f46; }
|
| 50 |
+
.material-item.active { background: #312e81; border-left: 3px solid var(--accent); }
|
| 51 |
.list-swatch { width: 22px; height: 22px; border-radius: 4px; border: 1px solid rgba(255,255,255,0.2); flex-shrink: 0; }
|
| 52 |
.list-info { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
| 53 |
.list-count { font-family: monospace; font-weight: bold; color: var(--accent); font-size: 14px; }
|
| 54 |
#total-stats { padding: 15px; border-top: 1px solid var(--border); background: #202022; font-size: 13px; display: flex; justify-content: space-between; font-weight: bold; color: var(--success); }
|
| 55 |
|
| 56 |
+
/* 3D 模式切换器 */
|
| 57 |
+
.mode-switch { display: flex; background: #18181b; padding: 4px; border-radius: 6px; border: 1px solid var(--border); width: 140px; }
|
| 58 |
+
.mode-btn { flex: 1; padding: 6px; text-align: center; font-size: 12px; cursor: pointer; border-radius: 4px; color: #888; transition: 0.2s; }
|
| 59 |
+
.mode-btn.active { background: var(--accent); color: white; font-weight: bold; box-shadow: 0 2px 5px rgba(0,0,0,0.3); }
|
| 60 |
|
| 61 |
+
/* 3D 控制滑块区 */
|
| 62 |
+
.slider-row { margin-bottom: 15px; }
|
| 63 |
+
.slider-header { display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 5px; color: #ccc; }
|
| 64 |
|
| 65 |
+
/* 弹窗 */
|
| 66 |
.modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.9); z-index: 100; display: none; justify-content: center; align-items: center; backdrop-filter: blur(5px); }
|
| 67 |
+
.modal-content { background: var(--panel); padding: 20px; border-radius: 12px; width: 80vw; height: 85vh; display: flex; flex-direction: column; gap: 15px; border: 1px solid var(--border); box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5); }
|
| 68 |
#crop-container { flex: 1; background: #000; position: relative; overflow: hidden; display: flex; justify-content: center; align-items: center; border: 1px solid #3f3f46; border-radius: 4px; }
|
| 69 |
#crop-img { max-width: 100%; max-height: 100%; display: block; }
|
| 70 |
#crop-box { position: absolute; border: 2px dashed #fff; box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.6); cursor: move; display: none; }
|
|
|
|
| 76 |
|
| 77 |
<div class="modal-overlay" id="cropModal">
|
| 78 |
<div class="modal-content">
|
| 79 |
+
<h3 style="margin:0; color: white;">✂️ 图片导入向导</h3>
|
| 80 |
<div id="crop-container" onmousedown="startCropDrag(event)">
|
| 81 |
<img id="crop-img">
|
| 82 |
<div id="crop-box">
|
|
|
|
| 86 |
<div class="modal-footer">
|
| 87 |
<div style="display: flex; gap: 20px; align-items: center;">
|
| 88 |
<label style="display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer;">
|
| 89 |
+
<input type="checkbox" id="ditherCheck" checked> <span>色彩融合 (Dithering)</span>
|
| 90 |
</label>
|
| 91 |
</div>
|
| 92 |
<div style="display: flex; gap: 10px;">
|
|
|
|
| 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.0</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>
|
|
|
|
| 116 |
<div style="width:1px; height:24px; background:var(--border);"></div>
|
| 117 |
|
| 118 |
<div class="control-group">
|
| 119 |
+
<div class="control-label" style="color: var(--success);">🎯 精度</div>
|
| 120 |
<input type="number" id="targetRes" value="60" min="10" max="300" onchange="reprocessCurrentImg()" onkeydown="if(event.key==='Enter') reprocessCurrentImg()">
|
| 121 |
</div>
|
| 122 |
|
|
|
|
| 133 |
|
| 134 |
<div class="mode-switch">
|
| 135 |
<div class="mode-btn active" id="mode-2d" onclick="setMode('2d')">2D 平面</div>
|
| 136 |
+
<div class="mode-btn" id="mode-3d" onclick="setMode('3d')">3D 爆炸</div>
|
| 137 |
+
</div>
|
| 138 |
+
|
| 139 |
+
<div class="control-group">
|
| 140 |
+
<div class="control-label">形状</div>
|
| 141 |
+
<select id="shapeSelect" onchange="draw()">
|
| 142 |
+
<option value="circle">🔵 圆形</option>
|
| 143 |
+
<option value="square">⬛ 方块</option>
|
| 144 |
+
</select>
|
| 145 |
</div>
|
| 146 |
|
| 147 |
<div style="flex:1;"></div>
|
| 148 |
|
| 149 |
+
<button class="action-btn btn-purple" id="btn-export-2d" onclick="exportImageWithRuler()">💾 导出平面</button>
|
| 150 |
+
<button class="action-btn btn-purple" id="btn-export-3d" onclick="exportSlices()" style="display:none;">📑 导出分层</button>
|
| 151 |
<button class="action-btn" onclick="clearCanvas()">🗑️</button>
|
| 152 |
</div>
|
| 153 |
|
|
|
|
| 166 |
</div>
|
| 167 |
|
| 168 |
<div class="panels">
|
| 169 |
+
<div id="panel-3d-controls" style="padding: 15px; background: #202022; border-bottom: 1px solid var(--border); display: none;">
|
| 170 |
+
|
| 171 |
+
<button class="action-btn btn-green" style="width:100%; margin-bottom:15px;" onclick="autoGenerate3D()">
|
| 172 |
+
🧊 一键生成立体层级
|
| 173 |
+
</button>
|
| 174 |
+
|
| 175 |
+
<div class="slider-row">
|
| 176 |
+
<div class="slider-header">
|
| 177 |
+
<span>💥 爆炸/间距 (Explode)</span>
|
| 178 |
+
<span id="explode-val">0%</span>
|
| 179 |
+
</div>
|
| 180 |
+
<input type="range" id="explodeSlider" min="0" max="100" value="0" oninput="updateExplode(this.value)">
|
| 181 |
</div>
|
| 182 |
+
|
| 183 |
+
<div class="slider-row">
|
| 184 |
+
<div class="slider-header">
|
| 185 |
+
<span>🍰 显示层级 (Layers)</span>
|
| 186 |
+
<span id="layer-display">1 / 1</span>
|
| 187 |
+
</div>
|
| 188 |
+
<input type="range" id="layerSlider" min="1" max="1" value="1" oninput="updateLayerView(this.value)">
|
| 189 |
+
|
| 190 |
+
<label style="display:flex; align-items:center; gap:8px; margin-top:8px; font-size:12px; color:#aaa; cursor:pointer;">
|
| 191 |
+
<input type="checkbox" id="singleLayerCheck" onchange="draw()">
|
| 192 |
+
👁️ 仅看当前层 (专注模式)
|
| 193 |
+
</label>
|
| 194 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
</div>
|
| 196 |
|
| 197 |
<div class="panel-header">
|
|
|
|
| 226 |
|
| 227 |
const state = {
|
| 228 |
gridW: 60, gridH: 60,
|
| 229 |
+
pixels: {}, heightMap: {},
|
|
|
|
| 230 |
scale: 25,
|
| 231 |
offsetX: 50, offsetY: 50,
|
| 232 |
+
tool: 'pencil', curColorIdx: 2,
|
|
|
|
| 233 |
isDrag: false, lastX: 0, lastY: 0,
|
| 234 |
sourceImgData: null, sourceW: 0, sourceH: 0,
|
| 235 |
+
mode: '2d',
|
| 236 |
+
maxLayers: 1, viewLayer: 1, explodeVal: 0
|
|
|
|
| 237 |
};
|
| 238 |
|
| 239 |
// DOM
|
|
|
|
| 249 |
|
| 250 |
function init() {
|
| 251 |
window.addEventListener('resize', resizeAll);
|
| 252 |
+
resizeAll(); updateMaterialList(); draw();
|
|
|
|
|
|
|
| 253 |
viewport.addEventListener('wheel', e => {
|
| 254 |
+
e.preventDefault(); const delta = e.deltaY > 0 ? 0.9 : 1.1;
|
|
|
|
| 255 |
applyZoom(state.scale * delta, e.clientX, e.clientY);
|
| 256 |
}, {passive:false});
|
| 257 |
}
|
| 258 |
|
| 259 |
+
// --- 3D 逻辑 ---
|
| 260 |
function setMode(mode) {
|
| 261 |
state.mode = mode;
|
| 262 |
document.querySelectorAll('.mode-btn').forEach(b => b.classList.remove('active'));
|
| 263 |
document.getElementById(`mode-${mode}`).classList.add('active');
|
| 264 |
|
|
|
|
| 265 |
const controls3d = document.getElementById('panel-3d-controls');
|
| 266 |
const btnExp2d = document.getElementById('btn-export-2d');
|
| 267 |
const btnExp3d = document.getElementById('btn-export-3d');
|
|
|
|
| 270 |
controls3d.style.display = 'block';
|
| 271 |
btnExp2d.style.display = 'none';
|
| 272 |
btnExp3d.style.display = 'flex';
|
| 273 |
+
if(state.maxLayers === 1) autoGenerate3D(true);
|
|
|
|
| 274 |
} else {
|
| 275 |
controls3d.style.display = 'none';
|
| 276 |
btnExp2d.style.display = 'flex';
|
|
|
|
| 280 |
}
|
| 281 |
|
| 282 |
function autoGenerate3D(silent = false) {
|
| 283 |
+
if(!state.sourceImgData) { if(!silent) alert("请先导入图片!"); return; }
|
| 284 |
+
const targetW = state.gridW; const targetH = state.gridH;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
const tmpCv = document.createElement('canvas'); tmpCv.width = targetW; tmpCv.height = targetH;
|
| 286 |
const tmpCtx = tmpCv.getContext('2d');
|
| 287 |
const srcCv = document.createElement('canvas'); srcCv.width = state.sourceW; srcCv.height = state.sourceH;
|
|
|
|
| 289 |
tmpCtx.drawImage(srcCv, 0, 0, targetW, targetH);
|
| 290 |
const data = tmpCtx.getImageData(0, 0, targetW, targetH).data;
|
| 291 |
|
|
|
|
| 292 |
state.heightMap = {};
|
| 293 |
+
const layers = 6; state.maxLayers = layers;
|
|
|
|
|
|
|
| 294 |
for(let y=0; y<targetH; y++) {
|
| 295 |
for(let x=0; x<targetW; x++) {
|
| 296 |
const idx = (y*targetW + x)*4;
|
| 297 |
const r = data[idx], g = data[idx+1], b = data[idx+2], a = data[idx+3];
|
| 298 |
if(a < 128) continue;
|
|
|
|
|
|
|
| 299 |
const luma = 0.299*r + 0.587*g + 0.114*b;
|
| 300 |
+
let h = Math.ceil((luma / 255) * layers); if(h < 1) h = 1;
|
|
|
|
|
|
|
|
|
|
| 301 |
state.heightMap[`${x},${y}`] = h;
|
| 302 |
}
|
| 303 |
}
|
| 304 |
|
|
|
|
| 305 |
document.getElementById('layerSlider').max = layers;
|
| 306 |
document.getElementById('layerSlider').value = layers;
|
| 307 |
state.viewLayer = layers;
|
| 308 |
+
document.getElementById('layer-display').innerText = `${layers} / ${layers}`;
|
| 309 |
|
| 310 |
if(!silent) draw();
|
| 311 |
}
|
| 312 |
|
| 313 |
function updateLayerView(val) {
|
| 314 |
state.viewLayer = parseInt(val);
|
| 315 |
+
document.getElementById('layer-display').innerText = `${val} / ${state.maxLayers}`;
|
| 316 |
draw();
|
| 317 |
}
|
| 318 |
|
| 319 |
+
function updateExplode(val) {
|
| 320 |
+
state.explodeVal = parseInt(val);
|
| 321 |
+
document.getElementById('explode-val').innerText = `${val}%`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
draw();
|
| 323 |
}
|
| 324 |
|
|
|
|
| 331 |
processPixelation(state.sourceImgData, state.sourceW, state.sourceH, newWidth, newHeight);
|
| 332 |
resizeAll();
|
| 333 |
state.offsetX = (viewport.clientWidth - newWidth*state.scale)/2; state.offsetY = (viewport.clientHeight - newHeight*state.scale)/2;
|
|
|
|
| 334 |
if(state.mode === '3d') autoGenerate3D(true);
|
| 335 |
draw();
|
| 336 |
}
|
|
|
|
| 357 |
if(dist < bestDist) { bestDist=dist; bestP=i; }
|
| 358 |
}
|
| 359 |
state.pixels[`${x},${y}`] = bestP;
|
| 360 |
+
state.heightMap[`${x},${y}`] = 1;
|
| 361 |
if(useDither) {
|
| 362 |
const [nr, ng, nb] = paletteRGB[bestP];
|
| 363 |
const er = oldR - nr, eg = oldG - ng, eb = oldB - nb;
|
|
|
|
| 368 |
}
|
| 369 |
}
|
| 370 |
|
| 371 |
+
function applyZoom(newScale, cx, cy) {
|
| 372 |
+
const oldScale = state.scale; state.scale = Math.max(5, Math.min(150, newScale));
|
| 373 |
+
const rect = viewport.getBoundingClientRect(); const mx = cx - rect.left; const my = cy - rect.top;
|
| 374 |
+
state.offsetX = mx - (mx - state.offsetX) * (state.scale / oldScale); state.offsetY = my - (my - state.offsetY) * (state.scale / oldScale);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
draw();
|
| 376 |
}
|
| 377 |
|
| 378 |
function draw() {
|
| 379 |
const s = state.scale; const ox = state.offsetX; const oy = state.offsetY;
|
| 380 |
+
const shape = document.getElementById('shapeSelect').value;
|
| 381 |
|
| 382 |
ctx.fillStyle = '#09090b'; ctx.fillRect(0, 0, cvs.width, cvs.height);
|
| 383 |
ctx.save(); ctx.translate(ox, oy);
|
| 384 |
|
|
|
|
| 385 |
if (state.mode === '3d') {
|
| 386 |
+
const offsetPerLayer = s * 0.15;
|
| 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) {
|
| 412 |
+
const [x, y] = key.split(',').map(Number);
|
| 413 |
+
if(shape==='circle') { ctx.beginPath(); ctx.arc(x*s+s/2, y*s+s/2, s/2, 0, Math.PI*2); ctx.fill(); }
|
| 414 |
+
else { ctx.fillRect(x*s, y*s, s, s); }
|
| 415 |
+
}
|
| 416 |
+
}
|
| 417 |
+
ctx.restore();
|
| 418 |
+
}
|
| 419 |
+
|
| 420 |
+
ctx.save();
|
| 421 |
+
ctx.translate(layerOffsetX, layerOffsetY);
|
| 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(); }
|
| 435 |
+
else { ctx.fillRect(drawX+2, drawY+2, s, s); }
|
| 436 |
}
|
| 437 |
|
|
|
|
| 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(255,255,255,0.15)';
|
| 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 |
}
|
| 455 |
+
ctx.restore();
|
| 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); }
|
|
|
|
| 468 |
const p = PALETTE[state.pixels[key]];
|
| 469 |
if(!p) continue;
|
| 470 |
ctx.fillStyle = p.hex;
|
| 471 |
+
if(shape === 'circle') {
|
| 472 |
+
ctx.beginPath(); ctx.arc(x*s + s/2, y*s + s/2, s/2 - s*0.05, 0, Math.PI*2); ctx.fill();
|
| 473 |
+
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(); }
|
| 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';
|
|
|
|
| 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 |
}
|
| 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() {
|
| 523 |
+
const used = new Set(); for(let k in state.pixels) used.add(state.pixels[k]);
|
| 524 |
+
const usedArr = Array.from(used);
|
| 525 |
+
if(usedArr.length < 2) { alert("颜色太少无法合并"); return; }
|
| 526 |
+
let minDiff = Infinity, mergePair = [-1, -1];
|
| 527 |
+
const rgbs = {}; usedArr.forEach(idx => { const v = parseInt(PALETTE[idx].hex.slice(1), 16); rgbs[idx] = [(v>>16)&0xff, (v>>8)&0xff, v&0xff]; });
|
| 528 |
+
for(let i=0; i<usedArr.length; i++) { for(let j=i+1; j<usedArr.length; j++) { const c1 = rgbs[usedArr[i]], c2 = rgbs[usedArr[j]]; const dist = Math.sqrt((c1[0]-c2[0])**2 + (c1[1]-c2[1])**2 + (c1[2]-c2[2])**2); if(dist < minDiff) { minDiff = dist; mergePair = [usedArr[i], usedArr[j]]; } } }
|
| 529 |
+
const [keep, remove] = mergePair;
|
| 530 |
+
let count = 0; for(let k in state.pixels) { if(state.pixels[k] === remove) { state.pixels[k] = keep; count++; } }
|
| 531 |
+
draw(); alert(`已合并 [${PALETTE[remove].name}] 到 [${PALETTE[keep].name}]`);
|
| 532 |
+
}
|
| 533 |
+
function exportSlices() {
|
| 534 |
+
const layers = state.maxLayers; const unit = 20; const w = state.gridW * unit; const h = state.gridH * unit; const margin = 40;
|
| 535 |
+
const expCv = document.createElement('canvas'); expCv.width = w + margin*2; expCv.height = (h + margin) * layers + margin;
|
| 536 |
+
const ctx = expCv.getContext('2d'); ctx.fillStyle = 'white'; ctx.fillRect(0,0,expCv.width, expCv.height);
|
| 537 |
+
for(let l=1; l<=layers; l++) {
|
| 538 |
+
const startY = (l-1)*(h+margin) + margin;
|
| 539 |
+
ctx.fillStyle = '#000'; ctx.font = '20px Arial'; ctx.textAlign = 'left'; ctx.fillText(`第 ${l} 层 (Layer ${l})`, margin, startY - 10);
|
| 540 |
+
ctx.translate(margin, startY);
|
| 541 |
+
for(let key in state.pixels) {
|
| 542 |
+
const hVal = state.heightMap[key] || 1;
|
| 543 |
+
if(hVal >= l) {
|
| 544 |
+
const [x, y] = key.split(',').map(Number); const p = PALETTE[state.pixels[key]];
|
| 545 |
+
ctx.fillStyle = p.hex; ctx.beginPath(); ctx.arc(x*unit + unit/2, y*unit + unit/2, unit/2 - 1, 0, Math.PI*2); ctx.fill();
|
| 546 |
+
ctx.fillStyle = '#fff'; ctx.beginPath(); ctx.arc(x*unit + unit/2, y*unit + unit/2, unit/6, 0, Math.PI*2); ctx.fill();
|
| 547 |
+
}
|
| 548 |
+
}
|
| 549 |
+
ctx.strokeStyle = '#ddd'; ctx.lineWidth=1;
|
| 550 |
+
for(let i=0; i<=state.gridW; i++) { ctx.moveTo(i*unit, 0); ctx.lineTo(i*unit, h); } for(let i=0; i<=state.gridH; i++) { ctx.moveTo(0, i*unit); ctx.lineTo(w, i*unit); } ctx.stroke();
|
| 551 |
+
ctx.translate(-margin, -startY);
|
| 552 |
+
}
|
| 553 |
+
const link = document.createElement('a'); link.download = `立体拼豆_分层图纸.png`; link.href = expCv.toDataURL(); link.click();
|
| 554 |
+
}
|
| 555 |
function exportImageWithRuler() {
|
| 556 |
+
const beadSizeMM = 5; const pxPerMM = 11.8; const unit = Math.round(beadSizeMM * pxPerMM); const margin = unit * 3;
|
|
|
|
| 557 |
const expCvs = document.createElement('canvas'); expCvs.width = state.gridW * unit + margin; expCvs.height = state.gridH * unit + margin;
|
| 558 |
+
const eCtx = expCvs.getContext('2d'); eCtx.fillStyle = 'white'; eCtx.fillRect(0, 0, expCvs.width, expCvs.height);
|
|
|
|
| 559 |
eCtx.fillStyle = '#000'; eCtx.font = `bold ${unit*0.8}px Arial`; eCtx.textAlign = 'center'; eCtx.textBaseline = 'middle';
|
| 560 |
for(let i=0; i<=state.gridW; i++) { if(i%5===0) { const x = margin + i*unit; eCtx.fillText(i, x, margin/2); eCtx.fillRect(x, margin-unit, 4, unit); } }
|
| 561 |
for(let i=0; i<=state.gridH; i++) { if(i%5===0) { const y = margin + i*unit; eCtx.fillText(i, margin/2, y); eCtx.fillRect(margin-unit, y, unit, 4); } }
|
| 562 |
eCtx.translate(margin, margin);
|
| 563 |
for(let key in state.pixels) {
|
| 564 |
+
const [x, y] = key.split(',').map(Number); const p = PALETTE[state.pixels[key]]; eCtx.fillStyle = p.hex;
|
| 565 |
+
eCtx.beginPath(); eCtx.arc(x*unit + unit/2, y*unit + unit/2, unit/2 - unit*0.05, 0, Math.PI*2); eCtx.fill();
|
| 566 |
eCtx.fillStyle = '#fff'; eCtx.beginPath(); eCtx.arc(x*unit + unit/2, y*unit + unit/2, unit*0.15, 0, Math.PI*2); eCtx.fill();
|
| 567 |
eCtx.fillStyle = getContrastColor(p.hex) === '#fff' ? '#fff' : '#000'; eCtx.font = `bold ${unit*0.35}px Arial`; eCtx.fillText(p.id, x*unit + unit/2, y*unit + unit/2);
|
| 568 |
}
|
| 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) { if(!input.files[0]) return; const reader = new FileReader(); 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'; } }; reader.readAsDataURL(input.files[0]); input.value = ''; }
|
| 575 |
function closeModal() { modal.style.display = 'none'; }
|
|
|
|
| 581 |
state.sourceImgData = tmpCtx.getImageData(0, 0, cw, ch); state.sourceW = cw; state.sourceH = ch;
|
| 582 |
reprocessCurrentImg(); closeModal();
|
| 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;
|
| 595 |
+
else if(state.tool === 'eraser') delete state.pixels[key];
|
| 596 |
+
else if(state.tool === 'bucket') floodFill(x, y, state.curColorIdx);
|
| 597 |
+
else if(state.tool === 'picker') { if(state.pixels[key] !== undefined) selectColor(state.pixels[key]); setTool('pencil'); }
|
| 598 |
+
draw();
|
| 599 |
+
}
|
| 600 |
+
function floodFill(x, y, targetIdx) {
|
| 601 |
+
const oldIdx = state.pixels[`${x},${y}`]; if(oldIdx === targetIdx) return;
|
| 602 |
+
const q = [[x,y]]; let count = 0;
|
| 603 |
+
while(q.length && count < state.gridW * state.gridH) {
|
| 604 |
+
count++; const [cx, cy] = q.pop(); const k = `${cx},${cy}`;
|
| 605 |
+
if(cx<0 || cx>=state.gridW || cy<0 || cy>=state.gridH) continue;
|
| 606 |
+
if(state.pixels[k] !== oldIdx) continue;
|
| 607 |
+
state.pixels[k] = targetIdx; q.push([cx+1,cy], [cx-1,cy], [cx,cy+1], [cx,cy-1]);
|
| 608 |
+
}
|
| 609 |
+
draw();
|
| 610 |
+
}
|
| 611 |
init();
|
| 612 |
</script>
|
| 613 |
</body>
|