-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
195 lines (165 loc) Β· 5.26 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
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
//DOM hooks
const timeBtn = document.getElementById('timeBtn');
const chaosBtn = document.getElementById('chaosBtn');
const stopBtn = document.getElementById('stopBtn');
const cleanBtn = document.getElementById('cleanBtn');
const displayBtn = document.getElementById('displayBtn');
const colorPicker = document.getElementById('colorPicker');
//state variables
let timeHasBeenDisplaying = false;
let intervalId = 0;
let displayMode = displayBtn.textContent;
function display() {
cleanUp();
if (displayMode === 'Clock') {
displayTime();
}
if (displayMode === 'Chaos') {
displayChaos();
}
}
function displayTime() {
let timeId = setInterval(() => {
startTime();
}, 500);
intervalId = timeId;
}
function displayChaos() {
let chaosId = setInterval(() => {
startChaos();
}, 900);
intervalId = chaosId;
}
function stopDisplay() {
clearInterval(intervalId);
}
function startTime() {
document.querySelectorAll('.dots > rect').forEach((el) => el.classList.add('active'));
timeHasBeenDisplaying = true;
function checkTime(i) {
return i < 10 ? '0' + i : i;
}
let time = new Date(),
h = checkTime(time.getHours()),
m = checkTime(time.getMinutes()),
s = checkTime(time.getSeconds());
document.querySelector('svg').setAttribute('data-hour', h);
document.querySelector('svg').setAttribute('data-minute', m);
document.querySelector('svg').setAttribute('data-second', s);
}
function startChaos() {
cleanUp();
//there are 42 elements (not including dots)
const numOfElems = Math.round(Math.random() * 42);
let arr = [];
//populate an array with randomly generated digit - element pairs X(times) up to 42
for (let i = 0; i < numOfElems; i++) {
//(1-6)
const randomDig = Math.floor(Math.random() * 6) + 1;
//(0-6)
const randomEl = Math.round(Math.random() * 6);
//rBundle - random bundle that will be an array of 2 elements, digit-number, and element number
let rBundle = [`.digit-${randomDig}`, randomEl];
arr.push(rBundle);
}
//add active class to each digit-elem pair in the array
arr.forEach((index) => {
document.querySelector(index[0]).children[index[1]].classList.add('active');
});
//clean up the array
arr = [];
//implementing random dots
//flexing objects
const randomDots = {
dotsActive: Math.round(Math.random()),
numberOfDots: Math.round(Math.random() * 3) + 1,
};
//truthy-falsy 0 || 1
if (randomDots.dotsActive) {
for (let i = 0; i < randomDots.numberOfDots; i++) {
let randomIndex = Math.round(Math.random() * 3);
document.querySelector('.dots').children[randomIndex].classList.add('active');
}
}
}
//clean up function runs everytime button is pressed, removes "active" class from all elements, and removes data- attributes to clearn up the time
function cleanUp() {
document
.querySelector('svg')
.querySelectorAll('*')
.forEach((el) => {
el.classList.remove('active');
});
document.querySelector('svg').setAttribute('data-hour', '');
document.querySelector('svg').setAttribute('data-minute', '');
document.querySelector('svg').setAttribute('data-second', '');
}
// EVENT LISTENERS
// display btn
displayBtn.addEventListener('click', () => {
cleanUp();
stopDisplay();
display();
if (displayMode === 'Clock') {
displayBtn.textContent = 'Chaos';
displayMode = displayBtn.textContent;
} else {
displayBtn.textContent = 'Clock';
displayMode = displayBtn.textContent;
}
});
// stop btn
stopBtn.addEventListener('click', () => {
stopDisplay();
// cleanUp();
});
// Hoover text change
// displayBtn.addEventListener('mouseover', () => {
// if (timeHasBeenDisplaying) {
// displayBtn.style.animationDuration = '0.1s';
// displayBtn.textContent === 'Clock' ? (displayBtn.textContent = 'Chaos') : (displayBtn.textContent = 'Clock');
// }
// });
// displayBtn.addEventListener('mouseleave', () => {
// if (timeHasBeenDisplaying) {
// displayBtn.style.animationDuration = '3.11s';
// displayBtn.textContent === 'Chaos' ? (displayBtn.textContent = 'Clock') : (displayBtn.textContent = 'Chaos');
// }
// });
// color picker
colorPicker.addEventListener('input', () => {
document.documentElement.style.setProperty('--chaos', event.target.value);
});
//secret setting to set the clock to the selected color
colorPicker.addEventListener('dblclick', () => {
document.documentElement.style.setProperty('--order-clock', event.target.value);
});
//old event listeners
/*
timeBtn.addEventListener('click', () => {
//apply highlighted effect if the button is pressed
timeBtn.classList.toggle('btn-active');
// cleanUp();
if (!timeIsDisplaying) {
console.log('Creating Time...');
displayTime();
timeIsDisplaying = true;
} else {
console.log('Stopping Time...');
stopDisplay();
timeIsDisplaying = false;
}
});
// CHAOS BTN
chaosBtn.addEventListener('click', () => {
console.log('Creating Chaos...');
// startChaos();
displayChaos();
// setInterval(() => startChaos(), 1000);
});
// Clean BTN
cleanBtn.addEventListener('click', () => {
console.log('Cleaning...');
cleanUp();
});
*/