Tweak spritesheet page

This commit is contained in:
dingo
2025-12-29 15:39:52 -05:00
parent bc035c9967
commit 6e928bc26a
+228 -1
View File
@@ -76,13 +76,69 @@
</div>
</div>
@if (!string.IsNullOrEmpty(Model.Sheet.CdnPath))
@* @if (!string.IsNullOrEmpty(Model.Sheet.CdnPath))
{
<div class="mt-4 p-4 bg-gray-800 rounded-lg">
<p class="text-sm text-gray-400 mb-2">Preview (Sheet: @Model.Sheet.SheetWidth x @Model.Sheet.SheetHeight, Frame: @Model.Sheet.FrameWidth x @Model.Sheet.FrameHeight = @Model.Sheet.TotalFrames frames)</p>
<img src="@Model.CdnBaseUrl/@Model.Sheet.CdnPath" alt="Sprite sheet preview" class="max-h-64 object-contain" style="image-rendering: pixelated;" />
</div>
} *@
@if (!string.IsNullOrEmpty(Model.Sheet.CdnPath))
{
<div class="mt-4 grid grid-cols-1 lg:grid-cols-2 gap-4">
<!-- Preview pane -->
<div class="p-4 bg-gray-800 rounded-lg">
<div class="flex items-center justify-between mb-2">
<p class="text-sm text-gray-400">
Preview (Sheet: @Model.Sheet.SheetWidth x @Model.Sheet.SheetHeight,
Frame: @Model.Sheet.FrameWidth x @Model.Sheet.FrameHeight = @Model.Sheet.TotalFrames frames)
</p>
<div class="flex items-center gap-2">
<label class="text-xs text-gray-400">Zoom</label>
<input id="sheetZoom" type="range" min="1" max="10" value="3" class="w-28" />
</div>
</div>
<!-- Hidden image source -->
<img id="sheetImg"
src="@Model.CdnBaseUrl/@Model.Sheet.CdnPath"
alt="Sprite sheet"
class="hidden"
style="image-rendering: pixelated;" />
<!-- Canvas preview -->
<div class="overflow-auto rounded border border-gray-700 bg-gray-900">
<canvas id="sheetCanvas" class="block"></canvas>
</div>
<div class="mt-2 text-xs text-gray-400 flex items-center justify-between">
<span>Click a cell to copy its frame index.</span>
<span id="sheetMeta"></span>
</div>
</div>
<!-- Optional: selection / debug pane -->
<div class="p-4 bg-gray-800 rounded-lg">
<h3 class="text-sm font-semibold text-white mb-2">Selected Frame</h3>
<div class="text-sm text-gray-300 space-y-2">
<div>Frame Index: <span id="selFrameIdx" class="font-mono text-white">-</span></div>
<div>Row/Col: <span id="selFrameRC" class="font-mono text-white">-</span></div>
<div>Pixel: <span id="selFramePx" class="font-mono text-white">-</span></div>
</div>
<div class="mt-4">
<h3 class="text-sm font-semibold text-white mb-2">Quick Insert</h3>
<p class="text-xs text-gray-400 mb-2">
This just copies; you can paste into any “Frames” sequence.
(If you want auto-insert into the last-focused frame input, we can do that too.)
</p>
<input id="copyBuffer" class="input font-mono" readonly value="" placeholder="Click a frame..." />
</div>
</div>
</div>
}
</div>
<div class="flex gap-4">
@@ -289,4 +345,175 @@
}
});
</script>
<script>
(() => {
const img = document.getElementById('sheetImg');
const canvas = document.getElementById('sheetCanvas');
const ctx = canvas.getContext('2d');
const zoomEl = document.getElementById('sheetZoom');
const metaEl = document.getElementById('sheetMeta');
const selFrameIdxEl = document.getElementById('selFrameIdx');
const selFrameRCEl = document.getElementById('selFrameRC');
const selFramePxEl = document.getElementById('selFramePx');
const copyBufferEl = document.getElementById('copyBuffer');
// Read from your inputs so preview updates before save
const getNum = (id, fallback) => {
const el = document.getElementById(id);
const v = el ? parseInt(el.value, 10) : NaN;
return Number.isFinite(v) && v > 0 ? v : fallback;
};
function getSheetConfig() {
const sheetW = getNum('Sheet_SheetWidth', img.naturalWidth || 1);
const sheetH = getNum('Sheet_SheetHeight', img.naturalHeight || 1);
const frameW = getNum('Sheet_FrameWidth', 64);
const frameH = getNum('Sheet_FrameHeight', 64);
const cols = Math.max(1, Math.floor(sheetW / frameW));
const rows = Math.max(1, Math.floor(sheetH / frameH));
const total = cols * rows;
return { sheetW, sheetH, frameW, frameH, cols, rows, total };
}
function draw() {
if (!img || !img.complete || img.naturalWidth === 0) return;
const zoom = parseInt(zoomEl?.value || '3', 10);
const { sheetW, sheetH, frameW, frameH, cols, rows, total } = getSheetConfig();
// Canvas pixel size = scaled (so it stays crisp)
canvas.width = sheetW * zoom;
canvas.height = sheetH * zoom;
// Crisp scaling
ctx.imageSmoothingEnabled = false;
// Draw sprite sheet scaled
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, sheetW * zoom, sheetH * zoom);
// Grid lines
ctx.lineWidth = 1;
ctx.strokeStyle = 'rgba(255,255,255,0.18)';
for (let c = 0; c <= cols; c++) {
const x = c * frameW * zoom + 0.5;
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, rows * frameH * zoom);
ctx.stroke();
}
for (let r = 0; r <= rows; r++) {
const y = r * frameH * zoom + 0.5;
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(cols * frameW * zoom, y);
ctx.stroke();
}
// Frame numbers
// Draw a subtle shadow box + text so it reads on any background
ctx.font = `${Math.max(10, 10 * zoom)}px ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace`;
ctx.textBaseline = 'top';
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
const idx = r * cols + c;
const px = c * frameW * zoom;
const py = r * frameH * zoom;
// background pill
const pad = 2 * zoom;
const label = String(idx);
const metrics = ctx.measureText(label);
const boxW = metrics.width + pad * 2;
const boxH = (12 * zoom) + pad * 2;
ctx.fillStyle = 'rgba(0,0,0,0.45)';
ctx.fillRect(px + 2, py + 2, boxW, boxH);
// text
ctx.fillStyle = 'rgba(255,255,255,0.90)';
ctx.fillText(label, px + 2 + pad, py + 2 + pad);
}
}
if (metaEl) {
metaEl.textContent = `${cols} × ${rows} = ${total} frames`;
}
}
function canvasToFrameIndex(evt) {
const zoom = parseInt(zoomEl?.value || '3', 10);
const { frameW, frameH, cols, rows } = getSheetConfig();
const rect = canvas.getBoundingClientRect();
const x = Math.floor((evt.clientX - rect.left) / zoom);
const y = Math.floor((evt.clientY - rect.top) / zoom);
const col = Math.floor(x / frameW);
const row = Math.floor(y / frameH);
if (col < 0 || col >= cols || row < 0 || row >= rows) return null;
const idx = row * cols + col;
return { idx, row, col, x, y };
}
function onClick(evt) {
const hit = canvasToFrameIndex(evt);
if (!hit) return;
selFrameIdxEl && (selFrameIdxEl.textContent = hit.idx);
selFrameRCEl && (selFrameRCEl.textContent = `${hit.row},${hit.col}`);
selFramePxEl && (selFramePxEl.textContent = `${hit.x},${hit.y}`);
if (copyBufferEl) copyBufferEl.value = hit.idx;
// Copy to clipboard (best-effort)
if (navigator.clipboard?.writeText) {
navigator.clipboard.writeText(String(hit.idx)).catch(() => {});
}
}
// Hook redraw to changes in relevant inputs
const watchIds = [
'Sheet_SheetWidth', 'Sheet_SheetHeight',
'Sheet_FrameWidth', 'Sheet_FrameHeight',
'Sheet_CdnPath'
];
function hookRedraw() {
watchIds.forEach(id => {
const el = document.getElementById(id);
if (!el) return;
el.addEventListener('input', () => {
// If CDN path changes, update img src and redraw on load
if (id === 'Sheet_CdnPath') {
// your CDN base is server-side; we won’t rebuild URL here.
// Prefer: only redraw after postback OR implement a client-side base variable.
// For now: just redraw grid w/ existing img.
}
draw();
});
});
zoomEl?.addEventListener('input', draw);
canvas?.addEventListener('click', onClick);
}
if (!img || !canvas || !ctx) return;
img.addEventListener('load', draw);
if (img.complete) draw();
hookRedraw();
})();
</script>
}