-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
133 lines (116 loc) · 3.74 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
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
var grid = document.querySelector(".grid")
function makeGrid(grid_size){
if(grid_size>30){
alert("Website will respond slowly when grid size is greater than 30")
}
const cssRules = `
padding: 10px;
display: grid;
grid-template-columns: repeat(${grid_size}, auto);
grid-template-rows: repeat(${grid_size}, auto);
height: 400px;
width: 400px;
background-color: #B7C9F2;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
`
grid.setAttribute('style', cssRules);
makePixels(grid_size)
}
function makePixels(gridSize){
grid.innerHTML = null
const cssRules = `
background-color: white;
`
for (let index = 1; index <=gridSize*gridSize; index++) {
grid.innerHTML += "<div class='pixel'></div>"
}
document.querySelectorAll('.pixel').forEach((el) => {
el.setAttribute('style',cssRules)
el.classList.add("toggleGrid")
})
}
function handleDrawing(color){
function draw(){
var cssRules = ``
if(color === 'rainbow'){
cssRules = `background-color: ${getRandomColor()};`
}
else {
cssRules = `background-color: ${color};`
}
this.setAttribute('style',cssRules)
}
function stopDrawing(){
document.querySelectorAll('.pixel').forEach((el) => {
el.removeEventListener('mouseover', draw)
})
}
grid.addEventListener("mousedown", function(event) {
draw.call(event.target)
document.querySelectorAll('.pixel').forEach((el) => {
el.addEventListener('mouseover', draw)
})
grid.addEventListener("mouseup", function(event){
stopDrawing.call(event.target)
})
})
}
function clear(){
document.querySelector(".clear").addEventListener("click", function(){
const cssRules = `background-color: white;
`
document.querySelectorAll('.pixel').forEach((el) => {
el.setAttribute('style',cssRules)
})
})
}
function toggleGrid(){
document.querySelectorAll('.pixel').forEach((el) => {
el.classList.toggle("toggleGrid")
})
}
document.querySelector(".gridsize").addEventListener('click', function(){
var size = document.getElementById("gridsize").value;
if (size === '') size = 16
makeGrid(size)
});
document.querySelector(".gridStatus").addEventListener("click", function(){
toggleGrid()
})
var eraser = document.querySelector(".eraser")
eraser.addEventListener("click", function(){
handleDrawing('white')
eraser.classList.add('pressed')
pencil.classList.remove('pressed')
rainbow.classList.remove('pressed')
})
var pencil = document.querySelector(".pencil")
pencil.addEventListener("click", function(){
handleDrawing('black')
pencil.classList.add('pressed')
rainbow.classList.remove('pressed')
eraser.classList.remove('pressed')
})
var rainbow = document.querySelector(".rainbow")
rainbow.addEventListener("click", function(){
handleDrawing('rainbow')
rainbow.classList.add('pressed')
pencil.classList.remove('pressed')
eraser.classList.remove('pressed')
})
document.querySelector("#favcolor").addEventListener("input", function(event){
handleDrawing(event.target.value)
document.documentElement.style.setProperty('--color', event.target.value);
rainbow.classList.remove('pressed')
pencil.classList.remove('pressed')
eraser.classList.remove('pressed')
})
function getRandomColor() {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return `rgb(${r}, ${g}, ${b})`;
}
makeGrid(16)
handleDrawing('black')
clear()