-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1356 lines (1300 loc) · 42.5 KB
/
index.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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// CONSTANTS
const CELLS_PER_ROW = 25;
const CELLS_PER_ROW_MOBILE = 13;
const PROPAGATION_DELAY = 31;
const CELL_TOUCH_ANIMATION_LENGTH = 500;
const CELL_TOUCH_ANIMATION_FPS = 18;
const DISTANCE_OF_DESTRUCTION = 4;
const TIME_BETWEEN_DOT_GENERATE = 8000;
const MIN_DOT_SIZE = 6; // relative size in percentage
const MAX_DOT_SIZE = 8; // relative size in percentage
const MIN_DOT_SIZE_MOBILE = 20;
const MAX_DOT_SIZE_MOBILE = 25;
const DOT_ANIMATION_LENGTH = 1000;
const MAX_DOTS = 3;
const MAX_DOT_EXISTING_TIME = MAX_DOTS * TIME_BETWEEN_DOT_GENERATE;
const LOADING_FADE_OUT_TIME = 1000;
const MIN_LOADING_TIME = 4000;
const MAIN_CONTENT_SIZE = 20; // relative size in percentage
const MAIN_CONTENT_SIZE_MOBILE = 15; // relative size in percentage
const MIN_VISUALIZATION_UPDATE_INTERVAL = 17; // max 59fps
const MAX_VISUALIZATION_UPDATE_INTERVAL = 50; // max 20fps
const TRACK_INFO_HEIGHT = 0.8; // relative size compared to main content
const TRACK_INFO_TRAVEL_TIME = 40000;
const BTCUSDT_STREAM_NAME = 'btcusdt@aggTrade';
const BTCUSDT_STATISTIC_STREAM_NAME = 'btcusdt@ticker';
const ETHUSDT_STREAM_NAME = 'ethusdt@aggTrade';
const ETHUSDT_STATISTIC_STREAM_NAME = 'ethusdt@ticker';
const WS_CRYPTO_SUBSCRIBE_MESSAGE = {
method: 'SUBSCRIBE',
params: [
BTCUSDT_STREAM_NAME,
BTCUSDT_STATISTIC_STREAM_NAME,
ETHUSDT_STREAM_NAME,
ETHUSDT_STATISTIC_STREAM_NAME,
],
id: 1,
};
const PONG_MESSAGE_TIME_INTERVAL = 5 * 60 * 1000;
const CRYPTO_UPDATE_RATE = 4;
const TOP_CELLS_EFFECT_COLORS = [
[0, 255, 255], [255, 0, 255], [255, 255, 0],
[255, 255, 255], [255, 127, 0], [255, 0, 127],
[127, 255, 0], [0, 255, 127], [127, 0, 255], [0, 127, 255]];
const DATE_ABBREVIATION = {
Mon: 'Monday',
Tue: 'Tuesday',
Wed: 'Wednesday',
Thu: 'Thursday',
Fri: 'Friday',
Sat: 'Saturday',
Sun: 'Sunday',
Jan: 'January',
Feb: 'February',
Mar: 'March',
Apr: 'April',
May: 'May',
Jun: 'June',
Jul: 'July',
Aug: 'August',
Sep: 'September',
Oct: 'October',
Nov: 'November',
Dec: 'December',
};
// GLOBAL VARS
let rows;
let cols;
const AppContainer = document.getElementById('app');
const btcPriceTrackingContainer = document.querySelector('#widgets .btc-tracking');
const ethPriceTrackingContainer = document.querySelector('#widgets .eth-tracking');
const cells = [];
const intervalIds = [];
const isCellExisted = [];
let audioCtx = null;
const audioOutput = document.getElementById('audio-output');
let audioSource = null;
let gainNode = null;
let audioAnalyser = null;
let tracks = [];
let currentTrackIndex = -1;
let trackData = null;
const visualizationBars = document.getElementsByClassName('bar');
let widgetsExpanded = false;
let wsInstance = null;
let wsEventID = 3;
let lastPongMessageTime = 0;
let lastBTCPrice = null;
let lastETHPrice = null;
let lastMessageTime = 0;
const weatherInfo = {};
// Marina Bay coordinates
let lat = 1.2878;
let long = 103.8666;
const pageLoadPromise = new Promise((resolve) => {
window.addEventListener('load', resolve);
});
let animationCells = 0;
let visualizationUpdateInterval = MIN_VISUALIZATION_UPDATE_INTERVAL;
// pre-calculate scaling values to reduce resources later
const destroyedCellAnimationScale = Array(DISTANCE_OF_DESTRUCTION + 1).fill(null).map(() => []);
let cellAnimationFrames = 0;
const dotGameLinks = [
{
imgSrc: 'github',
linkElement: document.getElementById('a-github'),
},
{
imgSrc: 'gmail',
linkElement: document.getElementById('a-gmail'),
},
{
imgSrc: 'linkedin',
linkElement: document.getElementById('a-linkedin'),
},
{
imgSrc: 'whatsapp',
linkElement: document.getElementById('a-whatsapp'),
},
];
let decoPatternUrl = '';
// UTILS
function square(number) {
return number * number;
}
function createFrString(num) {
let res = '1fr';
for (let i = 1; i < num; i++) {
res += ' 1fr';
}
return res;
}
function getRandom(min, max) {
return (Math.floor(Math.random() * 1000000000) % (max - min + 1)) + min;
}
function makeAnimation(element, className) {
element.classList.remove(className);
// 2 consecutive classList operations will be merged into one
// trigger reflow, in order to make it work
element.offsetHeight;
element.classList.add(className);
}
function isMobileDevice() {
if (navigator.userAgentData) {
return navigator.userAgentData.mobile;
}
const toMatch = [
/Android/i,
/webOS/i,
/iPhone/i,
/iPad/i,
/iPod/i,
/BlackBerry/i,
/Windows Phone/i,
];
return toMatch.some((toMatchItem) => navigator.userAgent.match(toMatchItem));
}
function getTime() {
return Date.now();
}
function sleep(time) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}
function addUniversalSensitiveClickListener(element, handler, options = {}) {
// for mobile
element.addEventListener('touchstart', (event) => {
if (!isMobileDevice()) {
return;
}
handler(event);
}, options);
// for PC
element.addEventListener('mousedown', (event) => {
if (isMobileDevice()) {
return;
}
// only run with left click event
if (event.buttons !== 1) {
return;
}
handler(event);
}, options);
}
function roundToOddNumber(value) {
const lower = Math.floor(value);
const upper = lower + 1;
return lower % 2 !== 0 ? lower : upper;
}
function formatCryptoNumber(number, {
hasDollarSign = true,
alwaysShowSign = false,
fractionDigits = 2,
} = {}) {
const numStr = number.toFixed(fractionDigits);
let result = '';
if (alwaysShowSign) {
result += number >= 0 ? '+' : '-';
} else if (number < 0) {
result += '-';
}
if (hasDollarSign) {
result += '$';
}
const integral = `${Math.trunc(Math.abs(number))}`;
const integralLen = integral.length;
for (let i = 0; i < integralLen; i++) {
result += integral[i];
if ((i + 1) % 3 === integralLen % 3 && i !== integralLen - 1) {
result += ',';
}
}
if (fractionDigits === 0) {
return result;
}
result += '.';
const dotPos = numStr.indexOf('.');
for (let i = 0; i < fractionDigits; i++) {
result += numStr[dotPos + 1 + i];
}
return result;
}
function shuffleArray(arr = []) {
const sz = arr.length;
for (let i = 0; i < sz; i++) {
const newIndex = getRandom(0, sz - 1);
const temp = arr[i];
arr[i] = arr[newIndex];
arr[newIndex] = temp;
}
}
function getDataFromClass(className) {
const element = document.querySelector(`.${className}`);
let str = '';
for (let i = 0; ; i++) {
const s = getComputedStyle(element).getPropertyValue(`--attr${i}`);
if (!s) {
break;
}
str += s;
}
element.remove();
return JSON.parse(window.atob(str));
}
function getStreamNameFromWebSocketMessage(message = '') {
const msgLen = message.length;
const token = 'stream":';
let current = -1;
let index;
for (index = 0; index < msgLen && current < 7; index++) {
if (message[index] === token[current + 1]) {
current++;
} else {
current = message[index] === token[0] ? 0 : -1;
}
}
if (current < 7) {
return 'unhandled_stream';
}
let stream = '';
for (let i = index + 1; i < msgLen && message[i] !== '"'; i++) {
stream += message[i];
}
return stream;
}
// LOGIC
function updateVisualizationInterval() {
visualizationUpdateInterval = animationCells === 0
? MIN_VISUALIZATION_UPDATE_INTERVAL
: MAX_VISUALIZATION_UPDATE_INTERVAL;
}
function getCellAnimationScale(frame, isDestroyed, distance) {
if (!isDestroyed) {
return 1;
}
return destroyedCellAnimationScale[distance][frame];
}
function makeCellAnimation(
row,
col,
{
distance = DISTANCE_OF_DESTRUCTION + 1,
triggeredByCode = false,
colorOpacity = 1,
color = [255, 255, 255],
isReset = false,
},
) {
if (row < 0 || row >= rows) {
return;
}
if (col < 0 || col >= cols) {
return;
}
if (!isCellExisted[row][col] && !triggeredByCode) {
return;
}
isReset &&= !isCellExisted[row][col];
const isDestroyed = isCellExisted[row][col]
&& !triggeredByCode && distance <= DISTANCE_OF_DESTRUCTION;
const cell = cells[row][col];
const deltaOpacity = colorOpacity / cellAnimationFrames;
const delay = 1000 / CELL_TOUCH_ANIMATION_FPS;
let count = 0;
function updateStyle(backgroundColorOpacity, scale) {
cell.style.backgroundColor = `rgba(${color[0]},${color[1]},${color[2]},${backgroundColorOpacity})`;
if (isDestroyed) {
cell.style.transform = count === 0 || count === cellAnimationFrames ? '' : `scale(${scale})`;
}
}
if (intervalIds[row][col] === null) {
animationCells++;
updateVisualizationInterval();
} else {
clearInterval(intervalIds[row][col]);
intervalIds[row][col] = null;
cell.style.transform = '';
}
// first frame
updateStyle(colorOpacity, 1);
// the rest of the frames
intervalIds[row][col] = setInterval(() => {
count++;
const opacity = colorOpacity - deltaOpacity * count;
updateStyle(opacity, getCellAnimationScale(count, isDestroyed, distance));
if (count === cellAnimationFrames) {
animationCells--;
updateVisualizationInterval();
clearInterval(intervalIds[row][col]);
intervalIds[row][col] = null;
}
}, delay);
if (isReset) {
cell.style.zIndex = '3';
isCellExisted[row][col] = true;
} else if (isDestroyed) {
cell.style.zIndex = '0';
isCellExisted[row][col] = false;
}
}
function createDot(dotSize, top, left, imgSrc, linkElement) {
const dot = document.createElement('div');
dot.style.height = `${dotSize}px`;
dot.style.width = `${dotSize}px`;
dot.style.borderRadius = `${dotSize}px`;
dot.style.top = `${top}px`;
dot.style.left = `${left}px`;
dot.style.opacity = '0';
dot.classList.add('randy');
if (!imgSrc) {
return dot;
}
dot.style.display = 'flex';
dot.style.justifyContent = 'center';
dot.style.alignItems = 'center';
const img = document.createElement('img');
img.src = imgSrc;
img.style.width = '65%';
img.style.height = '65%';
dot.append(img);
addUniversalSensitiveClickListener(dot, () => linkElement.click());
return dot;
}
function startSignatureNeonEffect() {
const signatureContainer = document.getElementById('signature-container');
window.cid += 'tfolio';
function makeEffect() {
let delay = 0;
function makeFlickeringEffect(delays) {
// delays must have an odd length
// first flickering
signatureContainer.classList.remove('neon-effect');
// the rest
delays.forEach((time, index) => {
delay += time;
setTimeout(
() => signatureContainer.classList[index % 2 === 0 ? 'add' : 'remove'](
'neon-effect',
),
delay,
);
});
}
makeFlickeringEffect([
137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
137, 137, 137, 137, 137, 137, 1409, 181, 1409, 181, 1801,
]);
}
makeEffect();
}
function topCellsEffect() {
const colorsLength = TOP_CELLS_EFFECT_COLORS.length;
const isMobile = isMobileDevice();
const propagationDelay = PROPAGATION_DELAY * 0.6 * (isMobile ? 1.5 : 1);
const commonOptions = {
triggeredByCode: true,
colorOpacity: isMobile ? 0.9 : 0.7,
};
makeCellAnimation(0, Math.floor(cols / 2), {
color: TOP_CELLS_EFFECT_COLORS[getRandom(0, colorsLength - 1)],
...commonOptions,
});
for (let distance = 1; distance <= Math.floor(cols / 2); distance++) {
const delay = distance * propagationDelay;
setTimeout(() => {
makeCellAnimation(0, Math.floor(cols / 2) - distance, {
color: TOP_CELLS_EFFECT_COLORS[getRandom(0, colorsLength - 1)],
...commonOptions,
});
makeCellAnimation(0, Math.floor(cols / 2) + distance, {
color: TOP_CELLS_EFFECT_COLORS[getRandom(0, colorsLength - 1)],
...commonOptions,
});
}, delay);
}
}
function startDotGame() {
let currentIndex = -1;
setInterval(() => {
// skip generating when the tab is out of focus
if (!document.hasFocus()) {
return;
}
// generate dot
const appWidth = AppContainer.offsetWidth;
const appHeight = AppContainer.offsetHeight;
const isMobile = isMobileDevice();
const minDotSize = isMobile ? MIN_DOT_SIZE_MOBILE : MIN_DOT_SIZE;
const maxDotSize = isMobile ? MAX_DOT_SIZE_MOBILE : MAX_DOT_SIZE;
const dotSize = (getRandom(minDotSize, maxDotSize) / 100) * appWidth;
const top = getRandom(appHeight / rows, appHeight / 8);
const left = getRandom(0, appWidth - dotSize);
currentIndex++;
const hasLink = currentIndex % 2 === 0;
const dot = createDot(
dotSize,
top,
left,
hasLink && dotGameLinks[(currentIndex / 2) % dotGameLinks.length].imgSrc,
hasLink && dotGameLinks[(currentIndex / 2) % dotGameLinks.length].linkElement,
);
addUniversalSensitiveClickListener(dot, () => {
dot.remove();
if (!hasLink) {
topCellsEffect();
}
});
AppContainer.append(dot);
makeAnimation(dot, 'dot');
dot.style.opacity = '1';
dot.style.transform = `translateY(${appHeight - top}px)`;
setTimeout(() => {
dot.style.opacity = '0';
setTimeout(() => dot.remove(), DOT_ANIMATION_LENGTH);
}, MAX_DOT_EXISTING_TIME);
}, TIME_BETWEEN_DOT_GENERATE);
}
function onCellTouch(
row,
col,
triggeredByCode = false,
colorOpacity = 0.4,
color = [255, 255, 255],
isReset = false,
) {
const options = {
triggeredByCode,
colorOpacity,
color,
isReset,
};
makeCellAnimation(row, col, { distance: 0, ...options });
// wave effect, similar to how bfs works
const maxDistance = rows + cols;
for (let distance = 1; distance <= maxDistance; distance++) {
const delay = PROPAGATION_DELAY * distance;
const commonOptions = { distance, ...options };
setTimeout(() => {
// create the ring
for (let k = 0; k < distance; k++) {
makeCellAnimation(row - distance + k, col + k, commonOptions);
makeCellAnimation(row + k, col + distance - k, commonOptions);
makeCellAnimation(row + distance - k, col - k, commonOptions);
makeCellAnimation(row - k, col - distance + k, commonOptions);
}
}, delay);
}
}
const getResourceBlobURL = (url) => new Promise((resolve) => {
const ajaxRequest = new XMLHttpRequest();
ajaxRequest.open('GET', url, true);
ajaxRequest.responseType = 'blob';
ajaxRequest.onload = () => {
const blobData = ajaxRequest.response;
const blobUrl = URL.createObjectURL(blobData);
resolve(blobUrl);
};
ajaxRequest.send();
});
async function setUpStaticResources() {
const widgetIcons = ['bitcoin', 'clock', 'ethereum', 'weather'];
const promises = [];
[
{ imgSrc: 'neon-background', type: 'webm' },
{ imgSrc: 'deco', type: 'png' },
...widgetIcons.map((imgSrc) => ({ imgSrc, type: 'png' })),
...dotGameLinks.map((item) => {
item.type = 'png';
return item;
}),
].forEach(async (item) => {
const promise = getResourceBlobURL(`./images/${item.imgSrc}.${item.type}`);
promises.push(promise);
const blobUrl = await promise;
const element = document.querySelector(`.for-preloading-purpose-${item.imgSrc}`) || {};
element.src = blobUrl;
if (item.imgSrc === 'deco') {
decoPatternUrl = blobUrl;
}
if (item.imgSrc === 'neon-background') {
const videoElement = document.getElementById('background-image');
videoElement.src = blobUrl;
}
if (widgetIcons.find((widgetIcon) => widgetIcon === item.imgSrc)) {
const widgetIconElement = document.querySelector(`.${item.imgSrc}-icon`);
widgetIconElement.src = blobUrl;
}
item.imgSrc = blobUrl;
});
return Promise.all(promises);
}
async function loadTrack(index) {
const { path, name, artist } = tracks[index];
const audioUrl = await getResourceBlobURL(path);
const nextTrackIndex = (currentTrackIndex + 1) % tracks.length;
if (index !== nextTrackIndex || trackData !== null) {
return;
}
trackData = {
path,
name,
artist,
audioUrl,
};
}
async function loadFirstTrack() {
const data = getDataFromClass('tracks-info');
const { url } = data;
tracks = data.tracks;
tracks.forEach((track) => { track.path = url + track.path; });
shuffleArray(tracks);
const demoMode = document.location.href.indexOf('demo=true') !== -1;
if (demoMode) {
const trackIndex = tracks.findIndex(({ name }) => name === 'Feel Good');
const tmp = tracks[0];
tracks[0] = tracks[trackIndex];
tracks[trackIndex] = tmp;
}
await loadTrack(0);
currentTrackIndex = 0;
}
function setVolume(value) {
gainNode.gain.value = value;
}
function prepareMusic() {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
audioSource = audioCtx.createMediaElementSource(audioOutput);
gainNode = audioCtx.createGain();
audioAnalyser = audioCtx.createAnalyser();
audioAnalyser.minDecibels = -90;
audioAnalyser.maxDecibels = -10;
audioAnalyser.smoothingTimeConstant = 0.88;
audioAnalyser.fftSize = 64;
audioSource.connect(gainNode);
gainNode.connect(audioAnalyser);
audioAnalyser.connect(audioCtx.destination);
audioOutput.src = trackData.audioUrl;
setVolume(0);
audioOutput.playbackRate = 0.1;
audioOutput.play();
if ('mediaSession' in navigator) {
navigator.mediaSession.metadata = new MediaMetadata({
title: 'Randy\'s music radio',
artist: 'Tap to go to the radio',
album: 'Tap to go to the radio',
artwork: [
{ src: './images/vinyl.png', sizes: '200x200', type: 'image/png' },
],
});
}
function reloadOnVisiblePause() {
if (audioOutput.src?.indexOf('blob') !== -1 && audioOutput.paused) {
window.location.reload();
}
}
document.onvisibilitychange = () => {
if (document.visibilityState === 'visible') {
reloadOnVisiblePause();
}
};
window.addEventListener('focus', reloadOnVisiblePause);
}
function prepareTrack(index) {
// avoid heavy load
setTimeout(() => loadTrack(index), 2003);
return setInterval(() => {
if (trackData !== null) {
return;
}
loadTrack(index);
}, 30000);
}
function clearOldSpans(trackInfoContainer) {
while (trackInfoContainer.lastChild) {
trackInfoContainer.removeChild(trackInfoContainer.lastChild);
}
}
function addSpanToTrackInfo(content, fontSize, trackInfoContainer) {
const span = document.createElement('span');
span.className = 'span-for-track-info nguyen';
const textSpan = document.createElement('span');
textSpan.textContent = content;
span.append(textSpan);
textSpan.style.fontSize = `${fontSize}px`;
trackInfoContainer.append(span);
// deco pattern
const decoPattern = document.createElement('img');
const padding = span.offsetHeight / 8;
decoPattern.src = decoPatternUrl;
decoPattern.style.height = `${span.offsetHeight}px`;
decoPattern.style.marginLeft = `${padding}px`;
decoPattern.style.marginRight = `${padding}px`;
decoPattern.style.transform = 'scale(0.9)';
span.append(decoPattern);
return span;
}
function createTrackInfo({ name, artist }) {
const trackInfoContainer = document.getElementById('track-info');
const trackInfoWidth = trackInfoContainer.offsetWidth;
const trackInfoHeight = trackInfoContainer.offsetHeight;
const fontSize = 0.75 * trackInfoHeight;
const content = `${name} - ${artist}`;
const dummySpan = addSpanToTrackInfo(content, fontSize, trackInfoContainer);
const spanWidth = dummySpan.offsetWidth;
const spanHeight = dummySpan.offsetHeight;
const spanTransitionTime = (TRACK_INFO_TRAVEL_TIME
* (trackInfoWidth + spanWidth - 2 * trackInfoHeight))
/ trackInfoWidth;
const timeBetWeenSpan = (TRACK_INFO_TRAVEL_TIME * spanWidth) / trackInfoWidth;
function addNewSpan() {
const span = addSpanToTrackInfo(content, fontSize, trackInfoContainer);
// optimization
span.style.willChange = 'transform';
setTimeout(() => {
if (span?.style?.willChange) {
span.style.willChange = 'auto';
}
}, spanTransitionTime);
span.style.top = `${(trackInfoHeight - spanHeight) / 2}px`;
span.style.left = `${trackInfoWidth - 2 * trackInfoHeight}px`;
span.style.transition = `transform ${spanTransitionTime}ms linear, opacity 1s`;
makeAnimation(span, 'to-make-a-transition-for-left-property-randy');
span.style.transform = `translateX(${-(
trackInfoWidth
- 2 * trackInfoHeight
+ spanWidth
)}px)`;
}
let intervalID;
function startTrackInfo() {
clearInterval(intervalID);
clearOldSpans(trackInfoContainer);
// skip when the tab is out of focus
if (!document.hasFocus()) {
return;
}
// the first span
addNewSpan();
// the rest
intervalID = setInterval(() => {
addNewSpan();
}, timeBetWeenSpan);
}
startTrackInfo();
dummySpan.remove();
function stopTrackInfo(endOfTrack = false) {
clearInterval(intervalID);
// remove old spans
setTimeout(() => clearOldSpans(trackInfoContainer), endOfTrack ? 1000 : 0);
if (!endOfTrack) {
return;
}
// remove listeners
window.onfocus = undefined;
window.onblur = undefined;
// faded animation for old spans
const trackInfoSpans = document.getElementsByClassName(
'span-for-track-info',
);
for (let i = 0; i < trackInfoSpans.length; i++) {
const span = trackInfoSpans[i];
makeAnimation(span, 'to-make-a-transition-for-opacity-property-nguyen');
span.style.opacity = '0';
}
}
// restart track info when the tab is focused
window.onfocus = () => {
startTrackInfo();
};
// stop track info when the tab is out of focus
window.onblur = () => {
stopTrackInfo();
};
return stopTrackInfo;
}
function startMusicVisualization(analyser) {
const bufferLength = analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);
const bassCircle = document.getElementById('bass-effect');
const trebleCircle = document.getElementById('treble-effect');
const diskSize = document.getElementById('disk').offsetWidth;
const updateVisualization = () => {
analyser.getByteFrequencyData(dataArray);
for (let i = 0; i < visualizationBars.length; i++) {
const bar = visualizationBars[i];
const newWidth = (dataArray[i] / 255) * 3 * diskSize;
bar.style.transform = `scaleX(${newWidth / bar._width_})`;
}
const dBBass = dataArray[0];
const dBTreble = dataArray[6];
bassCircle.style.transform = `scaleX(${Math.min(1, Math.max(0, (dBBass - 90) / 140)) * 1.25})`;
trebleCircle.style.transform = `scaleY(${Math.min(1, Math.max(0, (dBTreble - 40) / 100)) * 1.25})`;
};
let visualizationLoop = () => {
updateVisualization();
setTimeout(() => visualizationLoop?.(), visualizationUpdateInterval);
};
visualizationLoop();
return () => {
// reset circles and bars
bassCircle.style.transform = 'scale(1)';
trebleCircle.style.transform = 'scale(1)';
for (let i = 0; i < visualizationBars.length; i++) {
const bar = visualizationBars[i];
bar.style.transform = 'scaleX(0)';
}
// stop visualization
visualizationLoop = undefined;
};
}
async function startMusic(track) {
if (track === null) {
return;
}
trackData = null;
// animation at the beginning of the song
onCellTouch(Math.floor(rows / 2), Math.floor(cols / 2), true);
audioOutput.currentTime = 0;
function upTheVolumeAtBeginOfTheTrack() {
if (audioOutput.currentTime <= 0.1) {
setVolume(1);
} else {
setTimeout(upTheVolumeAtBeginOfTheTrack);
}
}
upTheVolumeAtBeginOfTheTrack();
const intervalId = prepareTrack((currentTrackIndex + 1) % tracks.length);
const stopVisualizationHandler = startMusicVisualization(audioAnalyser);
// delay to avoid heavy load
await sleep(1213);
const stopTrackInfoHandler = createTrackInfo(track);
audioOutput.onended = async () => {
if (trackData === null) {
trackData = track;
} else {
// delay to avoid heavy load
setTimeout(() => URL.revokeObjectURL(track.audioUrl), 20000);
}
const nextTrack = trackData;
audioOutput.src = nextTrack.audioUrl;
setVolume(0);
audioOutput.play();
currentTrackIndex = (currentTrackIndex + 1) % tracks.length;
clearInterval(intervalId);
stopVisualizationHandler();
stopTrackInfoHandler(true);
await sleep(1000);
// play next track
startMusic(nextTrack);
};
}
function prepareCellAnimation() {
cellAnimationFrames = CELL_TOUCH_ANIMATION_FPS * CELL_TOUCH_ANIMATION_LENGTH / 1000;
const deltaScale = 1 / cellAnimationFrames;
const originalScale = 0.98; // to avoid overlap between 2 cells
for (let frame = 0; frame <= cellAnimationFrames; frame++) {
for (let distance = 0; distance <= DISTANCE_OF_DESTRUCTION; distance++) {
const x = frame + (DISTANCE_OF_DESTRUCTION - distance) * 1.5;
const scale = Math.max(originalScale - deltaScale * x ** 2 / cellAnimationFrames * 0.5, 0);
destroyedCellAnimationScale[distance][frame] = scale;
}
}
}
function createGridSystem(appWidth, appHeight) {
prepareCellAnimation();
cols = isMobileDevice() ? CELLS_PER_ROW_MOBILE : CELLS_PER_ROW;
const cellSize = appWidth / cols;
rows = roundToOddNumber(appHeight / cellSize);
window.cid = 'rand';
AppContainer.style.gridTemplateColumns = createFrString(cols);
AppContainer.style.gridTemplateRows = createFrString(rows);
for (let i = 0; i < rows; i++) {
const cellsInRow = [];
for (let j = 0; j < cols; j++) {
const cell = document.createElement('div');
cell.className = 'cell';
addUniversalSensitiveClickListener(cell, () => onCellTouch(i, j));
AppContainer.append(cell);
cellsInRow.push(cell);
}
cells.push(cellsInRow);
isCellExisted.push(Array(cols).fill(true));
intervalIds.push(Array(cols).fill(null));
}
}
function resetGrid() {
onCellTouch(Math.floor(rows / 2), Math.floor(cols / 2), true, 0.5, [255, 0, 255], true);
}
function showMainPanel(mainContainerSizeAfterScaling) {
// styling for the main panel according to the screen size
const mainContainer = document.getElementById('main');
const mainContainerSize = mainContainer.offsetWidth;
mainContainer.style.transform = `scale(${mainContainerSizeAfterScaling / mainContainerSize})`;
// show the main panel
mainContainer.style.opacity = '1';
window.cid += 'y-nguye';
// show signature and apply animations for the main panel
const signature = document.getElementById('signature');
signature.style.strokeDashoffset = '0';
const signatureContainer = document.getElementById('signature-container');
signatureContainer.style.transform = `scale(${isMobileDevice() ? 3.5 : 5.5})`;
setTimeout(() => {
signatureContainer.style.transition = 'transform 3s ease-in-out';
signatureContainer.style.transform = 'scale(1)';
}, 5000);
const diskContainer = document.getElementById('disk');
setTimeout(() => addUniversalSensitiveClickListener(diskContainer, resetGrid), 8000);
diskContainer.classList.add('disk-animation');
// make mask fade after signature's animation is done
const mask = document.getElementById('mask');
mask.classList.add('faded-effect');
// remove mask after 1.5s from starting time, to avoid heavy load
setTimeout(() => mask.remove(), 9500);
// styling for bars
for (let i = 0; i < visualizationBars.length; i++) {
const bar = visualizationBars[i];
const width = mainContainerSize * (2 - i / (visualizationBars.length - 1));
bar._width_ = width;
bar.style.width = `${width}px`;
bar.style.transform = 'scaleX(0)';
}
}
function showMiniLogo() {
const translateAnimationLength = 0.08;
const hobbiesAnimationLength = 2 + translateAnimationLength;
// animation for hobbies
const hobbiesDiv = document.querySelectorAll('#mini-logo .hobbies div');
for (let i = 0; i < hobbiesDiv.length; i++) {
hobbiesDiv[i].style.animation = `hobbiesAnimation ${(hobbiesAnimationLength - translateAnimationLength) * hobbiesDiv.length}s ${(hobbiesAnimationLength - translateAnimationLength) * i}s linear infinite`;
}
// show mini logo
const miniLogoContainer = document.getElementById('mini-logo');
miniLogoContainer.style.transition = 'opacity 1s linear';
miniLogoContainer.style.opacity = '1';
}
function prepareTrackInfoLayout(
appWidth,
appHeight,
mainContainerSizeAfterScaling,
) {
// track info layout
const trackInfoContainer = document.getElementById('track-info');
const trackInfoHeight = mainContainerSizeAfterScaling * TRACK_INFO_HEIGHT;
trackInfoContainer.style.height = `${trackInfoHeight}px`;
const trackInfoWidth = Math.sqrt(square(appHeight) + square(appWidth)) + 4 * trackInfoHeight;
trackInfoContainer.style.width = `${trackInfoWidth}px`;
trackInfoContainer.style.left = `calc((100vw - ${trackInfoWidth}px) / 2)`;
window.cid += 'n_por';
// rotate track info according to the screen size
const alpha = Math.atan((appHeight - trackInfoHeight) / appWidth);
trackInfoContainer.style.transform = `rotate(${-alpha}rad)`;
}
function resetWidgetsLayout() {
const elements = document.querySelectorAll('#widgets .content div *');
const sz = elements.length;
for (let i = 0; i < sz; i++) {
const element = elements[i];
const isContainer = element.querySelectorAll('*').length > 0;
if (!isContainer) {
const text = element.textContent;
element.textContent = '';
element.textContent = text;
}
}
}
function showWidgets() {
const widgetsContainer = document.getElementById('widgets');
const widgetsIcon = document.querySelectorAll('#widgets img');
const widgetContents = document.querySelectorAll(
'#widgets .wrapper .content',
);
function makeWidgetsAnimation(willExpand) {
widgetsExpanded = willExpand;
// animation for widget icons
for (let i = 0; i < widgetsIcon.length; i++) {
const widgetIcon = widgetsIcon[i];
const addedClassName = willExpand
? 'widget-icon-expand'
: 'widget-icon-collapse';
const removedClassName = willExpand
? 'widget-icon-collapse'
: 'widget-icon-expand';
widgetIcon.classList.remove(removedClassName);
makeAnimation(widgetIcon, addedClassName);
}
// animation for widget containers
for (let i = 0; i < widgetContents.length; i++) {
const widgetContent = widgetContents[i];
const addedClassName = willExpand
? 'widget-content-expand'
: 'widget-content-collapse';
const removedClassName = willExpand
? 'widget-content-collapse'
: 'widget-content-expand';
widgetContent.classList.remove(removedClassName);
makeAnimation(widgetContent, addedClassName);
}
// animation for widgets container
const addedClassName = willExpand ? 'widgets-expand' : 'widgets-collapse';
const removedClassName = willExpand ? 'widgets-collapse' : 'widgets-expand';
widgetsContainer.classList.remove(removedClassName);
makeAnimation(widgetsContainer, addedClassName);
}
addUniversalSensitiveClickListener(widgetsContainer, () => {
makeWidgetsAnimation(!widgetsExpanded);
});
// force re-layout to avoid layout misalignments
resetWidgetsLayout();
// show widgets
widgetsContainer.style.transition = 'opacity 1s linear';
widgetsContainer.style.opacity = '1';
}
function killWSConnection() {
wsInstance?.close();
wsInstance = null;
}