forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlandingpage.js
211 lines (189 loc) · 6.92 KB
/
landingpage.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
window.onload = function () {
/**
* Pointers to html elements are prefixed with $
* Lists of pointers have $$
*/
var $heartbeatAudio = document.getElementById('heartbeat-audio');
var $$beating = document.getElementsByClassName('beating');
var $$beatingFirst = document.getElementsByClassName('beating-first');
var $$beatingSecond = document.getElementsByClassName('beating-second');
var $$beatingNumber = document.getElementsByClassName('beating-number');
var $$beatingColor = document.getElementsByClassName('beating-color');
var $swapOutFirst = document.getElementById('swap-out-first');
var $swapOutSecond = document.getElementById('swap-out-second');
var audioVolume = 0.5;
$heartbeatAudio.volume = audioVolume;
var heartbeatListeners = [];
var heartbeatIndex = 0;
var heartbeatDuration = 851.088;
var heartbeatTimeToFirstBeat = 105;
var heartbeatTimeToSecondBeat = 324;
console.log('heartbeatDuration: ' + heartbeatDuration);
/**
* For iOS we have to trigger the audio start on the first user interaction.
* This will 'unlock' the audio element so we can later play() at any time.
* @link https://www.py4u.net/discuss/287774
*/
var touchStartDone = false;
window.addEventListener('touchstart', function () {
if (touchStartDone) {
return;
}
touchStartDone = true;
console.log('touchstart: START');
$heartbeatAudio.volume = 0.01;
$heartbeatAudio.play().catch(function () { /* Ignore erros */ });
setTimeout(function () {
$heartbeatAudio.pause();
$heartbeatAudio.currentTime = 0;
console.log('touchstart: END');
}, 1);
});
setInterval(function () {
/**
* Only run when browser tab is active
* to not annoy the user with background sound otherwise.
*/
if (!document.hidden) {
heartbeatListeners.forEach(function (listener) {
listener(heartbeatIndex);
});
heartbeatIndex = heartbeatIndex + 1;
}
}, heartbeatDuration * 1.9);
// swap out main text on every X heartbeat
var swapOutTextEveryX = 1;
var swapOutsDone = 0;
function swapMainText(index) {
var textsFirst = [
'NoSQL',
'OfflineFirst',
'JavaScript',
'observable',
'reactive',
'realtime'
];
var textsSecond = [
'for the Web',
'for Node.js',
'for Browsers',
'for Capacitor',
'for Electron',
'for PWAs',
'for UI apps',
'you deserve',
'that syncs',
];
/**
* Do not directly change the text on the audio start,
* but wait a bit for the first beat sound.
*/
setTimeout(function () {
if (
index > 2 &&
index % swapOutTextEveryX === 0
) {
swapOutsDone = swapOutsDone + 1;
if (swapOutsDone % 2 === 0) {
$swapOutFirst.innerHTML = randomOfArray(textsFirst, $swapOutFirst.innerHTML);
} else {
$swapOutSecond.innerHTML = randomOfArray(textsSecond, $swapOutSecond.innerHTML);
}
}
}, heartbeatTimeToFirstBeat);
}
// beat sound on heartbeat
heartbeatListeners.push(function (index) {
/**
* This might throw because we cannot play audio when the user
* has not interacted with the dom yet.
**/
$heartbeatAudio.play()
.then(function () {
/**
* Only swap out the main text when the audio was playing,
* so we ensure that the user interacted with the site.
*/
swapMainText(index);
/**
* If play() did not error,
* we decrease the volume to
* ensure we do not piss of people that are looking
* for which browser tab is playing audio.
*/
audioVolume = audioVolume * 0.9;
$heartbeatAudio.volume = audioVolume;
}).catch(function () {
/**
* This might fail if the user has not already interaced with the UI
* and we are not allowed to play sound yet.
* Then we just do not play sound and ignore the error.
*/
});
});
// css animation of big logo on heartbeat
heartbeatListeners.push(function () {
Array.from($$beating).forEach(function (element) {
element.style.animationDuration = heartbeatDuration + 'ms';
element.classList.remove('animation');
element.offsetWidth
element.classList.add('animation');
});
Array.from($$beatingFirst).forEach(function (element) {
element.style.animationDuration = heartbeatDuration + 'ms';
element.classList.remove('animation');
element.offsetWidth
element.classList.add('animation');
});
Array.from($$beatingSecond).forEach(function (element) {
element.style.animationDuration = heartbeatDuration + 'ms';
element.classList.remove('animation');
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 () {
var value = parseFloat(element.innerHTML, 10);
var newValue = value + 1;
element.innerHTML = newValue + '';
}, heartbeatTimeToFirstBeat);
}
});
});
// tablet swap color on heartbeat
var colors = [
'#e6008d',
'#8d2089',
'#5f2688'
];
var lastColorByElementIndex = {};
heartbeatListeners.push(function () {
setTimeout(function () {
Array.from($$beatingColor).forEach(function (element, idx) {
var isColor = lastColorByElementIndex[idx];
if (!isColor) {
isColor = colors[0];
}
var newColor = randomOfArray(colors, isColor);
lastColorByElementIndex[idx] = newColor;
element.style.backgroundColor = newColor;
});
}, heartbeatTimeToSecondBeat);
});
};
// UTILS
function randomBoolean() {
return Math.random() < 0.5;
}
function randomOfArray(array, mustNotBe) {
var ret;
while (!ret || ret === mustNotBe) {
ret = array[Math.floor(Math.random() * array.length)];
}
return ret;
}