-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
161 lines (159 loc) · 5.61 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const grid = document.querySelector(".grid");
function paint(){
const grid = document.querySelector(".grid");
grid.addEventListener("mousedown", setColor);
grid.addEventListener("mouseup", stopFunction);
grid.addEventListener("dragend", stopFunction);
}
function stopFunction(){
let nodelist = document.querySelectorAll(".grid-element");
for(let div of nodelist){
div.removeEventListener("mousemove", greyScale);
div.removeEventListener("mousemove", skittles);
div.removeEventListener("mousemove", updateColor);
}
}
const clear = document.querySelector("#clear");
clear.addEventListener("click", reset);
function createDivs(size){
for(i = 1; i < size * size + 1; i++){
let createdDivs = document.createElement("div")
grid.appendChild(createdDivs)
createdDivs.setAttribute("data-pos", i)
createdDivs.setAttribute("class", "grid-element" )
}
grid.style.gridTemplateColumns = `repeat(${size}, 1fr)`
grid.style.gridTemplateRows = `repeat(${size}, 1fr)`
}
let newSize = [16]
let oldSize = [0]
const userInput = document.querySelector("#userInput");
userInput.addEventListener("input", function(e){
resetRT();
const sizeDisplay = document.querySelector("#sizeDisplay")
sizeDisplay.textContent = `${userInput.value}x${userInput.value}`;
let size = parseInt(userInput.value);
newSize.unshift(size);
oldSize = newSize.pop();
createDivs(size);
paint();
return oldSize;
})
function reset(){
let divs = document.querySelectorAll(".grid-element")
divs.forEach(function(div){
div.parentNode.removeChild(div);
return false;
});
createDivs(newSize);
paint();
}
function resetRT(size){
let divs = document.querySelectorAll(".grid-element")
divs.forEach(function(div){
div.parentNode.removeChild(div);
return false;
});
createDivs(size);
}
createDivs(16);
const li = document.querySelectorAll("ul li")
li.forEach(function(e){
e.addEventListener("click", fetchClassName)
})
let className = ["black"]
let oldClassName = ["black"]
let currentColor = "black"
function fetchClassName(e){
className.unshift(e.srcElement.className);
oldClassName = className.pop();
paint();
}
function setColor(){
let nodelist = document.querySelectorAll(".grid-element");
let div;
switch(className[0]){
case "black":
currentColor = "rgba(0, 0, 0, 0.99)"
for(div of nodelist){
div.removeEventListener("mousemove", greyScale);
div.removeEventListener("mousemove", skittles);
div.addEventListener("mousemove", updateColor);
}
break;
case "white":
currentColor = "rgba(255, 255, 255, 0.99)"
for(div of nodelist){
div.removeEventListener("mousemove", greyScale);
div.removeEventListener("mousemove", skittles);
div.addEventListener("mousemove", updateColor);
}
break;
case "red":
currentColor = "rgba(255, 0 ,0, 0.99)"
for(div of nodelist){
div.removeEventListener("mousemove", greyScale);
div.removeEventListener("mousemove", skittles);
div.addEventListener("mousemove", updateColor);
}
break;
case "green":
currentColor = "rgba(0, 255, 0, 0.99)"
for(div of nodelist){
div.removeEventListener("mousemove", greyScale);
div.removeEventListener("mousemove", skittles);
div.addEventListener("mousemove", updateColor);
}
break;
case "blue":
currentColor = "rgba(0, 0, 255, 0.99)"
for(div of nodelist){
div.removeEventListener("mousemove", greyScale);
div.removeEventListener("mousemove", skittles);
div.addEventListener("mousemove", updateColor);
}
break;
case "rainbow":
for(div of nodelist){
div.removeEventListener("mousemove", greyScale);
div.removeEventListener("mousemove", updateColor);
div.addEventListener("mousemove", skittles);
}
break;
case "erase":
currentColor = "rgba(0, 0, 0, 0.1)"
for(div of nodelist){
div.removeEventListener("mousemove", greyScale);
div.removeEventListener("mousemove", skittles);
div.addEventListener("mousemove", updateColor);
}
break;
case "greyscale":
for(div of nodelist){
div.style.backgroundColor = "rgba(0, 0, 0, 0.1)"
div.removeEventListener("mousemove", skittles);
div.removeEventListener("mousemove", updateColor);
div.addEventListener("mousemove", greyScale);
}
break;
}
}
function updateColor(e){
e.target.style.backgroundColor = currentColor //className[0] works but not what I want
console.log(currentColor)
}
function skittles(e){
let r = Math.floor(Math.random() * 256);
let g = Math.floor(Math.random() * 256);
let b = Math.floor(Math.random() * 256);
let rgb = `rgba(${r}, ${g}, ${b}, 0.7)`
e.target.style.backgroundColor = rgb;
}
function greyScale(e){
const value = getComputedStyle(e.target).getPropertyValue("background-color");
const parts = value.match(/[\d.]+/g);
parts[3] = Number(parts[3]) + 0.1;
let rgba = `rgba(0, 0, 0, ${parts[3]})`
e.target.style.backgroundColor = rgba;
}
paint();