-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
272 lines (249 loc) · 8.66 KB
/
main.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
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
Colors = {
hueForGeneration: Math.random(),
hsvToRgb(h, s, v) {
var r, g, b;
var i = Math.floor(h * 6);
var f = h * 6 - i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0:
(r = v), (g = t), (b = p);
break;
case 1:
(r = q), (g = v), (b = p);
break;
case 2:
(r = p), (g = v), (b = t);
break;
case 3:
(r = p), (g = q), (b = v);
break;
case 4:
(r = t), (g = p), (b = v);
break;
case 5:
(r = v), (g = p), (b = q);
break;
}
return [r * 255, g * 255, b * 255];
},
randomHSVColor() {
const goldenRatioConjugate = Math.PI / 2 + 0.618033988749895;
this.hueForGeneration += goldenRatioConjugate;
const hue = this.hueForGeneration % 1;
const saturation = 0.4 + 0.4 * Math.random();
const value = 0.7 + Math.random() / 10;
return [hue, saturation, value];
},
randomRGBColor() {
return this.hsvToRgb(...this.randomHSVColor());
},
};
class Options {
constructor(configuration) {
this.configuration = configuration;
}
getRandomGroundingTechnique() {
const techniques = this.configuration.grounding;
return this.getRandom(techniques);
}
getRandomRecoveryTechnique() {
const techniques = this.configuration.recovery;
return this.getRandom(techniques);
}
getRandomCareTechnique() {
const techniques = this.configuration.care;
return this.getRandom(techniques);
}
getRandom(fromArray) {
return fromArray[Math.floor(Math.random() * fromArray.length)];
}
}
function renderTechnique(technique) {
const text = technique.text;
const color = technique.color;
technique.element.innerHTML = `<p>${text}</p>`;
technique.element.style.backgroundColor = `rgb(${color[0]},${color[1]},${color[2]})`;
}
async function main() {
const configuration = {
grounding: [
"Listen to music",
"Sing along to your favorite song",
"Do some yoga",
"Go for a walk",
"Stretch your muscles",
"Go for a run",
"Cycle for a bit",
"Practice dancing",
"Find a thing with a scent and smell it",
"Count 5 things you see, then hear, feel, and smell",
"Do physical exercise",
"Write a diary entry",
"Write a short story",
"Follow an exercise video",
"Knit / Sew / Crochet",
"Drink one of your favorite beverages",
"Watch or play some sports",
"Do a breathing exercise",
"Listen to calming music",
"Eat something sour",
"Drink coffee or tea",
"Hug a stuffed animal",
"Feel the wind or a fan on your face",
"Drink some hot cocoa",
"Take a few deep breaths",
],
recovery: [
"Watch an old favorite movie",
"Meditate",
"Cook something for yourself",
"Bake something for yourself",
"Watch your favorite TV-show",
"Watch a new movie",
"Read a chapter of a book / Find a new book",
"Study a subject you love",
"Spend time in nature",
"Make a scrapbook page",
"Do puzzles / brain games",
"Craft something",
"Play a video game",
"Do a compassion exercise",
"Make a fitting music mix",
"Color a coloring book / picture",
"Do some people watching",
"Stand on the porch / balcony",
"Hide in bed with under a blanket",
"Lie down for a while",
"Read some comics",
"Draw",
"Go for a walk",
"Give yourself a break",
"Pet your shoulder",
"Tell yourself it's going to be okay",
],
care: [
"Take care of your nails",
"Take care of the skin on your face",
"Stretch your eye muscles by looking really far",
"Do some stretches",
"Take a bath or shower",
"Take care of your skin",
"Spend time with your pet / virtual pet",
"Pamper yourself",
"Do some gardening",
"Light a candle that smells good",
"Spend time with / call your friends",
"Eat something replenishing",
"Drink some tea",
"Take a nap",
"Take care of your oral hygiene",
"Use some lip balm",
"Take care of your hair",
"Take care of your surroundings",
"Clean your working area",
"Listen to some comedy",
"Make sure you stay hydrated",
"Take care of your hair",
"Eat some chocolate",
"Take a moment for yourself",
],
};
function renderTechniques(texts) {
return (
'<ul class="scrollable"><li></li><li></li><li></li><li>' +
texts.join("</li><li>") +
"</li><li></li><li></li><li></li></ul>"
);
}
const groundingEl = document.getElementById("grounding");
const recoveryEl = document.getElementById("recovery");
const careEl = document.getElementById("care");
groundingEl.innerHTML = renderTechniques(configuration.grounding);
recoveryEl.innerHTML = renderTechniques(configuration.recovery);
careEl.innerHTML = renderTechniques(configuration.care);
function throttle(fn, wait) {
let time;
return function () {
if (!time || time + wait - Date.now() < 0) {
fn();
time = Date.now();
}
};
}
const onScrollTextHighlight = (element) => () => {
for (const child of element.children) {
const scrollerBox = element.getBoundingClientRect();
const height = scrollerBox.height;
const boundingBox = child.getBoundingClientRect();
const position =
boundingBox.top - scrollerBox.top + boundingBox.height / 2;
const middle = height / 2;
const distance = Math.abs(middle - position);
child.style.opacity = 1 - (distance / height) * ((window.innerHeight * 3) / height);
}
};
const onScrollBgColor = (element) => () => {
requestAnimationFrame(() => {
const color = Colors.randomRGBColor();
element.style.backgroundColor = `rgb(${color[0]},${color[1]},${color[2]})`;
});
};
groundingEl.children[0].addEventListener(
"scroll",
throttle(onScrollBgColor(groundingEl), 1000)
);
groundingEl.children[0].addEventListener(
"scroll",
throttle(onScrollTextHighlight(groundingEl.children[0]), 50),
{ passive: true }
);
recoveryEl.children[0].addEventListener(
"scroll",
throttle(onScrollBgColor(recoveryEl), 1000)
);
recoveryEl.children[0].addEventListener(
"scroll",
throttle(onScrollTextHighlight(recoveryEl.children[0]), 50),
{ passive: true }
);
careEl.children[0].addEventListener(
"scroll",
throttle(onScrollBgColor(careEl), 1000)
);
careEl.children[0].addEventListener(
"scroll",
throttle(onScrollTextHighlight(careEl.children[0]), 50),
{ passive: true }
);
const scrollOptions = {
behavior: "smooth",
block: "center",
};
const spacers = 3;
function scrollToRandom() {
const selectedGrounding =
groundingEl.children[0].children[
Math.floor(Math.random() * configuration.grounding.length) +
spacers
];
selectedGrounding.scrollIntoView(scrollOptions);
const selectedRecovery =
recoveryEl.children[0].children[
Math.floor(Math.random() * configuration.recovery.length) +
spacers
];
selectedRecovery.scrollIntoView(scrollOptions);
const selectedCare =
careEl.children[0].children[
Math.floor(Math.random() * configuration.care.length) + spacers
];
selectedCare.scrollIntoView(scrollOptions);
}
const randomizeButton = document.getElementById("randomize-button");
randomizeButton.addEventListener("click", scrollToRandom);
setTimeout(() => randomizeButton.click(), 0);
}
main();