// fill entire canvas with a chosen color (preserve grid) function fillAllWithColor(color) for(let i = 0; i < currentGridSize; i++) for(let j = 0; j < currentGridSize; j++) pixelMatrix[i][j] = color; drawFullMatrix();
// additional: fill with selected color as bucket from button? add a new button? but we already have fill BG (background only) // Also we can add a "flood fill active color" button. const floodFillActiveBtn = document.createElement('button'); // we inject it elegantly into tools panel, but i'll just add near export for extra tool, but we can include without overclutter: let's modify tools panel // get tools panel and insert new button const toolsDiv = document.querySelector('.tools-panel'); const fillActiveBtn = document.createElement('button'); fillActiveBtn.innerText = '🪣 FLOOD FILL (active)'; fillActiveBtn.className = 'btn'; fillActiveBtn.style.background = '#5a4a2e'; fillActiveBtn.addEventListener('click', () => // prompt for which color? we need a target pixel? simplest: fill whole canvas? no, flood fill requires seed. // We'll make it interactive: click on canvas after pressing flood mode? easier: show message. alert('🔮 Double-click on any pixel to flood fill with current color! (or use the FILL BG button for full background)'); ); toolsDiv.appendChild(fillActiveBtn); // but also update fill BG: keep original fill background using default bg. // override fillBgBtn to fill canvas with DEFAULT_BG fillBgBtn.onclick = () => fillAllWithColor(DEFAULT_BG); ; // clear canvas clearBtn.onclick = () => clearCanvas(); ; // export PNG (scaled up to show pixels) function exportAsPNG() // we can export current canvas exactly as is, but we might also scale for better preview? But it's fine. const link = document.createElement('a'); link.download = `melon_pixelart_$currentGridSizex$currentGridSize.png`; link.href = canvas.toDataURL('image/png'); link.click(); // export sprite data (JSON matrix colors, also ready for melon playground community) function exportJSON() const exportObj = meta: tool: "Melon Playground Pixel Art Maker", gridSize: currentGridSize, paletteHint: "each cell holds hex color" , pixels: pixelMatrix.map(row => [...row]) ; const jsonStr = JSON.stringify(exportObj, null, 2); // copy to clipboard navigator.clipboard.writeText(jsonStr).then(() => alert(`✅ Sprite data ($currentGridSizex$currentGridSize) copied as JSON!\nYou can share or import later.`); ).catch(() => alert("⚠️ Could not copy, but you can use PNG export instead."); ); // color picker preview sync function updateColorPreview() const newColor = colorPicker.value; colorPreview.style.background = newColor; colorPicker.addEventListener('input', updateColorPreview); updateColorPreview(); // grid change event gridSizeSelect.addEventListener('change', changeGridSize); // ---- Mouse / touch event binding ---- canvas.addEventListener('mousedown', handlePointerStart); window.addEventListener('mousemove', handlePointerMove); window.addEventListener('mouseup', handlePointerEnd); // touch events canvas.addEventListener('touchstart', handlePointerStart, passive: false); canvas.addEventListener('touchmove', handlePointerMove, passive: false); canvas.addEventListener('touchend', handlePointerEnd); canvas.addEventListener('contextmenu', disableContextMenu); canvas.addEventListener('dblclick', handleCanvasDoubleClick); // additional keyboard: hold E to erase? optional, but we can set alt/ctrl handled already. // final initialisation function init() currentGridSize = 32; gridSizeSelect.value = "32"; pixelMatrix = initMatrix(currentGridSize, DEFAULT_BG); resizeAndRedraw(); // export listeners exportPngBtn.addEventListener('click', exportAsPNG); exportJsonBtn.addEventListener('click', exportJSON); init(); )(); </script> </body> </html> pixel art maker for melon playground
// update single cell in matrix & canvas function setPixel(row, col, color) if(row < 0 // fill entire canvas with a chosen color