-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (66 loc) · 2.54 KB
/
Copy pathscript.js
File metadata and controls
90 lines (66 loc) · 2.54 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
//Global Variables
let sliderInput = document.getElementById("sliderInput");
let sliderOutput = document.getElementById("sliderOutput");
let drawArea = document.getElementById("drawArea");
let buttonWhite = document.getElementById("white");
let buttonRainbow = document.getElementById("rainbow");
let buttonErase = document.getElementById("eraser");
sliderOutput.textContent = `${sliderInput.value}x${sliderInput.value}`;
//function that changes the displayed value of the slider.
sliderInput.oninput = function() {
sliderOutput.textContent = `${this.value}x${this.value}`;
sliderValue = this.value;
}
let sliderValue = sliderInput.value;
//function that creates squares based on the input value from the slider
function createSquares () {
let drawArea = document.getElementById("drawArea");
const parent = document.getElementById("drawArea");
while (parent.firstChild) {
parent.firstChild.remove()
}
for (i = 0; i < (sliderValue * sliderValue); i++) {
let column = document.createElement("div");
drawArea.appendChild(column);
column.classList.add("square");
column.style.height = 30 / sliderValue + "em";
column.style.width = 30 / sliderValue + "em";
}
};
function randomColor() {
let random = Math.floor(Math.random()*16777215).toString(16);
let color = "#" + random;
return color;
}
//function that changes color of Square to white when hovering
function colorWhite() {
let whiteColor = document.getElementsByClassName("square");
for (i = 0; i < whiteColor.length; i++) {
whiteColor[i].onmouseover = (e) => {
e.target.style.backgroundColor = "white";
};
};
};
//function that erases color of Square when hovering
function colorErase() {
let eraseColor = document.getElementsByClassName("square");
for (i = 0; i < eraseColor.length; i++) {
eraseColor[i].onmouseover = (e) => {
e.target.style.backgroundColor = "#3E3054";
};
};
};
//function that changes color of Square randomly when hovering
function colorRainbow() {
let rainbowColor = document.getElementsByClassName("square");
for (i = 0; i < rainbowColor.length; i++) {
rainbowColor[i].onmouseover = (e) => {
e.target.style.backgroundColor = randomColor();
};
};
};
createSquares();
sliderInput.addEventListener("input", createSquares);
buttonWhite.addEventListener("click", colorWhite);
buttonRainbow.addEventListener("click", colorRainbow);
buttonErase.addEventListener("click", colorErase);