-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
82 lines (74 loc) · 2.58 KB
/
app.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
const container = document.querySelector('#container');
const etch = document.querySelector('.etch');
const gridRow = document.createElement('div');
const resizeBtn = document.getElementById('refresh');
const randomBtn = document.getElementById('randomBtn');
var gridSize = 30;
var random = false;
//Creating grid
function createGrid() {
container.innerHTML = "";
let gridMeasuremnt = 500/gridSize;
for (i=0; i<gridSize; i++) {
const gridRow = document.createElement('div');
gridRow.setAttribute('style', 'height :' + `${gridMeasuremnt}` + 'px');
gridRow.classList.add('gridRow');
container.appendChild(gridRow);
for (j=0; j<gridSize; j++) {
const square = document.createElement('div');
square.classList.add('square');
gridRow.appendChild(square);
square.setAttribute("style",
'height :' + `${gridMeasuremnt}` + 'px; width :' + `${gridMeasuremnt}` + 'px; opacity: 0');
square.addEventListener("mouseover", alterSquare);
}
}
}
// Depending on color selection changes opacity of square on mouseover
function alterSquare () {
if (random === true) {
this.style.background = "rgb("+Math.floor(Math.random()*256) + ","+
Math.floor(Math.random()*256) + "," + Math.floor(Math.random()*256) + ")";
this.style.opacity = parseFloat(this.style.opacity) + 0.25;
} else {
this.style.background = "black";
this.style.opacity = parseFloat(this.style.opacity) + 0.25;
}
}
// Adding and removing shake effect
resizeBtn.addEventListener("click", (e) => {
e.preventDefault();
etch.classList.add("apply-shake");
container.innerHTML = "";
createGrid();
});
etch.addEventListener("animationend", (e) => {
etch.classList.remove("apply-shake");
});
// Creates new grid when new values given by user
function newGridNum() {
if (parseInt(noOfPixels.value) < 101 && parseInt(noOfPixels.value) > 1 ){
numChoice = parseInt(noOfPixels.value);
numChoiceInt = parseInt(numChoice);
} else {
alert("Pick an integer between 2-100");
}
gridSize = numChoiceInt;
createGrid();
}
//Event Listeners
//Toggle between black and color
randomBtn.addEventListener("click", (e) => {
if (randomBtn.innerHTML == "Random") {
random = true;
createGrid();
randomBtn.innerHTML = "Black"
} else {
random = false;
createGrid();
randomBtn.innerHTML = "Random"
}
});
submitBtn.addEventListener("click", newGridNum);
submitBtn.addEventListener("click", createGrid);
createGrid();