-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathetchasketchJS.js
More file actions
133 lines (119 loc) · 3.7 KB
/
etchasketchJS.js
File metadata and controls
133 lines (119 loc) · 3.7 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
/* etchasketchJS.js */
// creating and styling header
const headerText = document.createElement("h2");
headerText.classList.add("headerText");
headerText.textContent = "It's time to draw!";
headerText.style.color = getColorHeader();
headerText.style.textAlign = "center";
header.appendChild(headerText);
// creating button
const btn = document.createElement("button");
btn.classList.add("btn");
let btnText = document.createTextNode("Set Grid Size");
btn.appendChild(btnText);
button.appendChild(btn);
// creating and styling container element
const container = document.getElementById("container");
container.classList.add("container");
document.getElementById("container").style.width = "900px";
document.getElementById("container").style.height = "900px";
container.style.border = "solid";
container.style.borderWidth = "thin";
// random page background color
document.body.style.backgroundColor = getColor();
// creating hover constants with random color function
const hoverIn = (ev) => {
ev.currentTarget.style.backgroundColor = getColor();
// opacity increase
ev.currentTarget.style.opacity -= "-0.1";
};
const hoverOut = (ev) => {
ev.currentTarget.style.backgroundColor = getColor();
};
// creating random color function for gridSquares
function getColor() {
return (
"hsl(" +
360 * Math.random() +
"," +
(25 + 70 * Math.random()) +
"%," +
(80 + 10 * Math.random()) +
"%)"
);
}
// creating random color function for headerText
function getColorHeader() {
return (
"hsl(" +
360 * Math.random() +
"," +
(25 + 70 * Math.random()) +
"%," +
(50 + 10 * Math.random()) +
"%)"
);
}
// set default grid dimensions
let num = 16;
// for loop function to create grid rows
function createGridRow(num) {
for (let x = 0; x < num; x++) {
let gridSquare = document.createElement("div");
gridSquare.setAttribute("class", "new");
gridSquare.classList.add("gridSquare");
// gridSquare styling
gridSquare.style.height = `${900 / num}px`;
gridSquare.style.width = `${900 / num}px`;
// mouse hover event listener
gridSquare.addEventListener("mouseover", hoverIn);
gridSquare.addEventListener("mouseout", hoverOut);
// appending gridSquare
container.appendChild(gridSquare);
}
}
// for loop function to create grid columns by calling grid row function
function createGridColumn(num) {
for (let y = 0; y < num; y++) {
newGridRow(num);
}
}
// calling functions to create grid
createGridColumn(num);
// button event
btn.addEventListener("click", () => {
let userInput = prompt("Please choose a canvas size.");
if (userInput == null || userInput == "" || userInput > 100) {
alert("Please input a valid number between 1 and 100!");
} else {
console.log(userInput);
// clearing existing grid
container.replaceChildren();
// creating new grid with user input
userNum = Number(userInput);
newGridColumn(userNum);
// return canvasQuery;
}
});
// for loop function to create new grid rows
function newGridRow(userNum) {
for (let x = 0; x < userNum; x++) {
let gridSquare = document.createElement("div");
gridSquare.setAttribute("class", "new");
gridSquare.classList.add("gridSquare");
// gridSquare styling
gridSquare.style.height = `${900 / userNum}px`;
gridSquare.style.width = `${900 / userNum}px`;
// mouse hover event listener
gridSquare.addEventListener("mouseover", hoverIn);
gridSquare.addEventListener("mouseout", hoverOut);
// appending gridSquare
container.appendChild(gridSquare);
}
}
// for loop function to create new grid columns by calling new grid row function
function newGridColumn(userNum) {
for (let y = 0; y < userNum; y++) {
newGridRow(userNum);
}
}