-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
112 lines (104 loc) · 3.74 KB
/
app.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
window.addEventListener("load", () => {
const colorPallette = document.querySelectorAll(".color-pallette div");
const brushes = document.querySelectorAll(".thickness div");
const eraser = document.querySelector(".eraser");
const rectangle = document.querySelector(".rectangle");
const fillBrush = document.querySelector(".fill-color");
let drawControl = { drawing: false, control: false };
let shapeControl = { shapeDraw: false, shapeDrawing: false };
let brushProperties = { brushColor: "#58b19f", brushSize: "25px" };
let shapeProperties = {
shapeX: null,
shapeY: null,
divPrevious: null,
fillColor: false
};
fillBrush.addEventListener("click", () => {
shapeProperties.fillColor = true;
shapeControl.shapeDraw = false;
drawControl.drawing = false;
});
rectangle.addEventListener("click", () => {
shapeControl.shapeDraw = true;
drawControl.drawing = false;
shapeProperties.fillColor = false;
});
eraser.addEventListener("click", () => {
brushProperties.brushColor = "#F8EFBA";
shapeControl.shapeDraw = false;
shapeProperties.fillColor = false;
});
brushes.forEach(brush => {
brush.addEventListener("click", () => {
brushProperties.brushSize = brush.className.split(" ")[0];
shapeControl.shapeDraw = false;
shapeProperties.fillColor = false;
});
});
colorPallette.forEach(color => {
color.addEventListener("click", () => {
brushProperties.brushColor = color.className.split(" ")[0];
shapeControl.shapeDraw = false;
});
});
document.addEventListener("keydown", event => {
if (event.ctrlKey) {
drawControl.control = true;
}
});
document.addEventListener("keyup", event => {
if (event.keyCode == 17) {
drawControl.control = false;
}
});
document.body.addEventListener("mousedown", event => {
if (!shapeControl.shapeDraw && !shapeProperties.fillColor) {
drawControl.drawing = true;
} else if (shapeControl.shapeDraw) {
shapeControl.shapeDrawing = true;
shapeProperties.shapeX = event.clientX;
shapeProperties.shapeY = event.clientY;
}
});
document.body.addEventListener("mouseup", () => {
drawControl.drawing = false;
shapeControl.shapeDrawing = false;
shapeProperties.divPrevious = null;
});
document.body.addEventListener("mousemove", function(event) {
if (drawControl.drawing) {
const div = document.createElement("div");
div.classList.add("drawing");
div.style.top = `${event.clientY}px`;
div.style.left = `${event.clientX}px`;
div.style.background = brushProperties.brushColor;
div.style.height = brushProperties.brushSize;
div.style.width = brushProperties.brushSize;
document.body.addEventListener("keydown", function(event) {
if (event.keyCode == 67 && drawControl.control) {
document.body.removeChild(div);
}
});
document.body.appendChild(div);
} else if (shapeControl.shapeDrawing) {
if (shapeProperties.divPrevious)
document.body.removeChild(shapeProperties.divPrevious);
const div = document.createElement("div");
div.classList.add("shape");
div.style.top = `${shapeProperties.shapeY}px`;
div.style.left = `${shapeProperties.shapeX}px`;
div.style.height = `${event.clientY - shapeProperties.shapeY}px`;
div.style.width = `${event.clientX - shapeProperties.shapeX}px`;
shapeProperties.divPrevious = div;
div.addEventListener("click", () => {
console.log("click");
});
document.body.appendChild(div);
document.body.addEventListener("keydown", function(event) {
if (event.keyCode == 67 && drawControl.control) {
document.body.removeChild(div);
}
});
}
});
});