-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
64 lines (50 loc) · 1.61 KB
/
app.js
File metadata and controls
64 lines (50 loc) · 1.61 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
class CanvasDrawing {
constructor(drawingPanel) {
this.canvas = document.querySelector(".canvas");
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
this.context = this.canvas.getContext("2d");
this.context.lineCap = "round";
this.context.lineJoin = "round";
this.setLineWidth(1);
this.setLineColor("#000");
this.drawing = false;
this.drawingPanel = drawingPanel;
this.init();
}
setLineWidth = (width) => {
this.context.lineWidth = width;
};
setLineColor = (color) => {
this.context.strokeStyle = color;
};
init() {
this.addListeners();
}
addListeners() {
const canvas = this.canvas;
canvas.addEventListener("mousedown", this.startDrawing);
canvas.addEventListener("mousemove", this.draw);
canvas.addEventListener("mouseup", this.stopDrawing);
canvas.addEventListener("mouseout", this.stopDrawing);
this.drawingPanel.addPanelListeners(this.setLineColor, this.setLineWidth);
}
startDrawing = (e) => {
this.drawing = true;
this.lastX = e.offsetX;
this.lastY = e.offsetY;
};
stopDrawing = (e) => {
this.drawing = false;
};
draw = (e) => {
if (!this.drawing) return;
const context = this.context;
context.beginPath();
context.moveTo(this.lastX, this.lastY);
context.lineTo(e.offsetX, e.offsetY);
context.stroke();
[this.lastX, this.lastY] = [e.offsetX, e.offsetY];
};
}
const canvasDrawing = new CanvasDrawing(drawingPanel);