Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
318 lines (276 loc) · 9.13 KB
/
Copy pathindex.html
File metadata and controls
318 lines (276 loc) · 9.13 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
<html lang="en">
<head>
<meta charset="UTF-8">
<title>touch grass</title>
<meta name="description" content="touch grass">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/png" href="/favicon.png">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<style>
@import url('https://fonts.googleapis.com/css?family=Inter:400');
:root {
--background: url(assets/grass.png);
}
body {
background: var(--background) !important;
background-size: cover !important;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100vw;
margin: 0;
overflow: hidden;
font-family: 'Inter';
transition: background 5s cubic-bezier(0.61, 1, 0.88, 1);
}
.spark {
position: absolute;
width: 30px;
height: 8px;
border-radius: 3px;
background-color: white;
transform: none;
}
a {
text-decoration: none;
color: rgba(255, 255, 255, 0.5);
}
a:hover {
color: #fff;
}
#github {
position: fixed;
top: 0;
right: 0;
padding: 2rem;
}
.milestone-message {
position: fixed;
top: 1rem;
left: 1rem;
background-color: rgba(255, 255, 255, 0.4);
backdrop-filter: blur(30px);
-webkit-backdrop-filter: blur(30px);
color: black;
padding: 1rem;
border-radius: 1rem;
z-index: 1000;
display: flex;
align-items: center;
justify-content: space-between;
max-width: 26rem;
}
.milestone-message button {
background: none;
border: none;
font-size: 16px;
cursor: pointer;
color: black;
margin-left: 10px;
}
#sunriseSunset {
opacity: 0;
position: fixed;
bottom: 0;
right: 0;
padding: 1rem;
color: rgba(255, 255, 255, 0.4);
transition: opacity 1s linear;
}
</style>
</head>
<body>
<div id="box" class="container"></div>
<a id="github" href="https://github.com/juxtopposed/touchgrass" target="_blank" class="material-icons">help</a>
<p id="sunriseSunset"> Powered by <a href="https://sunrisesunset.io/" target="_blank">SunriseSunset.io</a></p>
<script>
const getCurrentDayTime = async ({ latitude, longitude }) => {
const response = await fetch(`https://api.sunrisesunset.io/json?lat=${latitude}&lng=${longitude}`);
if (!response.ok) { throw new Error("Failed to fetch day times") };
const { results } = await response.json();
const currentTime = new Date();
const currentDate = currentTime.toLocaleDateString("en-US");
const { first_light, sunrise, sunset, last_light } = results;
const [firstLightTime, sunriseTime, sunsetTime, lastLightTime] = [
first_light,
sunrise,
sunset,
last_light,
].map((time) => new Date(`${currentDate} ${time}`));
if (currentTime < firstLightTime) {
return "night";
} else if (currentTime < sunriseTime) {
return "sunrise";
} else if (currentTime < sunsetTime) {
return "day";
} else if (currentTime < lastLightTime) {
return "sunset";
} else {
return "night";
}
};
const getLocation = async () => {
return await new Promise((resolve, reject) => {
const onSuccess = (position) => {
const { latitude, longitude } = position.coords;
resolve({ latitude, longitude });
};
const onError = (error) => reject(error);
navigator.geolocation.getCurrentPosition(onSuccess, onError);
});
};
const updateBackground = async () => {
let currentDayTime;
try {
const { latitude, longitude } = await getLocation();
currentDayTime = await getCurrentDayTime({ latitude, longitude });
document.getElementById("sunriseSunset").style.opacity = "1";
} catch (error) {
console.error(error);
}
let backgroundUrl;
switch (currentDayTime) {
case "night":
backgroundUrl = "url(assets/grass_night.png)";
break;
case "sunset":
backgroundUrl = "url(assets/grass_sunset.png)";
break;
case "sunrise":
backgroundUrl = "url(assets/grass_sunrise.png)";
break;
default:
backgroundUrl = "url(assets/grass.png)";
break;
}
document.body.style.setProperty("--background", backgroundUrl);
};
updateBackground();
const oneHourMs = 1000 * 60 * 60;
setInterval(updateBackground, oneHourMs);
</script>
<script>
let styleSheet = null;
const SPARK_ELEMENT_WIDTH = 30;
const DISTANCE = 40;
const RANDOMNESS_ON = true;
const milestones = [50, 100, 500, 1000, 10000, 50000, 100000, 500000, 1000000, 10000000];
function createTransformSteps() {
if (Array.from(arguments).length === 0) {
throw new Error("arguments to createTransformSteps should never be empty!");
}
const inputSteps = Array.from(arguments);
const outputSteps = [inputSteps.shift()];
inputSteps.forEach((step, i) => {
outputSteps.push(`${outputSteps[i]} ${step}`);
});
return outputSteps;
}
const dynamicAnimation = (name, rotation) => {
if (!styleSheet) {
styleSheet = document.createElement("style");
styleSheet.type = "text/css";
document.head.appendChild(styleSheet);
}
const randomDist = RANDOMNESS_ON
? Math.floor((Math.random() - 0.5) * DISTANCE * 0.7)
: 0;
const [s1, s2, s3] = createTransformSteps(
`translate(-50%, -50%) rotate(${rotation}deg) translate(10px, 0px)`,
`translate(${DISTANCE + randomDist}px, 0px) scale(0.5, 0.5)`,
`translate(${SPARK_ELEMENT_WIDTH / 2}px, 0) scale(0, 0)`
);
styleSheet.sheet.insertRule(
`@keyframes ${name} {
0% {
transform: ${s1};
}
70% {
transform: ${s2};
}
100% {
transform: ${s3};
}
}`,
styleSheet.length
);
};
document.body.addEventListener("pointerdown", (e) => {
e.preventDefault();
const center = { x: e.pageX, y: e.pageY };
incrementClickCount();
makeBurst(center);
});
const makeBurst = (center) => {
for (let i = 0; i < 8; i++) {
const randomSpace = RANDOMNESS_ON
? Math.floor(-17 + Math.random() * 34)
: 0;
makeSpark(center, 45 * i + randomSpace);
}
};
const makeSpark = (center, rotation) => {
const div = document.createElement("div");
const aniName = `disappear_${rotation}`;
dynamicAnimation(aniName, rotation);
div.classList.add("spark");
div.style.left = `${center.x}px`;
div.style.top = `${center.y}px`;
div.style.animation = `${aniName} 500ms ease-out both`;
document.body.append(div);
setTimeout(() => {
document.body.removeChild(div);
}, 1000);
};
const incrementClickCount = () => {
let clickCount = parseInt(localStorage.getItem("clickCount")) || 0;
clickCount++;
localStorage.setItem("clickCount", clickCount);
checkMilestones(clickCount);
};
const milestoneMessages = {
50: "wow, you've touched grass 50 times! :)",
100: "amazing, you've touched grass 100 times! :D",
500: "500 times!!! leave some grass for the rest of us.",
1000: "alright slow down. 1000 times is the safe daily limit. back to your phone now.",
10000: "you've touched grass 10,000 times (good job i guess)",
50000: "50K! you did it! alright enough grass, go back home (or give your mouse some rest)",
100000: "alright it's not even cool anymore. get some screen time...",
500000: "idk what to make of 500,000 times of touching grass... but yayyy you did it.",
1000000: "you've touched grass 1 MILLION times! phew! you may go home now, no more grass or milestones for you.",
10000000: "how are you still here?..."
};
const checkMilestones = (clickCount) => {
if (milestones.includes(clickCount)) {
const message = milestoneMessages[clickCount];
showMilestoneMessage(message);
if (clickCount == 10000000) {
setTimeout(() => {
window.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ", '_blank').focus();
}, 2000);
}
}
};
const showMilestoneMessage = (message) => {
const messageDiv = document.createElement("div");
messageDiv.classList.add("milestone-message");
const text = document.createElement("span");
text.innerText = message;
const closeButton = document.createElement("button");
closeButton.innerText = "×";
closeButton.addEventListener("click", () => {
document.body.removeChild(messageDiv);
});
messageDiv.appendChild(text);
messageDiv.appendChild(closeButton);
document.body.appendChild(messageDiv);
setTimeout(() => {
if (document.body.contains(messageDiv)) {
document.body.removeChild(messageDiv);
}
}, 10000);
};
</script>
</body>
</html>