-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
120 lines (100 loc) · 3.25 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
const CONTAINER_DIMENSIONS = 480;
let canvasDimensions = 16;
let brushColor = "rgb(0, 0, 0)";
let rainbow = false;
let holding = false;
let currentSelected = document.querySelector("#normal");
const colorPicker = document.querySelector("#color-picker");
const tools = document.querySelector("#tools");
const container = document.querySelector("#container");
container.style.width = container.style.height = `${CONTAINER_DIMENSIONS}px`;
startSite();
function startSite () {
createGrid(canvasDimensions);
const delay = 0.2;
const toolList = Array.from(tools.children);
for (let i = 0; i < toolList.length; i++) {
toolList[i].classList.add("fadein");
toolList[i].style.animationDelay = `${delay * (i + 2)}s`;
}
}
function createGrid(dimensions) {
container.textContent = "";
for (let i = 0; i < dimensions; i++) {
const row = document.createElement("div");
row.classList.add("row");
for (let j = 0; j < dimensions; j++) {
const pixel = document.createElement("div");
pixel.classList.add("pixel");
pixel.style.width = pixel.style.height = `${CONTAINER_DIMENSIONS / dimensions}px`;
pixel.style.backgroundColor = "rgb(255, 255, 255)";
row.appendChild(pixel);
}
container.appendChild(row);
}
}
function draw(target) {
if (target.classList.contains("pixel") && holding) {
if (rainbow) {
target.style.backgroundColor = randomRGB();
}
else {
target.style.backgroundColor = brushColor;
}
}
}
function select(target) {
rainbow = false;
currentSelected.classList.remove("selected");
currentSelected = target;
currentSelected.classList.add("selected");
}
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function randomRGB() {
return `rgb(${randomInt(0, 255)}, ${randomInt(0, 255)}, ${randomInt(0, 255)})`;
}
container.addEventListener("mousedown", (event) => {
holding = true;
draw(event.target);
});
container.addEventListener("mouseup", () => {
holding = false;
});
container.addEventListener("mouseover", (event) => {
draw(event.target);
});
colorPicker.addEventListener("input", () => {
brushColor = colorPicker.value;
colorPicker.style.backgroundColor = colorPicker.value;
});
tools.addEventListener("click", (event) => {
switch(event.target.id) {
case "normal":
select(event.target);
brushColor = colorPicker.value;
break;
case "rainbow":
select(event.target);
rainbow = true;
break;
case "eraser":
select(event.target);
brushColor = "rgb(255, 255, 255)";
break;
case "clear":
createGrid(canvasDimensions);
break;
case "size":
let input = prompt("Enter the new size (max 100):");
while (isNaN(input) || input < 1 || input > 100) {
if (input === null) break;
input = prompt("Invalid! Enter the new size (max 100):");
}
if (input === null) break;
canvasDimensions = input;
createGrid(canvasDimensions);
break;
}
});