-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesign.js
More file actions
47 lines (43 loc) · 1.4 KB
/
Copy pathdesign.js
File metadata and controls
47 lines (43 loc) · 1.4 KB
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
// Selecting necessary DOM Elements
const pixelCanvas = document.querySelector("#pixelCanvas");
const gridWidth = document.querySelector("#inputWidth");
const gridHeight = document.querySelector("#inputHeight");
const colorPicker = document.querySelector("#colorPicker");
const submitBtn = document.querySelector("#submit-btn");
let isDrawing = false;
//Add Event to the button
submitBtn.addEventListener("click", myGrid);
//Creating Grid
function myGrid(event) {
event.preventDefault();
//Clearing the previous canvas when the user set another width & height
pixelCanvas.innerHTML = "";
for (let row = 0; row < gridHeight.value; row++) {
const gridRow = document.createElement("tr");
for (let col = 0; col < gridWidth.value; col++) {
const gridCol = document.createElement("td");
gridCol.classList.add("pixel");
gridRow.appendChild(gridCol);
}
pixelCanvas.appendChild(gridRow);
}
//Add Event to the created grid
pixelCanvas.addEventListener("mousedown", function () {
isDrawing = true;
});
pixelCanvas.addEventListener("mousemove", draw);
}
// function to draw pixels
function draw(event) {
if (isDrawing) {
event.stopPropagation();
const color = colorPicker.value;
event.target.style.backgroundColor = color;
}
}
// Stop drawing once the user stop using mouse
window.addEventListener("mouseup", function () {
if (isDrawing) {
isDrawing = false;
}
});