-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
135 lines (114 loc) · 4.56 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
134
135
const board = document.querySelector(".board");
const toolsMenu = document.querySelector(".toolsMenu");
const sizeSlider = document.querySelector(".scale");
const buttons = Array.from(toolsMenu.children);
const colorPicker = document.querySelector("#colorPicker");
let boardSize = 50;
let clickAction;
let currentColor = "black";
function changeCurrentTool(newTool) {
buttons.forEach((button) => {
button.classList.remove("toggledButton");
})
if (newTool.className === "colorSelector") {
clickAction = (pixel) => {
if (pixel.style.backgroundColor === "") return;
currentColor = pixel.style.backgroundColor;
colorPicker.parentElement.style.backgroundColor = currentColor;
newTool.classList.remove("toggledButton");
}
}if (newTool.className === "paintBrush") {
clickAction = (pixel) => {
pixel.style.backgroundColor = currentColor;
}
}else if (newTool.className === "rainbowBrush") {
clickAction = (pixel) => {
const red = Math.floor(Math.random() * 256);
const green = Math.floor(Math.random() * 256);
const blue = Math.floor(Math.random() * 256);
pixel.style.backgroundColor = `rgb(${red}, ${green}, ${blue})`;
}
}else if (newTool.className === "fillTool") {
clickAction = (pixel) => {
let initialColor = pixel.style.backgroundColor;
clickActionHelper(pixel);
function clickActionHelper(pixel) {
function getPixelofID(pixelID) {
return document.querySelector(`#pixel-${pixelID}`);
}
function checkValidFill(pixelID) {
const pixel = getPixelofID(pixelID);
if (pixel === null || pixel.style.backgroundColor !== initialColor) {
return false;
}
return true;
}
const pixelID = parseInt(pixel.id.replace("pixel-", ""));
pixel.style.backgroundColor = currentColor;
console.log(pixelID);
//left right
if (checkValidFill(pixelID + boardSize)) clickActionHelper(getPixelofID(pixelID + boardSize));
if (checkValidFill(pixelID - boardSize)) clickActionHelper(getPixelofID(pixelID - boardSize));
//top bottom
if (checkValidFill(pixelID + 1) && pixel.parentElement === getPixelofID(pixelID+1).parentElement) clickActionHelper(getPixelofID(pixelID + 1));
if (checkValidFill(pixelID - 1) && pixel.parentElement === getPixelofID(pixelID+1).parentElement) clickActionHelper(getPixelofID(pixelID - 1));
}
}
}else if (newTool.className === "eraser") {
clickAction = (pixel) => {
pixel.style.backgroundColor = "";
}
}
newTool.classList.add("toggledButton");
}
//registers when to paint
function setupPaintEventListeners() {
const pixels = document.querySelectorAll(".pixel");
let isMouseDown = false;
pixels.forEach((pixel) => {
pixel.addEventListener("mouseenter", () => {
if (isMouseDown) {
clickAction(pixel);
}
})
pixel.addEventListener("click", () => {
clickAction(pixel);
})
pixel.addEventListener("mousedown", () => {
isMouseDown = true;
})
pixel.addEventListener("mouseup", () => {
isMouseDown = false;
})
})
}
function createNewBoard(dimensions) {
boardSize = dimensions;
while (board.firstChild) board.removeChild(board.firstChild);
let pixelNum = 0;
for (let i = 0; i < dimensions; i++) {
const boardRow = document.createElement("div");
boardRow.classList.add("boardRow");
for (let j = 0; j < dimensions; j++) {
const pixel = document.createElement("div");
pixel.classList.add("pixel");
pixel.setAttribute("id", `pixel-${++pixelNum}`);
boardRow.appendChild(pixel);
}
board.appendChild(boardRow);
}
setupPaintEventListeners();
}
//usage
createNewBoard(50);
sizeSlider.addEventListener('mouseup', () => {
createNewBoard(sizeSlider.value);
});
buttons.forEach((button) =>
button.addEventListener("click", () => {
changeCurrentTool(button);
}))
colorPicker.addEventListener("input", () => {
colorPicker.parentElement.style.backgroundColor = colorPicker.value;
currentColor = colorPicker.value;
});