broadfield-dev commited on
Commit
bba8bcb
·
verified ·
1 Parent(s): b646aad

Rename index.html to script.js

Browse files
Files changed (2) hide show
  1. index.html +0 -19
  2. script.js +89 -0
index.html DELETED
@@ -1,19 +0,0 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
script.js ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.addEventListener('DOMContentLoaded', () => {
2
+ // DOM elements
3
+ const canvas = document.getElementById('unscramble-canvas');
4
+ const ctx = canvas.getContext('2d');
5
+ const loadingMessage = document.getElementById('loading-message');
6
+
7
+ // Prevent right-click on the canvas
8
+ canvas.oncontextmenu = (e) => {
9
+ e.preventDefault();
10
+ return false;
11
+ };
12
+
13
+ // --- Main Unscrambling Logic ---
14
+
15
+ async function unscramble() {
16
+ try {
17
+ // 1. Fetch the map data and the scrambled image simultaneously
18
+ const [mapResponse, scrambledImage] = await Promise.all([
19
+ fetch('assets/map.json'),
20
+ loadImage('assets/scrambled.png')
21
+ ]);
22
+
23
+ if (!mapResponse.ok) {
24
+ throw new Error(`Failed to load map.json: ${mapResponse.statusText}`);
25
+ }
26
+
27
+ const mapData = await mapResponse.json();
28
+ const { gridSize, scrambleMap, width, height } = mapData;
29
+
30
+ // Hide the loading message and set canvas dimensions
31
+ loadingMessage.style.display = 'none';
32
+ canvas.width = width;
33
+ canvas.height = height;
34
+
35
+ // 2. Calculate the tile dimensions
36
+ const tileW = canvas.width / gridSize;
37
+ const tileH = canvas.height / gridSize;
38
+
39
+ // 3. Create an "inverse map" for easy unscrambling.
40
+ // The scrambleMap tells us: new_position -> original_position
41
+ // We need to know where the tile for an original_position is located now.
42
+ const inverseMap = new Array(scrambleMap.length);
43
+ for (let newIndex = 0; newIndex < scrambleMap.length; newIndex++) {
44
+ const originalIndex = scrambleMap[newIndex];
45
+ inverseMap[originalIndex] = newIndex;
46
+ }
47
+
48
+ // 4. Loop through each TILE of the FINAL image and draw it on the canvas
49
+ for (let originalIndex = 0; originalIndex < scrambleMap.length; originalIndex++) {
50
+ // Find where this tile is located in the SCRAMBLED image
51
+ const scrambledIndex = inverseMap[originalIndex];
52
+
53
+ // Calculate source (from scrambled.png) and destination (on canvas) coordinates
54
+ const sourceX = (scrambledIndex % gridSize) * tileW;
55
+ const sourceY = Math.floor(scrambledIndex / gridSize) * tileH;
56
+
57
+ const destX = (originalIndex % gridSize) * tileW;
58
+ const destY = Math.floor(originalIndex / gridSize) * tileH;
59
+
60
+ // Copy the rectangular tile from the hidden scrambled image to the visible canvas
61
+ ctx.drawImage(
62
+ scrambledImage, // The source image
63
+ sourceX, sourceY, // Top-left corner of the source tile
64
+ tileW, tileH, // Dimensions of the source tile
65
+ destX, destY, // Top-left corner of the destination on the canvas
66
+ tileW, tileH // Dimensions of the destination tile
67
+ );
68
+ }
69
+
70
+ } catch (error) {
71
+ console.error("Unscrambling failed:", error);
72
+ loadingMessage.textContent = `Error: ${error.message}`;
73
+ loadingMessage.style.color = 'red';
74
+ }
75
+ }
76
+
77
+ // Helper function to load an image and return a promise
78
+ function loadImage(src) {
79
+ return new Promise((resolve, reject) => {
80
+ const img = new Image();
81
+ img.onload = () => resolve(img);
82
+ img.onerror = (err) => reject(new Error(`Failed to load image: ${src}`));
83
+ img.src = src;
84
+ });
85
+ }
86
+
87
+ // Run the main function
88
+ unscramble();
89
+ });