-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.js
101 lines (81 loc) · 3.14 KB
/
home.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
//Nose Squeak
document.addEventListener('DOMContentLoaded', function() {
const imageElement = document.getElementById('Image');
const audioElement = document.getElementById('Audio');
const originalImageWidth = imageElement.naturalWidth;
const originalImageHeight = imageElement.naturalHeight;
const originalCenterX = 771;
const originalCenterY = 360;
const radius = 23;
imageElement.addEventListener('click', (event) => {
const clickedX = event.offsetX;
const clickedY = event.offsetY;
const scaleFactorX = imageElement.width / originalImageWidth;
const scaleFactorY = imageElement.height / originalImageHeight;
const originalClickedX = clickedX / scaleFactorX;
const originalClickedY = clickedY / scaleFactorY;
const distance = Math.sqrt(
Math.pow(originalClickedX - originalCenterX, 2) + Math.pow(originalClickedY - originalCenterY, 2)
);
if (distance <= radius) {
audioElement.play();
}
});
});
//Quantum Moon
function moonPlacement() {
const moonDiv = document.querySelector('.moon');
const bodyWidth = document.body.clientWidth;
const bodyHeight = document.body.clientHeight;
const moonWidth = moonDiv.clientWidth;
const moonHeight = moonDiv.clientHeight;
const randomX = Math.random() * (bodyWidth - moonWidth);
const randomY = Math.random() * (bodyHeight - moonHeight);
moonDiv.style.left = `${randomX}px`;
moonDiv.style.top = `${randomY}px`;
}
function volumeDistance(audio, moonDiv) {
const moonRect = moonDiv.getBoundingClientRect();
const moonX = moonRect.left + moonRect.width / 2;
const moonY = moonRect.top + moonRect.height / 2;
const maxVolumeDistance = 300;
document.addEventListener('mousemove', (event) => {
const cursorX = event.clientX;
const cursorY = event.clientY;
const distance = Math.sqrt(Math.pow(cursorX - moonX, 2) + Math.pow(cursorY - moonY, 2));
const volume = 1 - Math.min(distance / maxVolumeDistance, 1);
audio.volume = Math.min(Math.max(volume, 0), 1);
});
}
function initializeMoon() {
const moonDiv = document.querySelector('.moon');
const audio = document.getElementById('Loop');
const showMoon = Math.random() < 1 / 6;
if (showMoon) {
moonDiv.style.display = 'block';
moonPlacement();
audio.loop = true;
audio.volume = 0;
audio.play();
volumeDistance(audio, moonDiv);
} else {
moonDiv.style.display = 'none';
audio.pause();
audio.currentTime = 0;
}
}
function handleVisibilityChange() {
if (document.visibilityState === 'visible') {
initializeMoon();
} else {
const moonDiv = document.querySelector('.moon');
const audio = document.getElementById('Loop');
moonDiv.style.display = 'none';
audio.pause();
audio.currentTime = 0;
}
}
window.onload = function() {
initializeMoon();
document.addEventListener('visibilitychange', handleVisibilityChange);
};