-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
161 lines (136 loc) · 4.21 KB
/
script.js
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
const map = L.map("map").setView([-6.9175, 107.6191], 8); // Center on Bandung
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxZoom: 19,
}).addTo(map);
document.getElementById("searchButton").addEventListener("click", function () {
alert("Notes: To find more place, zoom out the maps");
const name = document.getElementById("nameInput").value;
if (name) {
searchPlaces(name);
} else {
alert("Please enter a name.");
}
});
function searchPlaces(name) {
const query = `https://nominatim.openstreetmap.org/search?q=${encodeURIComponent(
name
)}&format=json&addressdetails=1`;
fetch(query)
.then((response) => response.json())
.then((data) => {
clearResults();
if (data.length > 0) {
data.forEach((place) => {
addMarker(place);
displayResult(place);
});
map.setView([data[0].lat, data[0].lon], 10); // Center map on the first result
} else {
alert("No places found.");
}
})
.catch((error) => {
console.error("Error fetching data: ", error);
alert("An error occurred while searching.");
});
}
function addMarker(place) {
const marker = L.marker([place.lat, place.lon]).addTo(map);
marker.bindPopup(place.display_name).openPopup();
}
function displayResult(place) {
const resultsDiv = document.getElementById("results");
const resultItem = document.createElement("div");
resultItem.innerText = place.display_name;
resultsDiv.appendChild(resultItem);
}
function clearResults() {
document.getElementById("results").innerHTML = "";
}
// Confetti
let W = window.innerWidth;
let H = window.innerHeight;
const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
const maxConfettis = 150;
const particles = [];
const possibleColors = [
"DodgerBlue",
"OliveDrab",
"Gold",
"Pink",
"SlateBlue",
"LightBlue",
"Gold",
"Violet",
"PaleGreen",
"SteelBlue",
"SandyBrown",
"Chocolate",
"Crimson",
];
function randomFromTo(from, to) {
return Math.floor(Math.random() * (to - from + 1) + from);
}
function confettiParticle() {
this.x = Math.random() * W;
this.y = Math.random() * H - H;
this.r = randomFromTo(11, 33);
this.d = Math.random() * maxConfettis + 11;
this.color =
possibleColors[Math.floor(Math.random() * possibleColors.length)];
this.tilt = Math.floor(Math.random() * 33) - 11;
this.tiltAngleIncremental = Math.random() * 0.07 + 0.05;
this.tiltAngle = 0;
this.draw = function () {
context.beginPath();
context.lineWidth = this.r / 2;
context.strokeStyle = this.color;
context.moveTo(this.x + this.tilt + this.r / 3, this.y);
context.lineTo(this.x + this.tilt, this.y + this.tilt + this.r / 5);
return context.stroke();
};
}
function Draw() {
const results = [];
requestAnimationFrame(Draw);
context.clearRect(0, 0, W, window.innerHeight);
for (var i = 0; i < maxConfettis; i++) {
results.push(particles[i].draw());
}
let particle = {};
let remainingFlakes = 0;
for (var i = 0; i < maxConfettis; i++) {
particle = particles[i];
particle.tiltAngle += particle.tiltAngleIncremental;
particle.y += (Math.cos(particle.d) + 3 + particle.r / 2) / 2;
particle.tilt = Math.sin(particle.tiltAngle - i / 3) * 15;
if (particle.y <= H) remainingFlakes++;
if (particle.x > W + 30 || particle.x < -30 || particle.y > H) {
particle.x = Math.random() * W;
particle.y = -30;
particle.tilt = Math.floor(Math.random() * 10) - 20;
}
}
return results;
}
window.addEventListener(
"resize",
function () {
W = window.innerWidth;
H = window.innerHeight;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
},
false
);
for (var i = 0; i < maxConfettis; i++) {
particles.push(new confettiParticle());
}
canvas.width = W;
canvas.height = H;
Draw();
alert("Welcome to : Find Me on the Map! by @shabirmp");
alert("Find your name on the map and share it with your friends!");
alert("Dont forget to support this project at Github : @shabirmp");
alert("Good Luck !");