forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlandingpage.ts
426 lines (348 loc) · 12.1 KB
/
landingpage.ts
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
import {
ensureNotFalsy,
RxLocalDocument,
now,
promiseWait
} from '../';
import {
merge,
fromEvent,
map,
distinctUntilChanged
} from 'rxjs';
import { getDatabase } from './database';
type MousePositionType = {
x: number;
y: number;
time: number;
};
type BeatingValuesType = {
beatPeriod: number;
text1: string;
text2: string;
color: string;
};
const dbPromise = getDatabase();
window.onload = async function () {
/**
* Having blinking stuff can be annoying for people with
* neuronal problems. So we disable it for everyone
* who has set the reduced motions settings in the browser/OS
* @link https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion
* @link https://web.dev/prefers-reduced-motion/
* @link https://github.com/pubkey/rxdb/pull/3800
* @link https://a11y-101.com/development/reduced-motion
*/
const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (reducedMotion) {
console.log('reducedMotion is set to true');
return;
}
const database = await dbPromise;
// once insert if not exists
try {
await database.insertLocal<BeatingValuesType>('beatingvalues', {
beatPeriod: 0,
text1: 'JavaScript',
text2: 'you deserve',
color: colors[0]
});
} catch (err) { }
const beatingValuesDoc = ensureNotFalsy(await database.getLocal<BeatingValuesType>('beatingvalues'));
(async () => {
await promiseWait(heartbeatDuration);
while (true) {
const beatInfo = getBeatCurrentBeatInfo();
const nextBeatPromise = promiseWait(beatInfo.timeToNextPeriod);
// only every second interval so we have a pause in between
if (beatInfo.period % 2 === 0) {
try {
await beatingValuesDoc.incrementalModify(docData => {
if (docData.beatPeriod >= beatInfo.period) {
return docData;
}
docData.beatPeriod = beatInfo.period;
docData.color = colors[beatInfo.period % 3];
if (beatInfo.period % 4 === 0) {
docData.text1 = shuffleWithSeed(textsFirst, beatInfo.period)[0];
} else {
docData.text2 = shuffleWithSeed(textsSecond, beatInfo.period)[0];
}
return docData;
});
} catch (err) { }
}
await nextBeatPromise;
}
})();
// track mouse position
const mousePointerDoc = await database.upsertLocal<MousePositionType>('mousepos', {
x: 0,
y: 0,
time: 0
});
let currentMousePosition: number[] = [];
window.addEventListener('mousemove', (ev) => {
currentMousePosition = [ev.clientX, ev.clientY];
});
merge(
fromEvent(window, 'mousemove'),
fromEvent(window, 'scroll'),
fromEvent(window, 'resize')
).subscribe(() => {
mousePointerDoc.incrementalPatch({
x: currentMousePosition[0],
y: currentMousePosition[1],
time: now()
});
});
startTiltToMouse(mousePointerDoc);
startEnlargeOnMousePos(mousePointerDoc);
/**
* Pointers to html elements are prefixed with $
* Lists of pointers have $$
*/
const $$beating: any[] = document.getElementsByClassName('beating') as any;
const $$beatingFirst: any[] = document.getElementsByClassName('beating-first') as any;
const $$beatingSecond: any[] = document.getElementsByClassName('beating-second') as any;
const $$beatingNumber = document.getElementsByClassName('beating-number');
const $$beatingColor: any[] = document.getElementsByClassName('beating-color') as any;
const $$beatingColorString: any[] = document.getElementsByClassName('beating-color-string') as any;
// const $swapOutFirst = ensureNotFalsy(document.getElementById('swap-out-first'));
// const $swapOutSecond = ensureNotFalsy(document.getElementById('swap-out-second'));
const heartbeatListeners: any[] = [];
let heartbeatIndex = 0;
const heartbeatTimeToFirstBeat = 105;
getBeatCurrentBeatInfo();
beatingValuesDoc.$
.pipe(
map(d => d.data),
distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b))
)
.subscribe((beatingValuesDocInner: BeatingValuesType) => {
heartbeatListeners.forEach(function (listener) {
listener(heartbeatIndex);
});
heartbeatIndex = heartbeatIndex + 1;
// $swapOutFirst.innerHTML = beatingValuesDocInner.text1;
// $swapOutSecond.innerHTML = beatingValuesDocInner.text2;
const color = beatingValuesDocInner.color;
Array.from($$beatingColor).forEach(function (element) {
element.style.backgroundColor = color;
});
Array.from($$beatingColorString).forEach(function (element) {
element.innerHTML = color;
});
});
/**
* css animation of big logo on heartbeat
* Notice that we have to trigger a reflow
* when we want to restart the animation.
* @link https://css-tricks.com/restart-css-animation/
*/
heartbeatListeners.push(function () {
Array.from($$beating).forEach(function (element) {
element.style.animationDuration = heartbeatDuration + 'ms';
element.classList.remove('animation');
void element.offsetWidth;
element.classList.add('animation');
});
Array.from($$beatingFirst).forEach(function (element) {
element.style.animationDuration = heartbeatDuration + 'ms';
element.classList.remove('animation');
void element.offsetWidth;
element.classList.add('animation');
});
Array.from($$beatingSecond).forEach(function (element) {
element.style.animationDuration = heartbeatDuration + 'ms';
element.classList.remove('animation');
void element.offsetWidth;
element.classList.add('animation');
});
});
// increase beating numbers
heartbeatListeners.push(function () {
Array.from($$beatingNumber).forEach(function (element) {
// only increase randomly so it looks more natural.
if (randomBoolean() && randomBoolean()) {
setTimeout(function () {
const value = parseFloat(element.innerHTML);
const newValue = value + 1;
element.innerHTML = newValue + '';
}, heartbeatTimeToFirstBeat);
}
});
});
};
const maxDegree = 22;
const minDegree = -1 * maxDegree;
// use max values to ensure it never looks broken, even on big screens.
function ensureInRange(val: number): number {
if (val < minDegree) {
return minDegree;
}
if (val > maxDegree) {
return maxDegree;
}
return val;
}
/**
* @link https://armandocanals.com/posts/CSS-transform-rotating-a-3D-object-perspective-based-on-mouse-position.html
*/
function startTiltToMouse(mousePosDoc: RxLocalDocument<any, MousePositionType>) {
const $$tiltToMouse: any[] = document.getElementsByClassName('tilt-to-mouse') as any;
const constrain = 100;
function transforms(x: number, y: number, el: HTMLElement) {
const box = el.getBoundingClientRect();
const calcX = -(y - box.y - (box.height / 2)) / constrain;
const calcY = (x - box.x - (box.width / 2)) / constrain;
return `perspective(150px) rotateX(${ensureInRange(calcX)}deg) rotateY(${ensureInRange(calcY)}deg) `;
}
function transformElement(el: any, xyEl: number[]) {
el.style.transform = transforms.apply(null, xyEl as any);
}
mousePosDoc.$.subscribe((mousePos) => {
if (!mousePos.data.time) {
return;
}
Array.from($$tiltToMouse).forEach($element => {
if (!isInViewport($element)) {
return;
}
const position = ensureNotFalsy([mousePos.data.x, mousePos.data.y]).concat([$element]);
transformElement($element, position);
});
});
}
/**
* @link https://stackoverflow.com/a/16225919/3443137
*/
function startEnlargeOnMousePos(mousePosDoc: RxLocalDocument<any, MousePositionType>) {
const $$enlargeOnMouse: any[] = document.getElementsByClassName('enlarge-on-mouse') as any;
function getElementPosition(el: HTMLElement) {
const rect = el.getBoundingClientRect();
const centerX = rect.left + (rect.width / 2);
const centerY = rect.top + (rect.height / 2);
return {
centerX,
centerY,
width: rect.width,
height: rect.height
};
}
function enlargeElement(el: HTMLElement, scale: number) {
const transform = `scale(${scale})`;
el.style.transform = transform;
}
mousePosDoc.$.subscribe((mousePos) => {
if (
!mousePos.data.time ||
!mousePos.data.x ||
!mousePos.data.y
) {
return;
}
Array.from($$enlargeOnMouse).forEach($element => {
if (!isInViewport($element)) {
return;
}
const elementPosition = getElementPosition($element);
const dx = mousePos.data.x - elementPosition.centerX;
const dy = mousePos.data.y - elementPosition.centerY;
const distance = Math.sqrt(dx * dx + dy * dy);
function easeInQuint(x: number): number {
return x ^ 1.9;
}
let scale = 1 + (elementPosition.width / 2) / (easeInQuint(distance + 300));
if (scale > 1.5) {
scale = 1.5;
}
if (scale < 1.01) {
scale = 1;
}
enlargeElement($element, scale);
});
});
}
const heartbeatDuration = 851;
function getBeatCurrentBeatInfo() {
// remove a big chunk so we do not have a large number for better precision.
const time = new Date().getTime() - 1960000000;
const ratio = time / heartbeatDuration;
const period = Math.floor(ratio);
const timeToNextPeriod = (ratio - period) * heartbeatDuration;
return {
ratio,
period,
timeToNextPeriod
};
}
const colors = [
'#e6008d',
'#8d2089',
'#5f2688'
];
const textsFirst = [
'NoSQL',
'OfflineFirst',
'JavaScript',
'observable',
'reactive',
'realtime',
'client side',
'fast'
];
const textsSecond = [
'for the Web',
'for Node.js',
'for Browsers',
'for Capacitor',
'for Electron',
'for hybrid apps',
'for PWAs',
'for React Native',
'for NativeScript',
'for UI apps',
'you deserve',
'that syncs',
];
// UTILS
function randomBoolean() {
return Math.random() < 0.5;
}
/**
* @link https://stackoverflow.com/questions/16801687/javascript-random-ordering-with-seed
*/
function shuffleWithSeed<T>(array: T[], seed: number): T[] {
array = array.slice(0);
let m = array.length;
let t;
let i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(random(seed) * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
++seed;
}
return array;
}
function random(seed: number) {
const x = Math.sin(seed++) * 10000;
return x - Math.floor(x);
}
/**
* @link https://www.javascripttutorial.net/dom/css/check-if-an-element-is-visible-in-the-viewport/
*/
function isInViewport(el: any) {
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}