-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
268 lines (217 loc) · 8.04 KB
/
script.js
File metadata and controls
268 lines (217 loc) · 8.04 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
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
let map;
let userMarker;
let userLocation = null;
let puzzleImage = null;
const PUZZLE_SIZE = 4; // 4x4 = 16 puzzli
function initMap() {
map = L.map('map').setView([52.2297, 21.0122], 13); // Domyślnie Warszawa
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18
}).addTo(map);
}
// Prośba o zgodę na geolokalizację
function requestGeolocation() {
if (!("geolocation" in navigator)) {
alert("Geolokalizacja nie jest wspierana przez tę przeglądarkę");
return;
}
navigator.geolocation.getCurrentPosition(
(position) => {
userLocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
console.log("Geolokalizacja pobrana:", userLocation);
},
(error) => {
console.error("Błąd geolokalizacji:", error);
alert("Nie udało się pobrać lokalizacji");
}
);
}
function requestNotificationPermission() {
if ("Notification" in window) {
Notification.requestPermission().then(permission => {
console.log("Uprawnienia do powiadomień:", permission);
});
}
}
document.getElementById('myLocationBtn').addEventListener('click', () => {
if (!userLocation) {
alert("Najpierw udostępnij swoją lokalizację");
requestGeolocation();
return;
}
map.setView([userLocation.lat, userLocation.lng], 15);
if (userMarker) {
map.removeLayer(userMarker);
}
// Dodanie markera
userMarker = L.marker([userLocation.lat, userLocation.lng]).addTo(map);
userMarker.bindPopup("Twoja lokalizacja").openPopup();
document.getElementById('coordinates').textContent =
`Współrzędne: ${userLocation.lat.toFixed(6)}, ${userLocation.lng.toFixed(6)}`;
});
// Funkcja do pobrania mapy jako obrazu
document.getElementById('downloadMapBtn').addEventListener('click', () => {
captureMap();
});
// Przechwycenie mapy do canvas używając leaflet-image
function captureMap() {
leafletImage(map, function(err, canvas) {
if (err) {
console.error("Błąd podczas przechwytywania mapy:", err);
alert("Nie udało się pobrać mapy. Spróbuj ponownie.");
return;
}
// Przeskalowanie do 400x400
const finalCanvas = document.getElementById('mapCanvas');
finalCanvas.width = 400;
finalCanvas.height = 400;
const ctx = finalCanvas.getContext('2d');
ctx.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, 400, 400);
puzzleImage = finalCanvas.toDataURL();
console.log("Mapa przechwycona do canvas");
createPuzzle();
});
}
function createPuzzle() {
const piecesContainer = document.getElementById('puzzlePieces');
const boardContainer = document.getElementById('puzzleBoard');
piecesContainer.innerHTML = '';
boardContainer.innerHTML = '';
const pieceWidth = 400 / PUZZLE_SIZE;
const pieceHeight = 400 / PUZZLE_SIZE;
for (let i = 0; i < PUZZLE_SIZE * PUZZLE_SIZE; i++) {
const slot = document.createElement('div');
slot.className = 'board-slot';
slot.dataset.position = i;
slot.addEventListener('dragover', handleDragOver);
slot.addEventListener('drop', handleDrop);
boardContainer.appendChild(slot);
}
const indices = Array.from({length: PUZZLE_SIZE * PUZZLE_SIZE}, (_, i) => i);
shuffleArray(indices);
indices.forEach(index => {
const piece = document.createElement('div');
piece.className = 'puzzle-piece';
piece.draggable = true;
piece.dataset.correctPosition = index;
const row = Math.floor(index / PUZZLE_SIZE);
const col = index % PUZZLE_SIZE;
piece.style.width = pieceWidth + 'px';
piece.style.height = pieceHeight + 'px';
piece.style.backgroundImage = `url(${puzzleImage})`;
piece.style.backgroundPosition = `-${col * pieceWidth}px -${row * pieceHeight}px`;
piece.addEventListener('dragstart', handleDragStart);
piece.addEventListener('dragend', handleDragEnd);
piecesContainer.appendChild(piece);
});
console.log("Puzzle utworzone i wymieszane");
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
let draggedElement = null;
function handleDragStart(e) {
draggedElement = e.target;
e.target.classList.add('dragging');
e.dataTransfer.effectAllowed = 'move';
}
function handleDragEnd(e) {
e.target.classList.remove('dragging');
}
function handleDragOver(e) {
if (e.preventDefault) {
e.preventDefault();
}
e.dataTransfer.dropEffect = 'move';
return false;
}
function handleDrop(e) {
if (e.stopPropagation) {
e.stopPropagation();
}
e.preventDefault();
const slot = e.target.closest('.board-slot');
if (!slot) return;
// Jeśli slot już ma puzzle, wyrzuć je z powrotem do kontenera
if (slot.children.length > 0) {
const existingPiece = slot.children[0];
document.getElementById('puzzlePieces').appendChild(existingPiece);
existingPiece.draggable = true;
}
slot.appendChild(draggedElement);
slot.classList.add('filled');
checkPiece(draggedElement, slot);
checkCompletion();
return false;
}
function checkPiece(piece, slot) {
const correctPosition = parseInt(piece.dataset.correctPosition);
const currentPosition = parseInt(slot.dataset.position);
if (correctPosition === currentPosition) {
slot.classList.add('correct');
piece.draggable = false;
console.log(`Puzzle ${correctPosition} umieszczone poprawnie!`);
} else {
slot.classList.remove('correct');
piece.draggable = true;
}
}
function checkCompletion() {
const slots = document.querySelectorAll('.board-slot');
let correctCount = 0;
slots.forEach(slot => {
if (slot.classList.contains('correct')) {
correctCount++;
}
});
console.log(`Poprawnie ułożonych puzzli: ${correctCount}/${PUZZLE_SIZE * PUZZLE_SIZE}`);
if (correctCount === PUZZLE_SIZE * PUZZLE_SIZE) {
console.log("Gratulacje! Wszystkie puzzle ułożone poprawnie!");
showNotification();
}
}
function showNotification() {
if (Notification.permission === "granted") {
console.log("Tworzenie powiadomienia...");
try {
const notification = new Notification("Puzzle ukończone! 🎉", {
body: "Gratulacje! Ułożyłeś wszystkie puzzle poprawnie!",
tag: "puzzle-complete",
requireInteraction: false
});
notification.onclick = function() {
window.focus();
notification.close();
};
console.log("Powiadomienie utworzone!");
} catch (err) {
console.error("Błąd tworzenia powiadomienia:", err);
alert("Gratulacje! Ułożyłeś wszystkie puzzle poprawnie!");
}
} else if (Notification.permission === "denied") {
console.log("Powiadomienia są zablokowane");
alert("Gratulacje! Ułożyłeś wszystkie puzzle poprawnie!\n\n(Powiadomienia są zablokowane - możesz je włączyć w ustawieniach przeglądarki)");
} else {
console.log("Proszenie o uprawnienia do powiadomień...");
Notification.requestPermission().then(permission => {
console.log("Nowe uprawnienie:", permission);
if (permission === "granted") {
showNotification();
} else {
alert("Gratulacje! Ułożyłeś wszystkie puzzle poprawnie!");
}
});
}
}
// Inicjalizacja
window.addEventListener('load', () => {
initMap();
requestGeolocation();
requestNotificationPermission();
});