HTMLify
image to lua
Views: 507 | Author: amar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Pixel → Lua Generator</title> <style> input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; margin-top: 0.5rem; } input[type=range]:focus { outline: none; } body { margin: 0; padding: 0; display: flex; flex-direction: column; min-height: 100vh; background: #1e1e1e; color: #cfcfcf; font-family: Arial, sans-serif; } header, footer { text-align: center; padding: 1rem; background: #2d2d2d; } main { flex: 1; padding: 1rem; max-width: 700px; margin: 0 auto; } label { display: block; margin-top: 1rem; } input, button, textarea { background: #2d2d2d; border: 1px solid #3c3c3c; color: #cfcfcf; font-size: 1rem; box-sizing: border-box; } input, textarea { width: 100%; padding: 0.5rem; margin-top: 0.5rem; } button { padding: 0.5rem; margin-top: 0.5rem; cursor: pointer; } .url-loader { display: flex; gap: 0.5rem; margin-top: 0.5rem; } .url-loader input[type=text] { flex: 1; } .url-loader button { flex: 0 0 80px; } #original { display: block; margin: 1rem auto; max-width: 100%; height: auto; border: 1px solid #3c3c3c; } #resolution, #outputResolution { text-align: center; margin-top: 0.25rem; color: #aaa; } .slider-container { margin-top: 1rem; display: none; } .slider-container span { font-weight: bold; } .canvas-container { position: relative; display: inline-block; margin: 1rem auto; border: 1px solid #3c3c3c; } #preview { display: block; max-width: 100%; height: auto; } #luaNameContainer { display: none; margin-top: 1rem; } #luaNameContainer .url-loader button { flex: 0 0 60px; } #colorPicker { width: 100%; height: 2.5rem; border: none; margin-top: 1rem; cursor: pointer; } #luaContainer { margin-top: 1rem; display: none; } textarea { height: 200px; font-family: monospace; font-size: 0.9rem; resize: vertical; line-height: 1.2; } #downloadBtn { width: 100%; } footer { font-size: 0.9rem; } </style> </head> <body> <header><h1>Pixel → Lua Generator</h1></header> <main> <!-- File upload --> <label for="fileInput">Choose pixel image:</label> <input type="file" id="fileInput" accept="image/*" /> <!-- URL loader --> <label for="imageUrl">Or paste image URL:</label> <div class="url-loader"> <input type="text" id="imageUrl" placeholder="https://example.com/image.jpg" /> <button id="loadFromUrl">Load</button> </div> <!-- Lua name input (URL mode) --> <div id="luaNameContainer"> <label for="luaName">Lua Filename (without extension):</label> <div class="url-loader"> <input type="text" id="luaName" placeholder="custom_name" /> <button id="setNameBtn">Set</button> </div> </div> <!-- Original preview --> <img id="original" src="" alt="Original Upload" style="display:none;" /> <div id="resolution"></div> <!-- Pixel size slider --> <div class="slider-container" id="sliderContainer"> <label for="pixelRange">Pixel Size: <span id="pixelSizeLabel">1</span></label> <input type="range" id="pixelRange" min="1" max="50" value="1" step="1" /> </div> <!-- Canvas preview --> <div class="canvas-container" id="canvasContainer" style="display:none;"> <canvas id="preview" width="0" height="0"></canvas> </div> <div id="outputResolution"></div> <!-- Color picker --> <label for="colorPicker">Pick Ignore Color:</label> <input type="color" id="colorPicker" value="#000000" /> <!-- Lua output --> <div id="luaContainer"> <label for="luaCode">Generated Lua</label> <textarea id="luaCode" readonly></textarea> </div> <button id="downloadBtn" type="button" disabled>Export as .lua</button> </main> <footer>Made by Amar</footer> <script> const fileInput = document.getElementById('fileInput'); const imageUrl = document.getElementById('imageUrl'); const loadFromUrl = document.getElementById('loadFromUrl'); const originalImg = document.getElementById('original'); const resolutionDiv = document.getElementById('resolution'); const outputResDiv = document.getElementById('outputResolution'); const pixelRange = document.getElementById('pixelRange'); const pixelSizeLabel = document.getElementById('pixelSizeLabel'); const sliderContainer = document.getElementById('sliderContainer'); const canvasContainer = document.getElementById('canvasContainer'); const preview = document.getElementById('preview'); const ctx = preview.getContext('2d'); const luaNameContainer = document.getElementById('luaNameContainer'); const luaNameInput = document.getElementById('luaName'); const setNameBtn = document.getElementById('setNameBtn'); const colorPicker = document.getElementById('colorPicker'); const luaContainer = document.getElementById('luaContainer'); const luaCode = document.getElementById('luaCode'); const downloadBtn = document.getElementById('downloadBtn'); let img = new Image(); let imgName = 'image'; let origW = 0, origH = 0; let loaded = false; let ignoreHex = '000000'; let sourceUrl = ''; function onImageLoaded() { origW = img.width; origH = img.height; loaded = true; sliderContainer.style.display = 'block'; canvasContainer.style.display = 'inline-block'; luaContainer.style.display = 'block'; downloadBtn.disabled = false; originalImg.src = img.src; originalImg.style.display = 'block'; resolutionDiv.textContent = `${origW}×${origH}`; luaNameContainer.style.display = sourceUrl ? 'block' : 'none'; drawPixelated(); updateLuaPreview(); } fileInput.addEventListener('change', e => { const file = e.target.files[0]; if (!file) return; imgName = file.name.replace(/\.[^.]+$/, ''); sourceUrl = ''; img = new Image(); img.onload = onImageLoaded; img.src = URL.createObjectURL(file); }); loadFromUrl.addEventListener('click', () => { const url = imageUrl.value.trim(); if (!url) return alert('Enter valid URL'); imgName = url.split('/').pop().split('.')[0] || 'image'; sourceUrl = url; img = new Image(); img.crossOrigin = 'anonymous'; img.onload = onImageLoaded; img.onerror = () => alert('Load failed.'); img.src = url; }); pixelRange.addEventListener('input', () => { pixelSizeLabel.textContent = pixelRange.value; if (!loaded) return; drawPixelated(); updateLuaPreview(); }); setNameBtn.addEventListener('click', () => { if (luaNameInput.value.trim()) imgName = luaNameInput.value.trim(); updateLuaPreview(); }); colorPicker.addEventListener('input', () => { ignoreHex = colorPicker.value.slice(1); if (loaded) updateLuaPreview(); }); function drawPixelated() { const size = +pixelRange.value; const tmpW = Math.ceil(origW / size); const tmpH = Math.ceil(origH / size); const tmp = document.createElement('canvas'); tmp.width = tmpW; tmp.height = tmpH; const tctx = tmp.getContext('2d'); tctx.imageSmoothingEnabled = false; tctx.drawImage(img, 0, 0, tmpW, tmpH); preview.width = origW; preview.height = origH; ctx.imageSmoothingEnabled = false; ctx.clearRect(0, 0, origW, origH); ctx.drawImage(tmp, 0, 0, tmpW, tmpH, 0, 0, origW, origH); // update output resolution outputResDiv.textContent = `${tmpW}×${tmpH}`; } function rgbToHex(r, g, b) { return ((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1); } function buildLuaString() { const size = +pixelRange.value; const tmpW = Math.ceil(origW / size); const tmpH = Math.ceil(origH / size); const tmp = document.createElement('canvas'); tmp.width = tmpW; tmp.height = tmpH; const tctx = tmp.getContext('2d'); tctx.imageSmoothingEnabled = false; tctx.drawImage(img, 0, 0, tmpW, tmpH); const data = tctx.getImageData(0, 0, tmpW, tmpH).data; const rows = []; for (let y = 0; y < tmpH; y++) { const cols = []; for (let x = 0; x < tmpW; x++) { const i = (y * tmpW + x) * 4; const alpha = data[i + 3]; const hex = alpha === 0 ? 'ffffff' : rgbToHex(data[i], data[i + 1], data[i + 2]); cols.push(`"${hex}"`); } rows.push(' { ' + cols.join(', ') + ' },'); } return [ '--[[', `Title: ${imgName}`, `Platform: ${sourceUrl ? new URL(sourceUrl).hostname : 'local'}`, `url: ${sourceUrl}`, ']]--', 'return {', ` title = "${imgName}",`, ` name = "${imgName}",`, ` width = ${tmpW},`, ` height= ${tmpH},`, ` ignore_colors = [ "${ignoreHex}" ],`, ' frames = {', ' {', ...rows, ' }', ' }', '}' ].join('\n'); } function updateLuaPreview() { luaCode.value = buildLuaString(); } downloadBtn.addEventListener('click', () => { const blob = new Blob([luaCode.value], { type: 'text/plain' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `${imgName}.lua`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); }); </script> </body> </html> |