-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (111 loc) · 3.35 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
import './style.css';
// State variables
let currentColor = generateRandomColor();
let currentMode = 'color';
let currentSize = 16;
let isDown = false;
// DOM elements
const container = document.querySelector('#container');
const colorPicker = document.querySelector('#color-picker');
const gridRange = document.querySelector('#grid-range');
const gridRangeOutput = document.querySelector('#grid-range-output');
const clearBtn = document.querySelector('#clear');
const modeButtons = getModeButtons();
function getModeButtons() {
const allButtons = document.querySelectorAll('button');
return [...allButtons].filter((button) => button.innerHTML !== 'Clear');
}
function debounce(func, delay = 0) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), delay);
};
}
function initialize() {
setupEventListeners();
loadInitialValues();
}
function setupEventListeners() {
container.onmousedown = () => (isDown = true);
container.onmouseup = () => (isDown = false);
colorPicker.addEventListener('input', handleColorPicker);
gridRange.addEventListener(
'input',
debounce((e) => updateSize(e.target.value))
);
clearBtn.addEventListener('click', clearGrid);
modeButtons.forEach((button) => {
button.addEventListener('click', function () {
updateMode(this.innerHTML.toLowerCase());
});
});
}
function handleColorPicker(e) {
updateMode('color');
updateColor(e.target.value);
}
function updateColor(newColor) {
currentColor = newColor;
}
function updateMode(newMode) {
deactivateAllButtons();
currentMode = newMode;
activateButton(document.querySelector(`#${newMode}`));
}
function updateSize(newSize) {
gridRangeOutput.textContent = `${newSize} x ${newSize}`;
adjustGridSize(newSize);
currentSize = newSize;
}
function deactivateAllButtons() {
modeButtons.forEach((button) => button.classList.remove('active'));
}
function activateButton(button) {
button.classList.add('active');
}
function adjustGridSize(newSize) {
const requiredTotalCells = newSize * newSize;
// If newSize is greater, add more cells
while (container.children.length < requiredTotalCells) {
const cell = document.createElement('div');
cell.addEventListener('mouseover', sketch);
cell.addEventListener('mousedown', sketch);
container.append(cell);
}
// If newSize is smaller, remove extra cells
while (container.children.length > requiredTotalCells) {
container.lastChild.remove();
}
Array.from(container.children).forEach((cell) => {
cell.style.backgroundColor = '';
});
container.style.setProperty('--grid-rows', newSize);
container.style.setProperty('--grid-cols', newSize);
}
function clearGrid() {
container.innerHTML = '';
}
function sketch({ type, target }) {
if (type === 'mouseover' && !isDown) return;
const modes = {
color: currentColor,
rainbow: generateRandomColor(),
eraser: '',
};
target.style.backgroundColor = modes[currentMode];
}
function generateRandomColor() {
return `#${Math.floor(Math.random() * 16777215)
.toString(16)
.padStart(6, '0')
.toUpperCase()}`;
}
function loadInitialValues() {
colorPicker.value = currentColor;
gridRange.value = currentSize;
gridRangeOutput.innerHTML = `${currentSize} x ${currentSize}`;
adjustGridSize(currentSize);
}
// Load the application
initialize();