-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
131 lines (108 loc) · 3.52 KB
/
main.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
let res = 32;
let currentColor = '#000000';
let backgroundColor = '#FFFFFF';
let currentMode = "color";
const gridContainer = document.getElementById("gridContainer");
const cells = gridContainer.getElementsByClassName('grid-item')
const gridResSlider = document.getElementById("gridRes")
const clearBtn = document.getElementById('clearBtn');
const colorGrab = document.getElementById('colorGrab');
const eraserBtn = document.getElementById('eraserBtn');
const colorBtn = document.getElementById('colorModeBtn');
const rainbowBtn = document.getElementById('rainbowModeBtn');
// Set color Mode
colorBtn.onclick = () => setCurrentMode('color');
rainbowBtn.onclick = () => setCurrentMode('rainbow');
eraserBtn.onclick = () => setCurrentMode('eraser');
// Mode selectaaah
function setCurrentMode(newMode) {
activateButton(newMode)
currentMode = newMode
}
// Activate modes & classes
function activateButton(newMode) {
if (currentMode === 'rainbow') {
rainbowBtn.classList.remove('active')
} else if (currentMode === 'color') {
colorBtn.classList.remove('active')
} else if (currentMode === 'eraser') {
eraserBtn.classList.remove('active')
}
if (newMode === 'rainbow') {
rainbowBtn.classList.add('active')
} else if (newMode === 'color') {
colorBtn.classList.add('active')
currentColor = colorGrab.value;
} else if (newMode === 'eraser') {
eraserBtn.classList.add('active')
}
}
// Resolution slider function
gridResSlider.addEventListener("click", function() {
res = gridResSlider.value;
clearGrid(gridContainer);
makeRows(res, res);
});
// setup grid function
function makeRows(rows, cols) {
gridContainer.style.setProperty('--grid-rows', rows);
gridContainer.style.setProperty('--grid-cols', cols);
for (c = 0; c < (rows * cols); c++) {
let cell = document.createElement("div");
//cell.innerText = (c + 1);
gridContainer.appendChild(cell).className = "grid-item";
};
};
makeRows(res, res);
// Rainbow color generator
function getRandomRgb() {
var num = Math.round(0xffffff * Math.random());
var r = num >> 16;
var g = num >> 8 & 255;
var b = num & 255;
return 'rgb(' + r + ', ' + g + ', ' + b + ')';
}
// Paint function
function paintGrid(elem, color){
if(elem.buttons == 1){
if(elem.target.classList == 'grid-item'){
let square = elem.target;
square.style.backgroundColor = currentColor;
}
} else {
return;
}
}
gridContainer.addEventListener('mousedown', event =>{
paintGridEvent = paintGrid(event, currentColor,);
if(event.buttons == 1){
window.addEventListener('mouseover', (e) => {
if(currentMode == 'rainbow'){
currentColor = getRandomRgb();
paintGrid(e, currentColor);
} else if (currentMode == 'eraser') {
currentColor = backgroundColor
paintGrid(e, currentColor);
} else {
paintGrid(e, currentColor);
}
});
}
});
// Clear function
const clearGrid = function(parent) {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}
clearBtn.addEventListener('click', function() {
clearGrid(gridContainer);
makeRows(res, res);
});
// Color Grabber
function setCurrentColor(newColor) {
currentColor = newColor;
}
colorGrab.oninput = (e) => setCurrentColor(e.target.value);
colorGrab.oninput = (e) => setCurrentMode('color');
setCurrentMode('color');