-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
103 lines (84 loc) · 2.21 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
const canvas = document.getElementById("drawing-canvas");
const increaseBtn = document.querySelector("#increase");
const decreaseBtn = document.querySelector("#decrease");
const strokeThickness = document.querySelector("#size");
const colorBtn = document.querySelector("#color");
const clearBtn = document.querySelector("#clear");
/*
******************* Canvas getContext() Method *********************
https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext#:~:text=getContext()%20method%20returns%20a,to%20a%20different%20context%20mode.
******************* CanvasRenderingContext2D ********************
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
*/
const ctx = canvas.getContext("2d");
let size = 10;
let isPressed = false;
let color = "black";
let x = undefined;
let y = undefined;
canvas.addEventListener("mousedown", function (e) {
isPressed = true;
x = e.offsetX;
y = e.offsetY;
});
canvas.addEventListener("mouseup", function (e) {
isPressed = false;
x = undefined;
y = undefined;
});
canvas.addEventListener("mousemove", function (e) {
if (isPressed) {
const x2 = e.offsetX;
const y2 = e.offsetY;
drawCircle(x2, y2);
drawLine(x, y, x2, y2);
x = x2;
y = y2;
}
});
// Drawing Lines
function drawLine(x1, y1, x2, y2) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.strokeStyle = color;
ctx.lineWidth = size * 2;
ctx.stroke();
}
// Drawing Circles
function drawCircle(x, y) {
ctx.beginPath();
ctx.arc(x, y, size, 0, Math.PI * 2);
ctx.fillStyle = color;
ctx.fill();
}
// Increase Btn
increaseBtn.addEventListener("click", function () {
size += 1;
if (size > 50) {
size = 50;
}
updateSize();
});
// Decrease Btn
decreaseBtn.addEventListener("click", function () {
size -= 1;
if (size < 1) {
size = 1;
}
updateSize();
});
// Color Btn
colorBtn.addEventListener("change", function (e) {
color = e.target.value;
console.log(e.target.value);
console.log(color);
});
// Clear Btn
clearBtn.addEventListener("click", function () {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});
// Updating the Stroke Width Dynamically
function updateSize() {
strokeThickness.innerText = size;
}