-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
129 lines (109 loc) · 3.4 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
let size = 50;
let draw = true;
let mode = 'Hover';
let brush = 'default';
let hslCounter = 0;
const grid = document.querySelector(".grid");
const sizeInput = document.querySelector('.input-grid');
const resizeBtn = document.querySelector('.btn-reset');
resizeBtn.addEventListener('click',resizeGrid)
const clearBtn = document.querySelector('.btn-clear');
clearBtn.addEventListener('click', clearDrawing);
document.querySelector("html").addEventListener('mouseup', disableDraw);
const modeBtn = document.querySelector('.btn-mode');
modeBtn.addEventListener('click', changeMode);
const eraserBtn = document.querySelector('.btn-eraser');
const rainbowBtn = document.querySelector('.btn-rainbow');
const randomBtn = document.querySelector('.btn-random');
eraserBtn.addEventListener('click',changeBrush);
rainbowBtn.addEventListener('click',changeBrush);
randomBtn.addEventListener('click',changeBrush);
createGrid(size);
function changeBrush(e){
newBrush = e.target.textContent.toLowerCase();
eraserBtn.classList.remove('btn-eraser-clicked');
rainbowBtn.classList.remove('btn-rainbow-clicked');
randomBtn.classList.remove('btn-random-clicked');
if(brush===newBrush){
brush = 'default';
return;
}
brush = newBrush;
e.target.classList.add(`btn-${brush}-clicked`);
}
function createGrid(size) {
let numPixels = size * size
let pixelLength = (getComputedStyle(grid).minWidth.replace('px', '') / size) + 'px';
while (numPixels) {
const pixel = document.createElement('div');
pixel.style.width = pixelLength;
pixel.style.height = pixelLength;
pixel.style.backgroundColor = 'transparent';
pixel.classList.add('pixel');
grid.appendChild(pixel);
pixel.addEventListener('mouseover', drawPixel);
pixel.addEventListener('mousedown', enableDraw)
pixel.addEventListener('mousedown', drawPixel);
numPixels--;
}
}
function resizeGrid(){
size = sizeInput.value;
if (size > 200) {
size = 200;
} else if (size < 1) {
size = 1;
} else if (size === ""){
size = 50;
}
clearGrid();
createGrid(size);
}
function drawPixel(e) {
if (!draw) return;
let color = getColor();
e.target.style.backgroundColor = color;
}
function getColor() {
if (brush === 'default') {
return 'rgb(56, 56, 56)';
} else if (brush === 'eraser') {
return 'transparent';
} else if (brush === 'rainbow') {
let color = `hsl(${hslCounter},100%,50%)`;
if (hslCounter === 360) {
hslCounter = 0;
} else {
hslCounter += 10;
}
return color;
} else if (brush === 'random') {
return `hsl(${Math.random() * 360}, ${Math.random() * (100 - 85) + 85}%, ${Math.random() * (65 - 35) + 35}%)`;
}
}
function changeMode(e) {
if (mode === 'Hover') {
mode = 'Click';
draw = false;
} else {
mode = 'Hover';
draw = true;
}
e.target.textContent = `On ${mode}`;
}
function enableDraw() {
if(mode==='Click'){
draw = true;
}
}
function disableDraw() {
if(mode==='Click'){
draw = false;
}
}
function clearDrawing() {
Array.from(document.querySelectorAll('.pixel')).forEach(pixel => pixel.style.backgroundColor = 'transparent');
}
function clearGrid() {
Array.from(document.querySelectorAll('.pixel')).forEach(pixel => grid.removeChild(pixel));
}