forked from sk66641/Random-Disco-Light-Simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
139 lines (118 loc) · 5.41 KB
/
script.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
document.addEventListener('DOMContentLoaded', () => {
const submitButton = document.getElementById('submit');
const resetButton = document.getElementById('reset');
const timerDisplay = document.getElementById('timerDisplay');
let timerInterval;
function run() {
let brightness = document.getElementById("brightness").value;
let saturation = document.getElementById("saturation").value;
let speed = document.getElementById("speed").value;
// Apply brightness and saturation to disco lights
let discoLights = document.querySelectorAll(".disco-light");
discoLights.forEach(light => {
light.style.filter = `brightness(${brightness}%) saturate(${saturation}%)`;
});
// Apply speed of change to animation duration
let animationDuration = 3000 / speed; // Adjust this value based on user input
document.styleSheets[0].insertRule(`@keyframes fadeInOut { 0% { opacity: 0; } 50% { opacity: 1; } 100% { opacity: 0; } }`);
document.styleSheets[0].deleteRule(2); // Update the existing fadeInOut animation rule with the new duration
}
submitButton.addEventListener('click', run);
resetButton.addEventListener('click', () => {
window.location.reload();
clearInterval(timerInterval);
});
function startCountdown(duration) {
let timer = duration;
timerDisplay.style.display = 'block';
timerInterval = setInterval(() => {
let minutes = Math.floor(timer / 60);
let seconds = timer % 60;
timerDisplay.textContent = `${pad(minutes)}:${pad(seconds)}`;
if (--timer < 0) {
clearInterval(timerInterval);
stopSimulation();
timerDisplay.style.display = 'none';
}
}, 1000);
}
function pad(number) {
return number.toString().padStart(2, '0');
}
function stopSimulation() {
// Display a message to the user
const messageDiv = document.getElementById('message');
messageDiv.style.display = 'block';
// Wait for a few seconds before reloading the page
setTimeout(() => {
window.location.reload();
}, 3000); // Change this value to adjust the delay
}
function run() {
let countdownValue = document.getElementById('countdown').value;
let n = document.getElementById("color").value;
let set_time = document.getElementById("time").value;
let unit = document.getElementById("unit").value;
let view = document.getElementById("view").value;
let brightness = document.getElementById('brightness').value;
let saturation = document.getElementById('saturation').value;
if (countdownValue && countdownValue > 0 && Number(n) >= 0 && Number.isInteger(Number(n)) && n !== "" && unit !== "unit" && view !== "select") {
// Clear error message if everything is correct
document.getElementById("error").innerHTML = "";
// Adjust brightness
let adjustedBrightness = brightness / 100;
let adjustedSaturation = saturation / 100;
document.body.style.filter = `brightness(${adjustedBrightness}) saturate(${adjustedSaturation})`;
// Start the simulation
startSimulation(n, set_time, unit, view);
// Start the countdown timer
startCountdown(countdownValue);
} else {
// Display error message if any input is missing or invalid
document.getElementById("error").innerHTML = "<strong>Please fill out all required fields correctly!</strong>";
document.getElementById("error").style.color = "red";
return;
}
}
function startSimulation(n, set_time, unit, view) {
// Move simulation code here
alert("Double click on the screen to reload!");
document.body.children[0].style.display = 'none';
document.body.style.cursor = "pointer";
document.body.addEventListener("dblclick", () => {
let cnf1 = confirm("Are you sure you want to reload?");
if (cnf1) {
window.location.reload();
}
});
if (unit === "seconds") {
set_time *= 1000;
}
function getRandomColor() {
let val1 = parseInt(0 + Math.random() * 256);
let val2 = parseInt(0 + Math.random() * 256);
let val3 = parseInt(0 + Math.random() * 256);
return `rgb(${val1}, ${val2}, ${val3})`;
}
function numberColors(num) {
let colors = `${getRandomColor()}`;
while (num > 1) {
colors += `, ${getRandomColor()}`;
num--;
}
return colors;
}
if (n == 1) {
document.body.style.backgroundColor = getRandomColor();
setInterval(() => {
document.body.style.backgroundColor = getRandomColor();
}, set_time);
} else {
let gradientType = view === "conic" ? "conic-gradient" : view === "linear" ? "linear-gradient" : "radial-gradient";
document.body.style.background = `${gradientType}(${numberColors(n - 1)}, ${getRandomColor()})`;
setInterval(() => {
document.body.style.background = `${gradientType}(${numberColors(n - 1)}, ${getRandomColor()})`;
}, set_time);
}
}
});