-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.html
155 lines (136 loc) · 4.73 KB
/
editor.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Canvas Grid Image App</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
#canvas-container {
position: relative;
display: inline-block;
margin-top: 10px;
}
canvas {
display: block;
border: 1px solid #ccc;
}
#output {
margin-top: 20px;
}
input {
width: 100%;
padding: 10px;
font-size: 14px;
box-sizing: border-box;
}
</style>
</head>
<body>
<h1>Image Grid Selector</h1>
<p>Drop an image below:</p>
<input type="file" id="imageInput" accept="image/*">
<div id="canvas-container">
<canvas id="gridCanvas"></canvas>
</div>
<div id="output">
<label for="selectedTiles">Selected Tile IDs:</label>
<input type="text" id="selectedTiles" readonly>
</div>
<script>
const canvas = document.getElementById('gridCanvas');
const ctx = canvas.getContext('2d');
const imageInput = document.getElementById('imageInput');
const selectedTilesInput = document.getElementById('selectedTiles');
const TILE_SIZE = 48;
const selectedTiles = new Set();
let currentImage = null
// Handle image file input
imageInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.onload = () => {
canvas.width = Math.ceil(img.width / TILE_SIZE) * TILE_SIZE;
canvas.height = Math.ceil(img.height / TILE_SIZE) * TILE_SIZE;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
drawGrid();
currentImage = img
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
});
// Draw grid lines on the canvas
function drawGrid() {
ctx.strokeStyle = 'rgba(0, 0, 0, 0.2)';
for (let x = 0; x < canvas.width; x += TILE_SIZE) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, canvas.height);
ctx.stroke();
}
for (let y = 0; y < canvas.height; y += TILE_SIZE) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(canvas.width, y);
ctx.stroke();
}
}
// Get tile ID based on click coordinates
function getTileId(x, y) {
const col = Math.floor(x / TILE_SIZE);
const row = Math.floor(y / TILE_SIZE);
const tilesPerRow = Math.floor(canvas.width / TILE_SIZE);
return row * tilesPerRow + col;
}
canvas.addEventListener('click', (event) => {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
const tileId = getTileId(x, y);
// Toggle the tile's selection
if (selectedTiles.has(tileId)) {
selectedTiles.delete(tileId); // Deselect
} else {
selectedTiles.add(tileId); // Select
}
// Redraw everything
redrawCanvas();
selectedTilesInput.value = Array.from(selectedTiles).sort((a, b) => a - b).join(',');
});
// Function to redraw everything
function redrawCanvas() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Redraw the image
if (currentImage) {
ctx.drawImage(currentImage, 0, 0);
}
// Redraw the grid
drawGrid();
// Highlight selected tiles
selectedTiles.forEach((tileId) => highlightTile(tileId));
}
function highlightTile(tileId) {
const tilesPerRow = Math.floor(canvas.width / TILE_SIZE);
const row = Math.floor(tileId / tilesPerRow);
const col = tileId % tilesPerRow;
ctx.fillStyle = 'rgba(255, 255, 0, 0.5)';
ctx.fillRect(
col * TILE_SIZE,
row * TILE_SIZE,
TILE_SIZE,
TILE_SIZE
);
}
</script>
</body>
</html>