-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
145 lines (112 loc) · 4.55 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
const ColorModes = {BLACK : 'black', RANDOM: 'randomColor', ERASE: 'erase'};
const maxGridSize = 100;
let shouldDraw = false;
let setGlobalListeners = false;
let colorMode = ColorModes.BLACK;
function init() {
let defaultGridSize = 16;
initGrid(defaultGridSize);
initMenu();
}
init();
function initMenu() {
const resetBtn = document.querySelector('#resetBtn');
resetBtn.addEventListener('click', () => {
const defaultOption = 16;
let gridSize = window.prompt(`Select new grid style (only numbers below ${maxGridSize}):`, defaultOption);
if (gridSize > maxGridSize) {
alert(`Sorry, choose number below ${maxGridSize}`)
return;
} else if (gridSize === null) {
return;
}
initGrid(gridSize);
});
const randomColorBtn = document.querySelector('#randomColorBtn');
randomColorBtn.addEventListener('click', () => {
colorMode = ColorModes.RANDOM;
randomColorBtn.style.backgroundColor = 'orange';
blackColorBtn.style.backgroundColor = null;
eraseBtn.style.backgroundColor = null;
});
const blackColorBtn = document.querySelector('#blackColorBtn');
blackColorBtn.addEventListener('click', () => {
colorMode = ColorModes.BLACK;
blackColorBtn.style.backgroundColor = 'orange';
eraseBtn.style.backgroundColor = null;
randomColorBtn.style.backgroundColor = null;
});
const eraseBtn = document.querySelector('#eraseBtn');
eraseBtn.addEventListener('click', () => {
colorMode = ColorModes.ERASE;
eraseBtn.style.backgroundColor = 'orange';
blackColorBtn.style.backgroundColor = null;
randomColorBtn.style.backgroundColor = null;
});
}
function initGrid(gridSize) {
resetDefaults();
const gridContainer = document.querySelector('.grid-container');
gridContainer.innerHTML = "";
for (let i = 0; i < gridSize * gridSize; i++) {
let gridItem = document.createElement('div');
gridItem.className = 'grid-item';
gridContainer.appendChild(gridItem);
}
gridContainer.style.gridTemplateColumns = `repeat(${gridSize}, 1fr)`;
gridContainer.style.gridTemplateRows = `repeat(${gridSize}, 1fr)`;
const gridStatus = document.querySelector('#gridStatus');
gridStatus.textContent = `Chosen grid: ${gridSize} x ${gridSize}`;
setUpDrawingEngin();
}
function resetDefaults() {
colorMode = ColorModes.BLACK;
const randomColorBtn = document.querySelector('#randomColorBtn');
const blackColorBtn = document.querySelector('#blackColorBtn');
const eraseBtn = document.querySelector('#eraseBtn');
blackColorBtn.style.backgroundColor = 'orange';
randomColorBtn.style.backgroundColor = null;
eraseBtn.style.backgroundColor = null;
}
function setUpDrawingEngin() {
if (!setGlobalListeners) {
const gridContainer = document.querySelector('.grid-container');
gridContainer.addEventListener('mousedown', () => {
shouldDraw = true;
});
const body = document.querySelector('body');
body.addEventListener('mouseup', () => {
shouldDraw = false;
});
setGlobalListeners = true;
}
const gridItems = document.querySelectorAll('.grid-item');
gridItems.forEach((item) => {
item.addEventListener('mouseenter', (e) => {
if (!shouldDraw) {
return;
}
if (colorMode === ColorModes.BLACK) {
const blackPencilColor = 'black';
// reset opacity in case when we draw over other colors
if (e.currentTarget.style.backgroundColor !== blackPencilColor) {
e.currentTarget.style.opacity = 0;
}
e.currentTarget.style.backgroundColor = blackPencilColor;
let opacityIncrement = 0.10;
let currentOpacity = Number(e.currentTarget.style.opacity);
let opacity = currentOpacity + opacityIncrement;
e.currentTarget.style.opacity = opacity;
}
if (colorMode === ColorModes.RANDOM) {
let randomColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
e.currentTarget.style.backgroundColor = randomColor;
e.currentTarget.style.opacity = 1;
}
if (colorMode === ColorModes.ERASE) {
e.currentTarget.style.opacity = 1;
e.currentTarget.style.backgroundColor = 'white';
}
});
});
};