-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsun.html
More file actions
443 lines (382 loc) · 16 KB
/
sun.html
File metadata and controls
443 lines (382 loc) · 16 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Daylight</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🌅</text></svg>">
<style>
body {
margin: 0;
padding: 20px;
min-height: 100vh;
transition: background 2s ease;
font-family: Arial, sans-serif;
color: white;
}
#sunTimes {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
font-family: sans-serif;
font-size: 48px;
font-weight: 300;
line-height: 1.5;
}
#sunTimes .time-label {
font-size: 24px;
opacity: 0.8;
font-weight: 400;
}
</style>
</head>
<body>
<button onclick="getLocation()">Fetch Data</button>
<div id="sunTimes">
<div><span class="time-label">Sunrise</span></div>
<div id="sunriseDisplay">--:--</div>
<div><span class="time-label">Sunset</span></div>
<div id="sunsetDisplay">--:--</div>
</div>
<p>Latitude: <span id="latitude"></span></p>
<p>Longitude: <span id="longitude"></span></p>
<p>Date: <span id="date"></span></p>
<p>Sunrise: <span id="sunrise"></span></p>
<p>Sunset: <span id="sunset"></span></p>
<p>Daylight change: <span id="daylightChange"></span></p>
<p>Notifications: <span id="notificationStatus"></span></p>
<p id="scheduledNotifications"></p>
</body>
<script>
const latitudeElement = document.getElementById('latitude');
const longitudeElement = document.getElementById('longitude');
const dateElement = document.getElementById('date');
const sunriseElement = document.getElementById('sunrise');
const sunsetElement = document.getElementById('sunset');
const sunriseDisplayElement = document.getElementById('sunriseDisplay');
const sunsetDisplayElement = document.getElementById('sunsetDisplay');
const daylightChangeElement = document.getElementById('daylightChange');
const notificationStatusElement = document.getElementById('notificationStatus');
const scheduledNotificationsElement = document.getElementById('scheduledNotifications');
let latitude = 0;
let longitude = 0;
/**
* Format ISO time string to 12-hour format
*/
function formatTo12Hour(isoString) {
const date = new Date(isoString);
let hours = date.getHours();
const minutes = date.getMinutes();
const ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // 0 should be 12
const minutesStr = minutes < 10 ? '0' + minutes : minutes;
return `${hours}:${minutesStr} ${ampm}`;
}
/**
* Request notification permission from the user.
*/
async function requestNotificationPermission() {
if (!("Notification" in window)) {
notificationStatusElement.innerHTML = "This browser does not support notifications";
return false;
}
if (Notification.permission === "granted") {
notificationStatusElement.innerHTML = "Notifications enabled ✓";
return true;
}
if (Notification.permission === "denied") {
notificationStatusElement.innerHTML = "Notifications blocked (check browser settings)";
return false;
}
// Permission is "default" - need to request
try {
const permission = await Notification.requestPermission();
if (permission === "granted") {
notificationStatusElement.innerHTML = "Notifications enabled ✓";
return true;
} else if (permission === "denied") {
notificationStatusElement.innerHTML = "Notifications blocked";
return false;
} else {
// User dismissed the prompt
notificationStatusElement.innerHTML = "Notification permission needed (click Fetch Data again)";
return false;
}
} catch (error) {
notificationStatusElement.innerHTML = "Could not request notification permission";
return false;
}
}
/**
* Schedule a notification at a specific time with an optional callback.
*/
function scheduleNotification(title, body, targetTime, onFire) {
const now = new Date();
const delay = targetTime - now;
if (delay > 0) {
setTimeout(() => {
new Notification(title, { body });
if (onFire) {
onFire();
}
}, delay);
return delay;
}
return null;
}
/**
* Format milliseconds into a human-readable time string.
*/
function formatTimeDifference(ms) {
const totalSeconds = Math.abs(Math.floor(ms / 1000));
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
if (minutes > 0 && seconds > 0) {
return `${minutes} minute${minutes !== 1 ? 's' : ''} and ${seconds} second${seconds !== 1 ? 's' : ''}`;
} else if (minutes > 0) {
return `${minutes} minute${minutes !== 1 ? 's' : ''}`;
} else {
return `${seconds} second${seconds !== 1 ? 's' : ''}`;
}
}
// Color palette for the sky gradient
const skyColors = [
'#1F214D', // Deep night
'#50366F', // Purple night
'#BF3475', // Pink dawn
'#EE6C45', // Orange sunrise
'#FFCE61', // Golden day
'#FFE58A' // Bright noon
];
/**
* Convert hex color to RGB object
*/
function hexToRgb(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
/**
* Interpolate between two RGB colors
*/
function interpolateColor(color1, color2, factor) {
const c1 = hexToRgb(color1);
const c2 = hexToRgb(color2);
const r = Math.round(c1.r + factor * (c2.r - c1.r));
const g = Math.round(c1.g + factor * (c2.g - c1.g));
const b = Math.round(c1.b + factor * (c2.b - c1.b));
return `rgb(${r}, ${g}, ${b})`;
}
/**
* Get color based on time of day relative to sunrise/sunset
*/
function getColorForTime(currentTime, sunriseTime, sunsetTime) {
const sunrise = new Date(sunriseTime);
const sunset = new Date(sunsetTime);
const now = currentTime;
// Calculate key times
const solarNoon = new Date((sunrise.getTime() + sunset.getTime()) / 2);
// Calculate previous sunset and next sunrise for night calculations
const prevSunset = new Date(sunset);
prevSunset.setDate(prevSunset.getDate() - 1);
const nextSunrise = new Date(sunrise);
nextSunrise.setDate(nextSunrise.getDate() + 1);
let progress;
if (now >= sunrise && now <= sunset) {
// Daytime: from sunrise to sunset
const dayLength = sunset - sunrise;
const timeSinceSunrise = now - sunrise;
progress = timeSinceSunrise / dayLength;
// Map progress (0 to 1) to color indices (0 to 5 and back to 0)
// 0 at sunrise, 5 at solar noon, 0 at sunset
let colorProgress;
if (progress <= 0.5) {
// Morning: sunrise to noon (colors 0 to 5)
colorProgress = progress * 2 * (skyColors.length - 1);
} else {
// Afternoon: noon to sunset (colors 5 to 0)
colorProgress = (2 - progress * 2) * (skyColors.length - 1);
}
return colorProgress;
} else {
// Nighttime: stay at darkest color
return 0;
}
}
/**
* Update the background gradient based on current time
*/
function updateBackgroundGradient(sunriseTime, sunsetTime) {
const now = new Date();
const colorProgress = getColorForTime(now, sunriseTime, sunsetTime);
// Get the two colors to interpolate between
const colorIndex = Math.floor(colorProgress);
const nextColorIndex = Math.min(colorIndex + 1, skyColors.length - 1);
const factor = colorProgress - colorIndex;
// Get base color
const baseColor = interpolateColor(skyColors[colorIndex], skyColors[nextColorIndex], factor);
// Create gradient effect: more blue at top, more yellow at bottom
// Shift top color toward earlier (bluer) and bottom toward later (yellower)
const topColorIndex = Math.max(0, colorIndex - 0.5);
const bottomColorIndex = Math.min(skyColors.length - 1, colorIndex + 1);
const topIndex = Math.floor(topColorIndex);
const topNextIndex = Math.min(topIndex + 1, skyColors.length - 1);
const topFactor = topColorIndex - topIndex;
const topColor = interpolateColor(skyColors[topIndex], skyColors[topNextIndex], topFactor);
const bottomIndex = Math.floor(bottomColorIndex);
const bottomNextIndex = Math.min(bottomIndex + 1, skyColors.length - 1);
const bottomFactor = bottomColorIndex - bottomIndex;
const bottomColor = interpolateColor(skyColors[bottomIndex], skyColors[bottomNextIndex], bottomFactor);
// Apply gradient
document.body.style.background = `linear-gradient(to bottom, ${topColor}, ${bottomColor})`;
}
/**
* Fetch sun data and schedule notifications for daylight changes.
*/
async function fetchAndScheduleNotifications() {
if (!latitude || !longitude) {
console.error('Location not set');
return;
}
// Get today's sunset times
let date = new Date();
const [sunriseToday, sunsetToday] = await fetchSunData(date);
sunriseElement.innerHTML = sunriseToday;
sunsetElement.innerHTML = sunsetToday;
sunriseDisplayElement.innerHTML = formatTo12Hour(sunriseToday);
sunsetDisplayElement.innerHTML = formatTo12Hour(sunsetToday);
const daylightToday = new Date(sunsetToday) - new Date(sunriseToday);
// Start updating the background gradient
updateBackgroundGradient(sunriseToday, sunsetToday);
// Fetch sunrise data from yesterday
date.setDate(date.getDate() - 1);
const [sunriseYesterday, sunsetYesterday] = await fetchSunData(date);
const daylightYesterday = new Date(sunsetYesterday) - new Date(sunriseYesterday);
// Calculate daylight change
const daylightChange = daylightToday - daylightYesterday;
daylightChangeElement.innerHTML = `${(daylightChange / (1000 * 60)).toFixed(2)} minutes`;
// Schedule notifications if enabled
if (Notification.permission === "granted") {
const sunriseTodayDate = new Date(sunriseToday);
const sunriseYesterdayDate = new Date(sunriseYesterday);
const sunsetTodayDate = new Date(sunsetToday);
const sunsetYesterdayDate = new Date(sunsetYesterday);
const sunriseDiff = sunriseTodayDate - sunriseYesterdayDate;
const sunsetDiff = sunsetTodayDate - sunsetYesterdayDate;
let scheduledMessages = [];
let latestNotificationTime = null;
// Schedule sunrise notification
if (sunriseDiff !== 0) {
const isGaining = sunriseDiff < 0; // Earlier sunrise = gaining daylight
const message = isGaining
? `Your extra ${formatTimeDifference(sunriseDiff)} of morning sunlight starts now, enjoy it!`
: `You just lost ${formatTimeDifference(sunriseDiff)} of morning sunlight.`;
const notificationTime = isGaining ? sunriseYesterdayDate : sunriseTodayDate;
const delay = scheduleNotification("Sunrise Change", message, notificationTime, () => {
// After notification fires, check if this is the last one
const now = new Date();
if (!latestNotificationTime || now >= latestNotificationTime) {
// Fetch new data for tomorrow's notifications
fetchAndScheduleNotifications();
}
});
if (delay) {
scheduledMessages.push(`Sunrise notification scheduled in ${formatTimeDifference(delay)}`);
if (!latestNotificationTime || notificationTime > latestNotificationTime) {
latestNotificationTime = notificationTime;
}
} else {
scheduledMessages.push(`Sunrise notification time has passed`);
}
}
// Schedule sunset notification
if (sunsetDiff !== 0) {
const isGaining = sunsetDiff > 0; // Later sunset = gaining daylight
const message = isGaining
? `Your extra ${formatTimeDifference(sunsetDiff)} of evening sunlight starts now, enjoy it!`
: `You just lost ${formatTimeDifference(sunsetDiff)} of evening sunlight.`;
const notificationTime = isGaining ? sunsetYesterdayDate : sunsetTodayDate;
const delay = scheduleNotification("Sunset Change", message, notificationTime, () => {
// After notification fires, check if this is the last one
const now = new Date();
if (!latestNotificationTime || now >= latestNotificationTime) {
// Fetch new data for tomorrow's notifications
fetchAndScheduleNotifications();
}
});
if (delay) {
scheduledMessages.push(`Sunset notification scheduled in ${formatTimeDifference(delay)}`);
if (!latestNotificationTime || notificationTime > latestNotificationTime) {
latestNotificationTime = notificationTime;
}
} else {
scheduledMessages.push(`Sunset notification time has passed`);
}
}
if (scheduledMessages.length > 0) {
scheduledNotificationsElement.innerHTML = scheduledMessages.join('<br>');
} else {
scheduledNotificationsElement.innerHTML = 'No daylight change today';
}
}
}
/**
* Get the user's current location using the Geolocation API.
*/
async function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(async (position) => {
// Success function
console.log(position);
latitude = position.coords.latitude;
longitude = position.coords.longitude;
latitudeElement.innerHTML = latitude;
longitudeElement.innerHTML = longitude;
// Request notification permission
await requestNotificationPermission();
// Fetch data and schedule notifications
await fetchAndScheduleNotifications();
// Update gradient every minute
setInterval(() => {
const now = new Date();
updateBackgroundGradient(sunriseElement.innerHTML, sunsetElement.innerHTML);
}, 60000);
}, () => {
// Error function
latitudeElement.innerHTML = "Unable to retrieve your location.";
longitudeElement.innerHTML = "Unable to retrieve your location.";
});
} else {
latitudeElement.innerHTML = "Geolocation is not supported by this browser.";
longitudeElement.innerHTML = "Geolocation is not supported by this browser.";
}
}
/**
* Fetch sunrise and sunset data from the Sunrise-Sunset API.
*/
async function fetchSunData(date) {
const dateString = date ? date.toISOString().split('T')[0] : 'today';
const apiUrl = `https://api.sunrise-sunset.org/json?lat=${latitude}&lng=${longitude}&formatted=0&date=${dateString}`;
return fetch(apiUrl)
.then(response => response.json())
.then(data => {
console.log(data);
const sunrise = data.results.sunrise;
const sunset = data.results.sunset;
console.log("Sunrise:", sunrise);
console.log("Sunset:", sunset);
return [sunrise, sunset];
})
.catch(error => {
console.error('Error fetching sun data:', error);
});
}
</script>
</html>