Tweak spritesheet page
This commit is contained in:
@@ -216,7 +216,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
<!-- Frame Reference -->
|
<!-- Frame Reference -->
|
||||||
<div class="mt-4 p-4 bg-gray-900 rounded-lg">
|
@* <div class="mt-4 p-4 bg-gray-900 rounded-lg">
|
||||||
<p class="text-sm text-gray-400 mb-2">Frame Reference (frames are numbered 0 to @(Model.Sheet.TotalFrames - 1), left-to-right, top-to-bottom)</p>
|
<p class="text-sm text-gray-400 mb-2">Frame Reference (frames are numbered 0 to @(Model.Sheet.TotalFrames - 1), left-to-right, top-to-bottom)</p>
|
||||||
<div class="grid gap-1" style="grid-template-columns: repeat(@Model.Sheet.Columns, minmax(0, 1fr)); max-width: fit-content;">
|
<div class="grid gap-1" style="grid-template-columns: repeat(@Model.Sheet.Columns, minmax(0, 1fr)); max-width: fit-content;">
|
||||||
@for (int i = 0; i < Model.Sheet.TotalFrames; i++)
|
@for (int i = 0; i < Model.Sheet.TotalFrames; i++)
|
||||||
@@ -224,7 +224,7 @@
|
|||||||
<div class="w-8 h-8 bg-gray-700 rounded text-xs flex items-center justify-center text-gray-300">@i</div>
|
<div class="w-8 h-8 bg-gray-700 rounded text-xs flex items-center justify-center text-gray-300">@i</div>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div> *@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -345,175 +345,175 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
(() => {
|
(() => {
|
||||||
const img = document.getElementById('sheetImg');
|
const img = document.getElementById('sheetImg');
|
||||||
const canvas = document.getElementById('sheetCanvas');
|
const canvas = document.getElementById('sheetCanvas');
|
||||||
const ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
|
|
||||||
const zoomEl = document.getElementById('sheetZoom');
|
const zoomEl = document.getElementById('sheetZoom');
|
||||||
const metaEl = document.getElementById('sheetMeta');
|
const metaEl = document.getElementById('sheetMeta');
|
||||||
|
|
||||||
const selFrameIdxEl = document.getElementById('selFrameIdx');
|
const selFrameIdxEl = document.getElementById('selFrameIdx');
|
||||||
const selFrameRCEl = document.getElementById('selFrameRC');
|
const selFrameRCEl = document.getElementById('selFrameRC');
|
||||||
const selFramePxEl = document.getElementById('selFramePx');
|
const selFramePxEl = document.getElementById('selFramePx');
|
||||||
const copyBufferEl = document.getElementById('copyBuffer');
|
const copyBufferEl = document.getElementById('copyBuffer');
|
||||||
|
|
||||||
// Read from your inputs so preview updates before save
|
// Read from your inputs so preview updates before save
|
||||||
const getNum = (id, fallback) => {
|
const getNum = (id, fallback) => {
|
||||||
const el = document.getElementById(id);
|
const el = document.getElementById(id);
|
||||||
const v = el ? parseInt(el.value, 10) : NaN;
|
const v = el ? parseInt(el.value, 10) : NaN;
|
||||||
return Number.isFinite(v) && v > 0 ? v : fallback;
|
return Number.isFinite(v) && v > 0 ? v : fallback;
|
||||||
};
|
};
|
||||||
|
|
||||||
function getSheetConfig() {
|
function getSheetConfig() {
|
||||||
const sheetW = getNum('Sheet_SheetWidth', img.naturalWidth || 1);
|
const sheetW = getNum('Sheet_SheetWidth', img.naturalWidth || 1);
|
||||||
const sheetH = getNum('Sheet_SheetHeight', img.naturalHeight || 1);
|
const sheetH = getNum('Sheet_SheetHeight', img.naturalHeight || 1);
|
||||||
const frameW = getNum('Sheet_FrameWidth', 64);
|
const frameW = getNum('Sheet_FrameWidth', 64);
|
||||||
const frameH = getNum('Sheet_FrameHeight', 64);
|
const frameH = getNum('Sheet_FrameHeight', 64);
|
||||||
|
|
||||||
const cols = Math.max(1, Math.floor(sheetW / frameW));
|
const cols = Math.max(1, Math.floor(sheetW / frameW));
|
||||||
const rows = Math.max(1, Math.floor(sheetH / frameH));
|
const rows = Math.max(1, Math.floor(sheetH / frameH));
|
||||||
const total = cols * rows;
|
const total = cols * rows;
|
||||||
|
|
||||||
return { sheetW, sheetH, frameW, frameH, cols, rows, total };
|
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);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function draw() {
|
if (metaEl) {
|
||||||
if (!img || !img.complete || img.naturalWidth === 0) return;
|
metaEl.textContent = `${cols} × ${rows} = ${total} frames`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const zoom = parseInt(zoomEl?.value || '3', 10);
|
function canvasToFrameIndex(evt) {
|
||||||
const { sheetW, sheetH, frameW, frameH, cols, rows, total } = getSheetConfig();
|
const zoom = parseInt(zoomEl?.value || '3', 10);
|
||||||
|
const { frameW, frameH, cols, rows } = getSheetConfig();
|
||||||
|
|
||||||
// Canvas pixel size = scaled (so it stays crisp)
|
const rect = canvas.getBoundingClientRect();
|
||||||
canvas.width = sheetW * zoom;
|
const x = Math.floor((evt.clientX - rect.left) / zoom);
|
||||||
canvas.height = sheetH * zoom;
|
const y = Math.floor((evt.clientY - rect.top) / zoom);
|
||||||
|
|
||||||
// Crisp scaling
|
const col = Math.floor(x / frameW);
|
||||||
ctx.imageSmoothingEnabled = false;
|
const row = Math.floor(y / frameH);
|
||||||
|
|
||||||
// Draw sprite sheet scaled
|
if (col < 0 || col >= cols || row < 0 || row >= rows) return null;
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
||||||
ctx.drawImage(img, 0, 0, sheetW * zoom, sheetH * zoom);
|
|
||||||
|
|
||||||
// Grid lines
|
const idx = row * cols + col;
|
||||||
ctx.lineWidth = 1;
|
return { idx, row, col, x, y };
|
||||||
ctx.strokeStyle = 'rgba(255,255,255,0.18)';
|
}
|
||||||
|
|
||||||
for (let c = 0; c <= cols; c++) {
|
function onClick(evt) {
|
||||||
const x = c * frameW * zoom + 0.5;
|
const hit = canvasToFrameIndex(evt);
|
||||||
ctx.beginPath();
|
if (!hit) return;
|
||||||
ctx.moveTo(x, 0);
|
|
||||||
ctx.lineTo(x, rows * frameH * zoom);
|
selFrameIdxEl && (selFrameIdxEl.textContent = hit.idx);
|
||||||
ctx.stroke();
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
for (let r = 0; r <= rows; r++) {
|
zoomEl?.addEventListener('input', draw);
|
||||||
const y = r * frameH * zoom + 0.5;
|
canvas?.addEventListener('click', onClick);
|
||||||
ctx.beginPath();
|
}
|
||||||
ctx.moveTo(0, y);
|
|
||||||
ctx.lineTo(cols * frameW * zoom, y);
|
|
||||||
ctx.stroke();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Frame numbers
|
if (!img || !canvas || !ctx) return;
|
||||||
// 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++) {
|
img.addEventListener('load', draw);
|
||||||
for (let c = 0; c < cols; c++) {
|
if (img.complete) draw();
|
||||||
const idx = r * cols + c;
|
hookRedraw();
|
||||||
|
})();
|
||||||
const px = c * frameW * zoom;
|
</script>
|
||||||
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>
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user