-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
101 lines (86 loc) Β· 2.91 KB
/
app.js
File metadata and controls
101 lines (86 loc) Β· 2.91 KB
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
const grid = document.querySelector('.container');
const changeSize = document.querySelector('.changeSize');
function createGrid(size) {
grid.innerHTML = '';
const containerWidth = grid.clientWidth;
const gridSize = containerWidth / size;
grid.style.opacity = '0';
for (let i = 0; i < size * size; i++) {
const gridItem = document.createElement('div');
gridItem.classList.add('grid-item');
gridItem.style.width = `${gridSize}px`;
gridItem.style.height = `${gridSize}px`;
grid.appendChild(gridItem);
}
setTimeout(() => {
grid.style.opacity = '1';
}, 100);
}
createGrid(16);
changeSize.addEventListener('input', () => {
const newSize = changeSize.value;
createGrid(newSize);
});
// Add window resize handler
window.addEventListener('resize', () => {
const currentSize = changeSize.value;
createGrid(currentSize);
});
grid.addEventListener('mouseover', (e) => {
e.target.style.backgroundColor = '#6366f1';
});
const restButton = document.querySelector('.reset');
restButton.addEventListener('click', () => {
const gridItems = document.querySelectorAll('.grid-item');
gridItems.forEach((item) => {
item.style.backgroundColor = getComputedStyle(
document.documentElement,
).getPropertyValue('--background-color');
});
});
function getRandomColor() {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return `rgb(${r}, ${g}, ${b})`;
}
const random = document.querySelector('.random');
random.addEventListener('click', () => {
grid.addEventListener('mouseover', (e) => {
e.target.style.backgroundColor = getRandomColor();
});
});
const eraser = document.querySelector('.eraser');
eraser.addEventListener('click', () => {
grid.addEventListener('mouseover', (e) => {
e.target.style.backgroundColor = getComputedStyle(
document.documentElement,
).getPropertyValue('--background-color');
});
});
const themeToggle = document.querySelector('.theme-toggle');
const html = document.documentElement;
themeToggle.addEventListener('click', () => {
const currentTheme = html.getAttribute('data-theme');
const newTheme = currentTheme === 'light' ? 'dark' : 'light';
html.setAttribute('data-theme', newTheme);
themeToggle.innerHTML =
newTheme === 'light'
? '<i class="bi bi-moon-fill"></i>'
: '<i class="bi bi-sun-fill"></i>';
});
const colorPicker = document.querySelector('.color-picker');
colorPicker.addEventListener('input', () => {
const selectedColor = colorPicker.value;
grid.addEventListener('mouseover', (e) => {
e.target.style.backgroundColor = selectedColor;
});
});
// Add active state to buttons
const buttons = document.querySelectorAll('button:not(.theme-toggle)');
buttons.forEach((button) => {
button.addEventListener('click', () => {
buttons.forEach((btn) => btn.classList.remove('active'));
button.classList.add('active');
});
});