-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
110 lines (81 loc) · 2.96 KB
/
script.js
File metadata and controls
110 lines (81 loc) · 2.96 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
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
const canvas = document.querySelector(".canvas");
const inputSize = document.querySelector(".input-size");
const inputColor = document.querySelector(".input-color");
const usedColors = document.querySelector(".used-colors");
const buttonSave = document.querySelector(".button-save");
const colResize = document.querySelector(".resize");
const main = document.querySelector("main");
const MIN_CANVAS_SIZE = 4;
let isPainting = false;
let isResizing = false;
const createElement = (tag, className = "") => {
const element = document.createElement(tag);
element.className = className;
return element;
};
const setPixelColor = (pixel) => {
pixel.style.backgroundColor = inputColor.value;
};
const createPixel = () => {
const pixel = createElement("div", "pixel");
pixel.addEventListener("mousedown", () => setPixelColor(pixel));
pixel.addEventListener("mouseover", () => {
if (isPainting) setPixelColor(pixel);
});
return pixel;
};
const loadCanvas = () => {
const length = inputSize.value;
canvas.innerHTML = "";
for (let i = 0; i < length; i += 1) {
const row = createElement("div", "row");
for (let j = 0; j < length; j += 1) {
row.append(createPixel());
}
canvas.append(row);
}
};
const updateCanvasSize = () => {
if (inputSize.value >= MIN_CANVAS_SIZE) {
loadCanvas();
}
};
const changeColor = () => {
const button = createElement("button", "button-color");
const currentColor = inputColor.value;
button.style.backgroundColor = currentColor;
button.setAttribute("data-color", currentColor);
button.addEventListener("click", () => (inputColor.value = currentColor));
const savedColors = Array.from(usedColors.children);
const check = (btn) => btn.getAttribute("data-color") != currentColor;
if (savedColors.every(check)) {
usedColors.append(button);
}
};
const resizeCanvas = (cursorPositionX) => {
if (!isResizing) return;
const canvasOffset = canvas.getBoundingClientRect().left;
const width = `${cursorPositionX - canvasOffset - 20}px`;
canvas.style.maxWidth = width;
colResize.style.height = width;
};
const saveCanvas = () => {
html2canvas(canvas, {
onrendered: (image) => {
const img = image.toDataURL("image/png");
const link = createElement("a");
link.href = img;
link.download = "pixelart.png";
link.click();
},
});
};
canvas.addEventListener("mousedown", () => (isPainting = true));
canvas.addEventListener("mouseup", () => (isPainting = false));
inputSize.addEventListener("change", updateCanvasSize);
inputColor.addEventListener("change", changeColor);
colResize.addEventListener("mousedown", () => (isResizing = true));
main.addEventListener("mouseup", () => (isResizing = false));
main.addEventListener("mousemove", ({ clientX }) => resizeCanvas(clientX));
buttonSave.addEventListener("click", saveCanvas);
loadCanvas();