diff options
author | elioat <elioat@tilde.institute> | 2024-12-24 22:24:09 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2024-12-24 22:24:09 -0500 |
commit | 9420a45728b4c2af3b4bc2a33f2f6981003dcc1c (patch) | |
tree | 5bed7ee2b0f84f54e94b7c301ed6594985a03daa /js/pixel-art/pixel | |
parent | 53efd3f023dede65c778deeacd0b2e6155632577 (diff) | |
download | tour-9420a45728b4c2af3b4bc2a33f2f6981003dcc1c.tar.gz |
*
Diffstat (limited to 'js/pixel-art/pixel')
-rw-r--r-- | js/pixel-art/pixel/app.js | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/js/pixel-art/pixel/app.js b/js/pixel-art/pixel/app.js index 382fc90..2075e87 100644 --- a/js/pixel-art/pixel/app.js +++ b/js/pixel-art/pixel/app.js @@ -24,7 +24,6 @@ let currentCanvasIndex = 0; let globalOffsetX = 0; let globalOffsetY = 0; - canvas.addEventListener('mousedown', handleInputStart); canvas.addEventListener('mousemove', handleInputMove); canvas.addEventListener('mouseup', handleInputEnd); @@ -66,7 +65,9 @@ function addNewCanvas() { } function initializeGrid() { - grid = Array(gridWidth).fill().map(() => Array(gridHeight).fill(null)); + if (canvases.length > 0) { + canvases[currentCanvasIndex].grid = Array(gridWidth).fill().map(() => Array(gridHeight).fill(null)); + } } function resizeCanvas() { @@ -193,9 +194,32 @@ function handlePan(e) { } function updateGridSize() { - gridWidth = parseInt(document.getElementById('gridWidth').value); - gridHeight = parseInt(document.getElementById('gridHeight').value); - initializeGrid(); + const newWidth = parseInt(document.getElementById('gridWidth').value); + const newHeight = parseInt(document.getElementById('gridHeight').value); + + // Validate input + if (newWidth <= 0 || newHeight <= 0) return; + + gridWidth = newWidth; + gridHeight = newHeight; + + // Update all existing canvases with new dimensions + canvases.forEach(canvasData => { + const newGrid = Array(gridWidth).fill().map(() => Array(gridHeight).fill(null)); + + // Preserve existing pixel data where possible + const minWidth = Math.min(canvasData.grid.length, gridWidth); + const minHeight = Math.min(canvasData.grid[0].length, gridHeight); + + for (let x = 0; x < minWidth; x++) { + for (let y = 0; y < minHeight; y++) { + newGrid[x][y] = canvasData.grid[x][y]; + } + } + + canvasData.grid = newGrid; + }); + centerGrid(); drawGrid(); saveToLocalStorage(); |