| | document.addEventListener('DOMContentLoaded', () => { |
| | |
| | const canvas = document.getElementById('unscramble-canvas'); |
| | const ctx = canvas.getContext('2d'); |
| | const loadingMessage = document.getElementById('loading-message'); |
| |
|
| | |
| | canvas.oncontextmenu = (e) => { |
| | e.preventDefault(); |
| | return false; |
| | }; |
| |
|
| | |
| |
|
| | async function unscramble() { |
| | try { |
| | |
| | const [mapResponse, scrambledImage] = await Promise.all([ |
| | fetch('assets/map.json'), |
| | loadImage('assets/scrambled.png') |
| | ]); |
| |
|
| | if (!mapResponse.ok) { |
| | throw new Error(`Failed to load map.json: ${mapResponse.statusText}`); |
| | } |
| |
|
| | const mapData = await mapResponse.json(); |
| | const { gridSize, scrambleMap, width, height } = mapData; |
| |
|
| | |
| | loadingMessage.style.display = 'none'; |
| | canvas.width = width; |
| | canvas.height = height; |
| |
|
| | |
| | const tileW = canvas.width / gridSize; |
| | const tileH = canvas.height / gridSize; |
| |
|
| | |
| | |
| | |
| | const inverseMap = new Array(scrambleMap.length); |
| | for (let newIndex = 0; newIndex < scrambleMap.length; newIndex++) { |
| | const originalIndex = scrambleMap[newIndex]; |
| | inverseMap[originalIndex] = newIndex; |
| | } |
| |
|
| | |
| | for (let originalIndex = 0; originalIndex < scrambleMap.length; originalIndex++) { |
| | |
| | const scrambledIndex = inverseMap[originalIndex]; |
| |
|
| | |
| | const sourceX = (scrambledIndex % gridSize) * tileW; |
| | const sourceY = Math.floor(scrambledIndex / gridSize) * tileH; |
| |
|
| | const destX = (originalIndex % gridSize) * tileW; |
| | const destY = Math.floor(originalIndex / gridSize) * tileH; |
| |
|
| | |
| | ctx.drawImage( |
| | scrambledImage, |
| | sourceX, sourceY, |
| | tileW, tileH, |
| | destX, destY, |
| | tileW, tileH |
| | ); |
| | } |
| |
|
| | } catch (error) { |
| | console.error("Unscrambling failed:", error); |
| | loadingMessage.textContent = `Error: ${error.message}`; |
| | loadingMessage.style.color = 'red'; |
| | } |
| | } |
| |
|
| | |
| | function loadImage(src) { |
| | return new Promise((resolve, reject) => { |
| | const img = new Image(); |
| | img.onload = () => resolve(img); |
| | img.onerror = (err) => reject(new Error(`Failed to load image: ${src}`)); |
| | img.src = src; |
| | }); |
| | } |
| |
|
| | |
| | unscramble(); |
| | }); |