-
Notifications
You must be signed in to change notification settings - Fork 0
/
etch-a-sketch.js
154 lines (132 loc) · 3.99 KB
/
etch-a-sketch.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
const container = document.querySelector(".container");
const grey = document.querySelector(".grey");
const pastel = document.querySelector(".pastel");
const span = document.querySelector("span");
const random = document.querySelector(".random");
const pix16 = document.querySelector(".grid16");
const pix32 = document.querySelector(".grid32");
const pix64 = document.querySelector(".grid64");
const colorWell = document.querySelector("#colorWell");
let colorPick;
window.addEventListener("load", chooseColor, false);
pix16.addEventListener("click", grid16);
pix32.addEventListener("click", grid32);
pix64.addEventListener("click", grid64);
playGame(16);
function playGame(value) {
// create columns
for (let i = 1; i <= value; i++) {
const gridCol = document.createElement("div");
gridCol.classList.add("gridCol");
// create rows
for (let j = 2; j <= value; j++) {
const gridRow = document.createElement("div");
gridRow.classList.add("gridRow");
container.appendChild(gridRow);
// run pickGrey() if run over the grids
gridRow.addEventListener("mouseover", function (e) {
e.target.style.background = pickGrey();
});
gridCol.addEventListener("mouseover", function (e) {
e.target.style.background = pickGrey();
});
// if color picker is chosen
colorWell.addEventListener("click", function (e) {
// run the chooseColor() function
gridRow.addEventListener("mouseover", function (e) {
e.target.style.background = chooseColor();
});
gridCol.addEventListener("mouseover", function (e) {
e.target.style.background = chooseColor();
});
});
// if grey scale is chosen
grey.addEventListener("click", function (e) {
// run the pickGrey() function
gridRow.addEventListener("mouseover", function (e) {
e.target.style.background = pickGrey();
});
gridCol.addEventListener("mouseover", function (e) {
e.target.style.background = pickGrey();
});
});
// if pastel scale is chosen then
pastel.addEventListener("click", function (e) {
// run the pickPastel() function
gridRow.addEventListener("mouseover", function (e) {
e.target.style.background = pickPastel();
});
gridCol.addEventListener("mouseover", function (e) {
e.target.style.background = pickPastel();
});
});
random.addEventListener("click", function (e) {
gridRow.addEventListener("mouseover", function (e) {
e.target.style.background = pickRandom();
});
gridCol.addEventListener("mouseover", function (e) {
e.target.style.background = pickRandom();
});
});
}
}
}
function grid16() {
restart();
container.setAttribute("id", "grid16");
playGame(16);
}
function grid32() {
restart();
container.setAttribute("id", "grid32");
playGame(32);
}
function grid64() {
restart();
container.setAttribute("id", "grid64");
playGame(64);
}
function pickGrey() {
let random = Math.random();
let rgb = `rgba(0,0,0,${random})`;
console.log(rgb);
return rgb;
}
function pickPastel() {
return (
"hsl(" +
360 * Math.random() +
"," +
(25 + 70 * Math.random()) +
"%," +
(85 + 10 * Math.random()) +
"%)"
);
}
function pickRandom() {
let random = [];
for (let i = 0; i < 3; i++) {
let randomChoice = Math.floor(Math.random() * 256);
random.push(randomChoice);
}
let joinChoice = random.join(", ");
let rgb = `rgb(${joinChoice})`;
return rgb;
}
function chooseColor() {
colorWell.addEventListener("input", updateFirst, false);
return colorPick;
}
function updateFirst(event) {
colorPick = event.target.value;
console.log(colorWell.value);
return colorPick;
}
function restart(e) {
document
.querySelectorAll(".gridCol")
.forEach((e) => e.parentNode.removeChild(e));
document
.querySelectorAll(".gridRow")
.forEach((e) => e.parentNode.removeChild(e));
}