-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.js
50 lines (50 loc) · 1.5 KB
/
draw.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
let boardTop = canvasBoard.getBoundingClientRect().top; // top = vertical distance from the left top corner to the canvasboard
let ix, iy, fx, fy
let drawingMode = false;
body.addEventListener("mousedown", function (e) {
if (curTool == "pencil" || curTool == "eraser") {
drawingMode = true;
tool.beginPath();
tool.moveTo(e.clientX, e.clientY - boardTop);
}
else if (curTool == 'rect' || curTool == "line") {
ix = e.clientX;
iy = e.clientY - boardTop;
}
})
body.addEventListener("mouseup", function (e) {
if (curTool == "pencil" || curTool == "eraser") {
drawingMode = false;
}
else if (curTool == "rect" || curTool == "line") {
// line and rect
fx = e.clientX;
fy = e.clientY - boardTop;
let width = fx - ix;
let height = fy - iy;
if (curTool == "rect") {
tool.strokeRect(ix, iy, width, height);
}
else if (curTool == "line") {
tool.beginPath();
tool.moveTo(ix, iy);
tool.lineTo(fx, fy);
tool.stroke();
}
}
})
body.addEventListener("mousemove", function (e) {
if (drawingMode == false) {
return;
}
else {
if (curTool == "pencil" || curTool == "eraser") {
fx = e.clientX;
fy = e.clientY - boardTop;
tool.lineTo(fx, fy);
tool.stroke();
ix = fx;
iy = fy;
}
}
})