Skip to content

Commit b253f42

Browse files
committed
Adding Day AsmrProg-YT#1
0 parents  commit b253f42

File tree

5 files changed

+256
-0
lines changed

5 files changed

+256
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_STORE
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<link rel="stylesheet" href="style.css">
9+
<title>#1 - Pixel Art Generator | AsmrProg</title>
10+
</head>
11+
12+
<body>
13+
14+
<div class="wrapper">
15+
<div class="options">
16+
<div class="opt-wrapper">
17+
<div class="slider">
18+
<label for="width-range">Grid Width</label>
19+
<input type="range" id="width-range" min="1" max="35">
20+
<span id="width-value">00</span>
21+
</div>
22+
<div class="slider">
23+
<label for="height-range">Grid Height</label>
24+
<input type="range" id="height-range" min="1" max="35">
25+
<span id="height-value">00</span>
26+
</div>
27+
</div>
28+
29+
<div class="opt-wrapper">
30+
<button id="submit-grid">Create Grid</button>
31+
<button id="clear-grid">Clear Grid</button>
32+
<input type="color" id="color-input">
33+
<button id="erase-btn">Erase</button>
34+
<button id="paint-btn">Paint</button>
35+
</div>
36+
37+
</div>
38+
<div class="container"></div>
39+
</div>
40+
41+
42+
43+
44+
<script src="index.js"></script>
45+
</body>
46+
47+
</html>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
let container = document.querySelector(".container");
2+
let gridButton = document.getElementById("submit-grid");
3+
let clearGridButton = document.getElementById("clear-grid");
4+
let gridWidth = document.getElementById("width-range");
5+
let gridHeight = document.getElementById("height-range");
6+
let colorButton = document.getElementById("color-input");
7+
let eraseBtn = document.getElementById("erase-btn");
8+
let paintBtn = document.getElementById("paint-btn");
9+
let widthValue = document.getElementById("width-value");
10+
let heightValue = document.getElementById("height-value");
11+
12+
let events = {
13+
mouse: {
14+
down: "mousedown",
15+
move: "mousemove",
16+
up: "mouseup"
17+
},
18+
touch: {
19+
down: "touchstart",
20+
mobe: "touchmove",
21+
up: "touchend",
22+
},
23+
};
24+
25+
let deviceType = "";
26+
27+
let draw = false;
28+
let erase = false;
29+
30+
const isTouchDevice = () => {
31+
try {
32+
document.createEvent("TouchEvent");
33+
deviceType = "touch";
34+
return true;
35+
} catch (e) {
36+
deviceType = "mouse";
37+
return false;
38+
}
39+
};
40+
41+
isTouchDevice();
42+
43+
gridButton.addEventListener("click", () => {
44+
container.innerHTML = "";
45+
let count = 0;
46+
for (let i = 0; i < gridHeight.value; i++) {
47+
count += 2;
48+
let div = document.createElement("div");
49+
div.classList.add("gridRow");
50+
51+
for (let j = 0; j < gridWidth.value; j++) {
52+
count += 2;
53+
let col = document.createElement("div");
54+
col.classList.add("gridCol");
55+
col.setAttribute("id", `gridCol${count}`);
56+
col.addEventListener(events[deviceType].down, () => {
57+
draw = true;
58+
if (erase) {
59+
col.style.backgroundColor = "transparent";
60+
} else {
61+
col.style.backgroundColor = colorButton.value;
62+
}
63+
});
64+
65+
col.addEventListener(events[deviceType].move, (e) => {
66+
let elementId = document.elementFromPoint(
67+
!isTouchDevice() ? e.clientX : e.touches[0].clientX,
68+
!isTouchDevice() ? e.clientY : e.touches[0].clientY,
69+
).id;
70+
checker(elementId);
71+
});
72+
73+
col.addEventListener(events[deviceType].up, () => {
74+
draw = false;
75+
});
76+
77+
div.appendChild(col);
78+
79+
}
80+
81+
container.appendChild(div);
82+
83+
}
84+
});
85+
86+
function checker(elementId) {
87+
let gridColumns = document.querySelectorAll(".gridCol");
88+
gridColumns.forEach((element) => {
89+
if (elementId == element.id) {
90+
if (draw && !erase) {
91+
element.style.backgroundColor = colorButton.value;
92+
} else if (draw && erase) {
93+
element.style.backgroundColor = "transparent";
94+
}
95+
}
96+
});
97+
}
98+
99+
clearGridButton.addEventListener("click", () => {
100+
container.innerHTML = "";
101+
});
102+
103+
eraseBtn.addEventListener("click", () => {
104+
erase = true;
105+
});
106+
107+
paintBtn.addEventListener("click", () => {
108+
erase = false;
109+
});
110+
111+
gridWidth.addEventListener("input", () => {
112+
widthValue.innerHTML = gridWidth.value < 9 ? `0${gridWidth.value}` : gridWidth.value;
113+
});
114+
115+
gridHeight.addEventListener("input", () => {
116+
heightValue.innerHTML = gridHeight.value < 9 ? `0${gridHeight.value}` : gridHeight.value;
117+
});
118+
119+
window.onload = () => {
120+
gridHeight.value = 0;
121+
gridWidth.value = 0;
122+
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
*{
2+
padding: 0;
3+
margin: 0;
4+
box-sizing: border-box;
5+
font-family: monospace;
6+
}
7+
8+
body{
9+
background-color: #43a047;
10+
}
11+
12+
.wrapper{
13+
background-color: #fff;
14+
width: 80vmin;
15+
position: absolute;
16+
transform: translate(-50%, -50%);
17+
top: 50%;
18+
left: 50%;
19+
padding: 40px 20px;
20+
border-radius: 8px;
21+
}
22+
23+
label{
24+
display: block;
25+
}
26+
27+
span{
28+
position: relative;
29+
font-size: 22px;
30+
bottom: -1px;
31+
}
32+
33+
.opt-wrapper{
34+
display: flex;
35+
justify-content: space-between;
36+
margin-bottom: 20px;
37+
gap: 10px;
38+
}
39+
40+
button{
41+
background-color: #43a047;
42+
border: none;
43+
border-radius: 5px;
44+
padding: 5px;
45+
color: #fff;
46+
}
47+
48+
input[type="color"]{
49+
-webkit-appearance: none;
50+
-moz-appearance: none;
51+
appearance: none;
52+
background-color: transparent;
53+
width: 70px;
54+
height: 40px;
55+
border: none;
56+
cursor: pointer;
57+
}
58+
59+
input[type="color"]::-webkit-color-swatch{
60+
border-radius: 8px;
61+
border: 4px solid #000;
62+
}
63+
input[type="color"]::-moz-color-swatch{
64+
border-radius: 8px;
65+
border: 4px solid #000;
66+
}
67+
68+
.gridCol{
69+
height: 1em;
70+
width: 1em;
71+
border: 1px solid #ddd;
72+
}
73+
74+
.gridRow{
75+
display: flex;
76+
}
77+
78+
@media only screen and (max-width: 768px){
79+
.gridCol{
80+
height: 0.8em;
81+
width: 0.8em;
82+
}
83+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Collection of AsmrProg Youtube Channel 100 days of JavaScript Coding Codes!
2+
3+
Find Video's on : https://Youtube.com/@AsmrProg

0 commit comments

Comments
 (0)