-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
276 lines (257 loc) · 10.2 KB
/
index.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
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
<!DOCTYPE HTML>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,600" rel="stylesheet">
<style>
html {
text-align: right;
font-family: 'Open Sans', sans-serif;
font-size: 12vmin;
font-style: normal;
font-weight: 300;
color: white;
background: black;
-webkit-font-smoothing: subpixel-antialiased;
overflow: hidden;
}
#dayOfMonthOrdinal {
font-size: 67%;
vertical-align: super;
}
#timeColon {
vertical-align: 0.07em;
opacity: 0;
will-change: opacity;
display: inline-block;
}
.date {
font-size: 60%;
}
.time {
font-weight: 600;
font-size: 260%;
margin-top: -0.15em;
}
.weather {
display: inline-block;
text-align: center;
}
.forecast {
position: relative;
max-width: 70vmin;
font-size: 140%;
margin-top: 0.1em;
/* Leave space for icons. */
padding-left: 24vmin;
min-height: 24vmin;
}
.weather .forecast:not(:first-child) {
margin-top: 0.3em;
}
.icon {
width: 24vmin;
height: 24vmin;
position: absolute;
left: 0;
/* center vertically */
top: 50%;
transform: translate(0, -50%);
}
.icon:not([src]) {
display: none;
}
.temp {
font-weight: 600;
}
.details {
font-size: 22%;
margin-top: -0.8em;
}
.christmas {
background-image: repeating-linear-gradient(90deg, red 0em, green 1em, red 2em);
color: transparent;
background-clip: text;
-webkit-background-clip: text;
}
</style>
<div class="date">
<span id="day">Saturday</span>, <span id="month">Sep</span> <span id="dayOfMonth">10</span><span id="dayOfMonthOrdinal">st</span>
</div>
<div class="time">
<span id="hour">5</span><span id="timeColon">:</span><span id="minute">21</span>
</div>
<div class="weather">
<div id="current" class="forecast">
<img class="icon">
<span class="temp">70</span>
<div class="details">Current: Unknown</div>
</div>
<div id="next" class="forecast">
<img class="icon">
<span class="temp">70</span>
<div class="details">Next: Unknown</div>
</div>
</div>
<script>
var weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
function dayOfWeekName(date) {
return weekdays[date.getDay()];
}
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
function monthName(date) {
return months[date.getMonth()];
}
// Return the English ordinal for a number.
function ordinal(number) {
// The general pattern:
// number ends in 1 -> st (e.g., 31st)
// number ends in 2 -> nd (e.g., 32nd)
// number ends in 3 -> rd (e.g., 33rd)
// 11, 12, and 13 are exceptions and just use 'th'.
switch(number % 10) {
case(1): if (number % 100 == 11) { break; } else { return 'st' };
case(2): if (number % 100 == 12) { break; } else { return 'nd' };
case(3): if (number % 100 == 13) { break; } else { return 'rd' };
}
return 'th';
}
var colonVisible = false;
function updateTimeColon() {
colonVisible = !colonVisible;
timeColon.style.opacity = colonVisible ? 1 : 0;
}
// Return an adjusted brightness level for the current date.
//
// dayLevel .---------------------.
// / \
// / \
// nightLevel -------------' '------------
// ^ ^ ^ ^ ^ ^
// 0 ... dayStart dayStart+1 ... dayEnd-1 dayEnd ... 23
//
// dayStart and dayEnd are in units of hours. The brightness is smoothly
// transitioned over one hour.
function brightness(date, dayStart, dayEnd, dayLevel, nightLevel) {
// The current hour and minutes, in units of hours (0, 24].
var hours = date.getHours() + date.getMinutes() / 60;
// Night to day transition: linearly interpolate from start to start+1.
if (hours > dayStart && hours < dayStart + 1)
return nightLevel + (hours - dayStart) * (dayLevel - nightLevel);
if (hours >= dayStart + 1 && hours <= dayEnd - 1)
return dayLevel;
// Day to night transition: linearly interpolate from end-1 to end.
if (hours > dayEnd - 1 && hours < dayEnd)
return dayLevel + (hours - dayEnd + 1) * (nightLevel - dayLevel);
return nightLevel;
}
function updateDateTime() {
var date = new Date();
day.innerText = dayOfWeekName(date);
month.innerText = monthName(date);
dayOfMonth.innerText = date.getDate();
dayOfMonthOrdinal.innerText = ordinal(date.getDate());
hour.innerText = date.getHours();
minute.innerText = (date.getMinutes() < 10 ? '0' : '') + date.getMinutes();
// Fade the display brightness at night (before 7:00 and after 22:00).
document.body.style.opacity = brightness(date, 7, 22, 1, 0.3);
// Add christmas color effect on December 24th and 25th.
if (date.getMonth() == 11 && (date.getDate() == 24 || date.getDate() == 25))
document.getElementsByClassName('date')[0].classList.add('christmas');
}
// Callback with the current and next forecast for the given lat/lon. The forecasts
// have a name, temperature, shortForecast (description), and icon.
// This uses api.weather.gov, see: https://forecast-v3.weather.gov/documentation
function getForecast(latitude, longitude, callback) {
// Convert from the weather.gov icons (api.weather.gov/icons) to one of our svg icons.
function updateIconUrl(forecastPeriod) {
var iconUrlMap = {
'skc': 'clear.svg',
'few': 'mostlysunny.svg',
'sct': 'partlycloudy.svg',
'bkn': 'mostlycloudy.svg',
'ovc': 'cloudy.svg',
'wind_skc': 'clear.svg',
'wind_few': 'mostlysunny.svg',
'wind_sct': 'partlycloudy.svg',
'wind_bkn': 'cloudy.svg',
'wind_ovc': 'cloudy.svg',
'snow': 'snow.svg',
'rain_snow': 'sleet.svg',
'rain_sleet': 'sleet.svg',
'snow_sleet': 'sleet.svg',
'fzra': 'sleet.svg',
'rain_fzra': 'sleet.svg',
'snow_fzra': 'sleet.svg',
'sleet': 'sleet.svg',
'rain': 'rain.svg',
'rain_showers': 'rain.svg',
'rain_showers_hi': 'rain.svg',
'tsra': 'tstorms.svg',
'tsra_sct': 'tstorms.svg',
'tsra_hi': 'tstorms.svg',
'tornado': 'unknown.svg',
'hurr_warn': 'tstorms.svg',
'hurr_watch': 'tstorms.svg',
'ts_warn': 'tstorms.svg',
'ts_watch': 'tstorms.svg',
'ts_hurr_warn': 'tstorms.svg',
'dust': 'hazy.svg',
'smoke': 'hazy.svg',
'haze': 'hazy.svg',
'hot': 'clear.svg',
'cold': 'clear.svg',
'blizzard': 'snow.svg',
'fog': 'fog.svg'
};
var icon = 'unknown.svg';
for (var iconUrl in iconUrlMap) {
if (forecastPeriod.icon.indexOf('/' + iconUrl) !== -1) {
icon = iconUrlMap[iconUrl];
break;
}
}
forecastPeriod.icon = icon;
return forecastPeriod;
}
var forecastUrl = 'https://api.weather.gov/points/' + latitude + ',' + longitude + '/forecast';
var forecastRequest = new Request(forecastUrl);
fetch(forecastRequest)
.then(function(response) {
return response.json();
}).then(function(json) {
var current = updateIconUrl(json.properties.periods[0]);
var next = updateIconUrl(json.properties.periods[1]);
callback(current, next);
}).catch(function(error) {
var errorForecast = {
name: 'Unknown',
temperature: '70',
shortForecast: 'Unknown',
icon: 'unknown.svg'
};
callback(errorForecast, errorForecast);
});
}
function updateWeather() {
var latitude = '37.7746088';
var longitude = '-122.3957547';
getForecast(latitude, longitude, function(currentForecast, nextForecast) {
function updateForecastDom(forecastElement, forecast) {
var iconElement = forecastElement.getElementsByClassName('icon')[0];
iconElement.src = 'icons/' + forecast.icon;
var tempElement = forecastElement.getElementsByClassName('temp')[0];
tempElement.innerText = forecast.temperature + '°';
var detailsElement = forecastElement.getElementsByClassName('details')[0];
detailsElement.innerText = forecast.name + ': ' + forecast.shortForecast;
}
updateForecastDom(document.getElementById('current'), currentForecast);
updateForecastDom(document.getElementById('next'), nextForecast);
});
}
window.setInterval(updateTimeColon, 1000); // Update time colon once per second (1000ms).
window.setInterval(updateDateTime, 60000); // Update date&time once per minute (60*1000ms).
updateDateTime(); // Immediately update once so the date&time is correct for the first frame.
window.setInterval(updateWeather, 3600000); // Update weather every hour (60*60*1000ms).
updateWeather(); // Immediately update once so the weather is correct for the first frame.
window.setInterval(function() { location.reload(); }, 86400000); // Refresh every day (24*60*60*1000ms).
</script>