-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
109 lines (96 loc) · 2.82 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
const MAX_SIZE = 100;
const DEFAULT_SIZE = 16;
const container = document.querySelector(".container");
const clearButton = document.querySelector("#clearButton");
const randomButton = document.querySelector("#random");
const opacityButton = document.querySelector("#opacity");
const defaultButton = document.querySelector("#default");
const colorButtons = document.querySelectorAll("#colors > button");
function generateGrid(size = DEFAULT_SIZE){
if (size > 100) size = MAX_SIZE;
let cellNumber = size * size;
container.style = `grid-template-columns: repeat(${size}, auto)`;
for (let i=0; i < cellNumber; i++) {
let cell = document.createElement("div");
cell.className = "cell";
cell.style.backgroundColor = "rgba(0,0,0,0)";
container.appendChild(cell);
}
console.log(size);
}
function draw(event) {
let cell = event.target;
if (cell.className === "cell"){
if (opacityButton.hasAttribute("disabled")) {
let newCOlor = `rgba(${getColor(cell, "hue")}, ${updateOpacity(cell)})`;
cell.style.backgroundColor = newCOlor;
console.log(newCOlor);
}
else if (randomButton.hasAttribute("disabled")) {
cell.style.backgroundColor =
`rgba(${randomColor()},${randomColor()},${randomColor()},${updateOpacity(cell)})`;
}
else {
cell.style.backgroundColor = "rgba(0,0,0,0.1)";
}
}
}
function updateOpacity(element) {
let newOpacity = (Number(getColor(element, "a")) + 0.1).toString();
return newOpacity;
}
function getColor(element, colorValue) {
let color = element.style.backgroundColor;
let reg = /(?<=\().+(?=\))/g; // captures only the rgba values
let colorArray;
if (color !== "") {
colorArray = color.match(reg).toString().split(",");
}
switch (colorValue) {
case "r": return colorArray[0];
case "g": return colorArray[1];
case "b": return colorArray[2];
case "a": return colorArray[3];
case "hue":
return `${colorArray[0]},${colorArray[1]},${colorArray[2]}`;
default: return "0,0,0,0";
}
}
function newGrid() {
let size = getNewGridSize("Input a number");
console.log(typeof size);
clearGrid();
generateGrid(size);
}
function getNewGridSize(message) {
let size = prompt("What size ?", message);
if (size === message) {
getNewGridSize(message);
}
else if (size === "default") {
return DEFAULT_SIZE;
}
else return Number(size);
}
function clearGrid() {
while (container.firstChild) {
container.removeChild(container.firstChild);
}
}
function randomColor() {
return Math.floor(Math.random() * 255);
}
function enableButtons (elements) {
elements.forEach(element => {
element.removeAttribute("disabled", "");
})
}
container.addEventListener("mouseover", draw);
colorButtons.forEach(button => {
button.addEventListener("click", event => {
enableButtons(colorButtons);
event.target.setAttribute("disabled", "");
});
});
clearButton.addEventListener("click", newGrid);
generateGrid();