-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
78 lines (59 loc) · 1.57 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
const grid = document.querySelector('.grid');
const clearBtn = document.getElementById('clear');
const randomBtn = document.getElementById('random');
const grayBtn = document.getElementById('grey');
const eraserBtn = document.getElementById('eraser');
var size = 16;
var mode = 'gray';
var color = 'black';
makeGrid(size);
function setEraser(){
color = 'white'
}
function setGrey(){
color = 'black'
}
function setRes(){
size = prompt("Enter the number of pixels");
clearGrid();
}
function setRandom(){
color = 'random'
}
function setSize(){
size = prompt("Enter the size of grid");
makeGrid(size);
}
function clearGrid(){
grid.innerHTML = ''
makeGrid(size)
}
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function changeColor(e){
if(color == 'random'){
e.target.style.backgroundColor = getRandomColor()
}
else{
e.target.style.backgroundColor = color
}
}
function makeGrid(size){
for(let i = 0; i < size; i++ ){
var cellRow = document.createElement('div')
cellRow.className = 'cellRow'
for(let j = 0; j < size; j++ ){
var cell = document.createElement('div')
cell.className = 'cell'
cell.addEventListener("mouseover",changeColor)
cellRow.appendChild(cell)
}
grid.appendChild(cellRow)
}
}