-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
118 lines (100 loc) · 4.18 KB
/
javascript.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
const gridDiv = document.getElementById('grid');
const dimensionBtn = document.getElementById('dimensionBtn');
const clearBtn = document.getElementById('clearBtn');
const rgbBtn = document.getElementById('rgbBtn');
const progressiveBtn = document.getElementById('progressBtn');
const blackBtn = document.getElementById('blackBtn');
var gridDimension = parseInt(getComputedStyle(document.getElementById('grid')).getPropertyValue('width')); //dimension in vertical width VW set for the grid itself
var currentDimension;
var currentColor = 'black';
dimensionBtn.addEventListener('click', () => dimensionClicked());
if(currentDimension == null) setGrid(16);
// Main game function
function game(){
let pixels = document.querySelectorAll('.pixels');
pixels.forEach(pixel => {
pixel.addEventListener('mouseover', function () {
setColor(pixel);
});
});
document.addEventListener('mouseover', () => adjustGrid());
clearBtn.addEventListener('click', () => clearGrid());
rgbBtn.addEventListener('click', () => currentColor = 'rgb');
blackBtn.addEventListener('click', () => currentColor = 'black');
progressiveBtn.addEventListener('click', () => currentColor = 'progressive');
}
// Adjust grid with screen sizes
function adjustGrid(){
let pixels = document.querySelectorAll('.pixels');
let columns = document.querySelectorAll('.columns');
gridDimension = parseInt(getComputedStyle(document.getElementById('grid')).getPropertyValue('width'))
columns.forEach(column => {
column.style.height = gridDimension / currentDimension + 'px';
column.style.width = gridDimension + 'px';
});
pixels.forEach(pixel => {
pixel.style.height = gridDimension / currentDimension + 'px';
pixel.style.width = gridDimension / currentDimension + 'px';
});
}
//Set Color of pixel when mouse hover it
function setColor(pixel, isAlreadyColored){
switch(currentColor){
case 'rgb':
pixel.style.backgroundColor = 'rgb(' + getRandomColor() + ')';
break;
case 'progressive':
let colorArray = (pixel.style.backgroundColor).replace(/[^\d\,]/g, '').split(',');
if(colorArray[0] === colorArray[1]){
let newColor = parseInt(colorArray[0]) - 50; //current shade of black + adding shade
console.log(colorArray[0]);
console.log(newColor);
pixel.style.backgroundColor = 'rgb(' + newColor + ',' + newColor + ',' + newColor + ')';
} else{
pixel.style.backgroundColor = 'rgb(200,200,200)';
}
break;
default:
pixel.style.backgroundColor = currentColor;
}
}
function getProgressive(pixel){
}
//Return a random rgb array
function getRandomColor(){
return [Math.floor(Math.random() * 255), Math.floor(Math.random() * 255), Math.floor(Math.random() * 255)];
}
//Clearing the Grid
function clearGrid(){
let pixels = document.querySelectorAll('.pixels');
pixels.forEach(pixel => {
pixel.style.backgroundColor = '';
});
}
//Setting up the Grid with dimension by default or asked
function setGrid(dimension){
gridDiv.innerHTML = '';
currentDimension = dimension;
for(let i = 0; i < dimension; i++){
let column = gridDiv.appendChild(document.createElement('div'));
column.classList.add('columns');
column.setAttribute('style', 'Display: flex; width: ' + gridDimension + 'px; height: ' + gridDimension / dimension + 'px;');
for(let i = 0; i< dimension; i++){
let pixel = column.appendChild(document.createElement('div'));
pixel.classList.add('pixels');
pixel.setAttribute('style', 'height: ' + gridDimension / dimension + 'px; width: ' + gridDimension / dimension + 'px;');
}
}
game();
}
//Prompt when dimension button is clicked to setup the Grid
function dimensionClicked(){
var tempDimensionGrid = parseInt(prompt('Dimension grid needed (MAX 100):'));
if(isNaN(tempDimensionGrid) ||
tempDimensionGrid < 2 ||
tempDimensionGrid > 100){
alert('A number between 2 and 100 must be entered!');
} else{
setGrid(tempDimensionGrid);
}
}