-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
329 lines (266 loc) · 9.66 KB
/
script.js
File metadata and controls
329 lines (266 loc) · 9.66 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
const container=document.createElement('div');
container.classList.add('grid-container');
const canvasGrid=document.querySelector('#canvas');
const modeInfo=document.querySelector('#mode-info');
canvasGrid.appendChild(container);
let mouseDown = false;
document.body.onmousedown= () => (mouseDown = true)
document.body.onmouseup = () => (mouseDown = false)
document.addEventListener("dragover", (event) => {
event.preventDefault();
});
const slider=document.querySelector('#slider');
const sliderText=document.querySelector('#slider-text');
const containerWidth=container.offsetWidth;
const rainbowBtn=document.querySelector('#rainbow');
let rainbowBtnClicks=0;
const opacityBtn=document.querySelector('#opacity');
let opacityBtnClicks=0;
let opacityBtnEnabled=false;
const clearBtn=document.querySelector('#reset');
let rainbowBtnEnabled=false;
let eraserEnabled=false;
let brushEnabled=true;
let currentColor='#252525';
const colorPicker=document.querySelector('#color-picker');
const brushBtn=document.querySelector('#brush');
let brushBtnClicks=1;
const eraserBtn=document.querySelector('#eraser');
let eraserBtnClicks=0;
brushBtn.style.cssText='border-style: inset; border-width: 3px;border-color: #2e2e2e;';
opacityBtn.addEventListener('click',()=>{
opacityBtnClicks++;
if(brushEnabled==true){
brushBtnClicks++;
}
if(eraserEnabled==true){
eraserBtnClicks++;
}
if(rainbowBtnEnabled==true){
rainbowBtnClicks++;
}
if(opacityBtnClicks%2==0){
opacityBtn.style.cssText='border-style: none;';
console.log("opacity disabled by opacity click");
}else{
opacityBtnEnabled=true;
opacityBtn.style.cssText='border-style: inset; border-width: 3px;border-color: #2e2e2e;';
opacityBtn.style.transitionDuration = "0.1s";
modeInfo.innerHTML='Opacity Progression Mode';
console.log("opacity enabled by opacity click");
brushEnabled=false;
brushBtn.style.cssText='border-style: none;';
console.log("brush disabled by opacity");
eraserEnabled=false;
eraserBtn.style.cssText='border-style: none;';
console.log("eraser disabled by opacity");
rainbowBtnEnabled=false;
rainbowBtn.style.cssText='border-style: none;';
console.log("rainbow disabled by opacity");
}
})
rainbowBtn.addEventListener('click',()=>{
rainbowBtnClicks++;
console.log("rainbow clicks "+rainbowBtnClicks);
if(opacityBtnEnabled==true){
opacityBtnClicks++;
}
if(brushEnabled==true){
brushBtnClicks++;
}
if(eraserEnabled==true){
eraserBtnClicks++;
}
if(rainbowBtnClicks%2==0){
rainbowBtn.style.cssText='border-style: none;';
console.log("rainbow disabled by rainbow click");
}
else{
console.log("rainbow enabled by rainbow click");
rainbowBtn.style.cssText='border-style: inset; border-width: 3px;border-color: #2e2e2e;';
rainbowBtn.style.transitionDuration = "0.1s";
modeInfo.innerHTML='Psychedelic Mode';
rainbowBtnEnabled=true;
eraserBtn.style.cssText='border-style: none;';
brushBtn.style.cssText='border-style: none;';
brushEnabled=false;
eraserEnabled=false;
opacityBtnEnabled=false;
opacityBtn.style.cssText='border-style: none;';
console.log("opacity disabled by opacity");
console.log("eraser disabled by rainbow click");
console.log("brush disabled by rainbow click");
}
})
brushBtn.addEventListener('click',()=>{
brushBtnClicks++;
console.log("brush clicks in "+brushBtnClicks);
if(eraserEnabled==true){
eraserBtnClicks++;
}
if(rainbowBtnEnabled==true){
rainbowBtnClicks++;
}
if(opacityBtnEnabled){
opacityBtnClicks++;
}
if (brushBtnClicks%2==0){
brushBtn.style.cssText='border-style: none;';
brushEnabled=false;
console.log("brush disabled by brush click");
}else{
console.log("brush enabled by brush click");
brushBtn.style.cssText='border-style: inset; border-width: 3px;border-color: #2e2e2e;';
brushBtn.style.transitionDuration = "0.1s";
modeInfo.innerHTML='Basic Color Mode';
brushEnabled=true;
eraserBtn.style.cssText='border-style: none;';
eraserEnabled=false;
rainbowBtn.style.cssText='border-style: none;';
rainbowBtnEnabled=false;
opacityBtnEnabled=false;
opacityBtn.style.cssText='border-style: none;';
console.log("opacity disabled by opacity");
console.log("eraser disabled by brush click");
console.log("brush clicks out "+brushBtnClicks);
}
})
eraserBtn.addEventListener('click',()=>{
eraserBtnClicks++;
console.log("eraser clicks "+eraserBtnClicks);
console.log("brush clicks in"+brushBtnClicks);
if(opacityBtnEnabled==true){
opacityBtnClicks++;
}
if(brushEnabled==true){
brushBtnClicks++;
console.log("brush clicks increased");
}
if(rainbowBtnEnabled==true){
rainbowBtnClicks++;
}
if (eraserBtnClicks%2==0){
eraserBtn.style.cssText='border-style: none;';
eraserEnabled=false;
console.log("eraser disabled by eraser click");
}else{
eraserBtn.style.cssText='border-style: inset; border-width: 3px;border-color: #2e2e2e;';
eraserBtn.style.transitionDuration = "0.1s";
modeInfo.innerHTML='Eraser';
brushBtn.style.cssText='border-style: none;';
brushEnabled=false;
console.log("brush disabled by eraser click");
rainbowBtn.style.cssText='border-style: none;';
rainbowBtnEnabled=false;
console.log("rainbow disabled by eraser click");
eraserEnabled=true;
console.log("eraser enabled by eraser click");
console.log("brush clicks out "+brushBtnClicks);
opacityBtnEnabled=false;
opacityBtn.style.cssText='border-style: none;';
console.log("opacity disabled by opacity");
}
})
const resetBtn=document.querySelector('#clear');
resetBtn.addEventListener('click',()=>{
resetGrid();
})
function newColor () {
currentColor=this.value;
console.log(this.value);
}
colorPicker.addEventListener('change', newColor);
slider.addEventListener('input',()=>{
let val = slider.value
sliderText.innerHTML = "<b>Choose Grid Size:</b> <br>"+val+"x"+val;
clearGrid();
createGrid(val);
})
function clearGrid(){
while (container.firstChild) {
container.removeChild(container.lastChild);
}
}
function resetGrid(){
console.log('clear');
const boxes=document.querySelectorAll('#boxes');
boxes.forEach(box => {
box.style.backgroundColor='white';
box.style.opacity=1;
console.log(box.style.backgroundColor);
console.log(box.style.opacity);
})
}
function createGrid(noOfRows){
console.log(noOfRows);
for(let i=0;i<noOfRows;i++){
const rows=document.createElement('div');
rows.classList.add('rows');
container.appendChild(rows);
rows.style.width=containerWidth + 'px';
rows.style.height=containerWidth/noOfRows + 'px';
for(let i=0;i<noOfRows;i++){
const rowSquares=document.createElement('div');
rowSquares.classList.add('row-squares');
rowSquares.addEventListener('mouseover', changeColor);
rowSquares.addEventListener('mousedown', changeColor);
rowSquares.style.backgroundColor = 'white';
rowSquares.setAttribute('id','boxes');
rows.appendChild(rowSquares);
rowSquares.style.width=containerWidth/noOfRows + 'px';
rowSquares.style.height=containerWidth/noOfRows + 'px';
}
}
// container.onclick== function(e) {
// console.log(e.target.id);
// changeColor(e);
// }
// container.onmouseover = function(e) {
// console.log(e.target.id);
// changeColor(e);
// }
// changeColor=(e) => {
// if(mousedown){
// e.target.style.backgroundColor='red';
// }
// }
// container.addEventListener("mouseover", eventHandler, false);
// function eventHandler(event) {
// if (event.target.id !== "pixel") {
// return;
// }
// changeColor(event);
// }
// function changeColor(event) {
// event.target.setAttribute("style", "background-color:red;");
// }
}
let opacityGrow=0;
function changeColor(e){
if (e.type === 'mouseover' && !mouseDown) return
if (eraserEnabled){
e.target.style.backgroundColor='white';
e.target.style.opacity=1;
}
else if (brushBtnClicks%2!=0&&brushEnabled){
e.target.style.opacity=1;
e.target.style.backgroundColor = currentColor;
}
else if (rainbowBtnClicks%2!=0&&rainbowBtnEnabled){
const randomColor = "#" + Math.floor(Math.random() * 16777215).toString(16);
e.target.style.backgroundColor = randomColor;
}
else if (opacityBtnClicks%2!=0&&opacityBtnEnabled){
if(e.target.style.opacity==1&& e.target.style.backgroundColor=='white'){
console.log("first element");
e.target.style.opacity = 0;
}
let opacityNew=e.target.style.opacity;
console.log(opacityNew);
opacityNew=opacityNew*100;
e.target.style.opacity = opacityNew +10+'%';
console.log(opacityNew);
e.target.style.backgroundColor = currentColor;
}
}
createGrid(16);