-
Notifications
You must be signed in to change notification settings - Fork 2
/
drawingTool.js
83 lines (69 loc) · 2.32 KB
/
drawingTool.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
let pen = document.querySelector("#pen");
let eraser = document.querySelector("#eraser");
let penOptions = pen.querySelector(".tool-options");
let eraserOptions = eraser.querySelector(".tool-options");
let penSize = penOptions.querySelector("#pensize");
let eraserSize = eraserOptions.querySelector("#erasersize");
let penColors = penOptions.querySelectorAll(".pen-colors div");
let currentPenSize = 1;
let currentPenColor = "black";
let currentEraserSize = 1;
penSize.addEventListener("change", function () {
// handle pen size
let penSizeValue = penSize.value;
// console.log(penSizeValue);
// pensize set hoga
currentPenSize = penSizeValue;
ctx.lineWidth = currentPenSize;
});
eraserSize.addEventListener("change", function () {
let eraserSizeValue = eraserSize.value;
currentEraserSize = eraserSizeValue;
ctx.lineWidth = currentEraserSize;
});
for (let i = 0; i < penColors.length; i++) {
penColors[i].addEventListener("click", function (e) {
let penColor = e.target.className;
currentPenColor = penColor;
ctx.strokeStyle = currentPenColor; // for lines
});
}
pen.addEventListener("click", function () {
if (pen.classList.contains("active-tool")) {
// pen already active
if (penOptions.classList.contains("hide")) {
penOptions.classList.remove("hide"); // remove hide class from penOptions
} else {
penOptions.classList.add("hide");
}
} else {
// pen is not active
// make pen active
eraser.classList.remove("active-tool");
eraser.classList.add("fade");
eraserOptions.classList.add("hide");
pen.classList.remove("fade");
pen.classList.add("active-tool");
ctx.lineWidth = currentPenSize;
ctx.strokeStyle = currentPenColor;
}
});
eraser.addEventListener("click", function () {
if (eraser.classList.contains("active-tool")) {
// eraser already active
if (eraserOptions.classList.contains("hide")) {
eraserOptions.classList.remove("hide"); // remove hide class from penOptions
} else {
eraserOptions.classList.add("hide");
}
} else {
// eraser not active
pen.classList.remove("active-tool");
pen.classList.add("fade");
penOptions.classList.add("hide");
eraser.classList.add("active-tool");
eraser.classList.remove("fade");
ctx.strokeStyle = "white";
ctx.lineWidth = currentEraserSize;
}
});