-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtileAtlasDemo.html
More file actions
178 lines (155 loc) · 6.46 KB
/
Copy pathtileAtlasDemo.html
File metadata and controls
178 lines (155 loc) · 6.46 KB
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
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Демонстрация вычисления координат тайлов в атласе</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f0f0f0;
}
.container {
max-width: 1200px;
margin: 0 auto;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
}
.input-section {
margin: 20px 0;
padding: 15px;
background-color: #f9f9f9;
border-radius: 5px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input {
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
width: 100px;
}
button {
padding: 10px 15px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
.result {
margin: 20px 0;
padding: 15px;
background-color: #e9f7ef;
border-left: 4px solid #28a745;
border-radius: 4px;
}
.atlas-info {
margin: 20px 0;
padding: 15px;
background-color: #e7f3ff;
border-left: 4px solid #007bff;
border-radius: 4px;
}
.examples {
margin: 20px 0;
padding: 15px;
background-color: #fff3cd;
border-left: 4px solid #ffc107;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
<h1>Демонстрация вычисления координат тайлов в атласе</h1>
<div class="atlas-info">
<h3>Информация о спрайт-атласе:</h3>
<p>- Размер тайла: 32x32 пикселя</p>
<p>- Количество столбцов: 64</p>
<p>- Общее количество тайлов: 6080</p>
<p>- Размер изображения: 2048x3040 пикселей</p>
</div>
<div class="input-section">
<label for="tileId">Введите ID тайла (0-6079):</label>
<input type="number" id="tileId" min="0" max="6079" value="0">
<button onclick="calculateCoordinates()">Вычислить координаты</button>
</div>
<div id="result" class="result" style="display:none;">
<h3>Результат:</h3>
<p>ID тайла: <span id="resultId"></span></p>
<p>X: <span id="resultX"></span> пикселей</p>
<p>Y: <span id="resultY"></span> пикселей</p>
<p>Ширина: <span id="resultW"></span> пикселей</p>
<p>Высота: <span id="resultH"></span> пикселей</p>
</div>
<div class="examples">
<h3>Примеры:</h3>
<ul>
<li>ID 0: x=0, y=0 (первый тайл в левом верхнем углу)</li>
<li>ID 1: x=32, y=0 (второй тайл в первой строке)</li>
<li>ID 63: x=2016, y=0 (последний тайл в первой строке)</li>
<li>ID 64: x=0, y=32 (первый тайл во второй строке)</li>
<li>ID 65: x=32, y=32 (второй тайл во второй строке)</li>
<li>ID 127: x=2016, y=32 (последний тайл во второй строке)</li>
</ul>
</div>
</div>
<script>
// Функция для вычисления координат в спрайт-атласе по ID тайла
function getAtlasCoordinates(tileId, tileSize = 32, columns = 64) {
// Проверяем, что ID тайла не превышает допустимое значение
const maxTiles = (2048 / tileSize) * (3040 / tileSize); // Максимальное количество тайлов в атласе
if (tileId < 0 || tileId >= maxTiles) {
throw new Error(`Tile ID ${tileId} выходит за пределы допустимого диапазона (0-${maxTiles-1})`);
}
// Вычисляем координаты в атласе
const x = (tileId % columns) * tileSize;
const y = Math.floor(tileId / columns) * tileSize;
return {
x: x,
y: y,
w: tileSize,
h: tileSize
};
}
function calculateCoordinates() {
const tileIdInput = document.getElementById('tileId');
const tileId = parseInt(tileIdInput.value);
if (isNaN(tileId) || tileId < 0 || tileId > 6079) {
alert('Пожалуйста, введите корректный ID тайла (от 0 до 6079)');
return;
}
try {
const coords = getAtlasCoordinates(tileId);
document.getElementById('resultId').textContent = tileId;
document.getElementById('resultX').textContent = coords.x;
document.getElementById('resultY').textContent = coords.y;
document.getElementById('resultW').textContent = coords.w;
document.getElementById('resultH').textContent = coords.h;
document.getElementById('result').style.display = 'block';
} catch (error) {
alert(error.message);
}
}
// Вычисляем координаты для тайла с ID 0 при загрузке страницы
window.onload = function() {
calculateCoordinates();
};
</script>
</body>
</html>