-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
121 lines (100 loc) · 2.84 KB
/
script.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
113
114
115
116
117
118
119
120
121
const canvasProps = {
width: window.innerWidth,
height: window.innerHeight,
center: {
x: window.innerWidth / 2,
y: window.innerHeight / 2,
},
};
const stageProps = {
width: 600,
height: 480,
left: canvasProps.center.x - 600 / 2,
top: canvasProps.center.y - 480 / 2,
};
myCanvas.width = canvasProps.width;
myCanvas.height = canvasProps.height;
const ctx = myCanvas.getContext("2d");
clearCanvas();
const shapes = [];
let currentRoute = [];
let currentShape = null;
let currentRectangle = { type: "rect" };
// To draw a rectangle
const downCallBackForRect = function (e) {
const mousePos = {
x: e.offsetX,
y: e.offsetY,
};
currentShape = new Rect(mousePos);
const moveCallBack = function (e) {
const mousePos = {
x: e.offsetX,
y: e.offsetY,
};
currentShape.setCorner2(mousePos);
clearCanvas();
drawShapes([...shapes, currentShape]);
};
const upCallBack = function () {
myCanvas.removeEventListener("pointermove", moveCallBack);
myCanvas.removeEventListener("pointerup", upCallBack);
shapes.push(currentShape);
};
myCanvas.addEventListener("pointermove", moveCallBack);
myCanvas.addEventListener("pointerup", upCallBack);
};
// To draw a line (Freeform)
const downCallBackForFreeform = function (e) {
const mousePos = {
x: e.offsetX,
y: e.offsetY,
};
currentShape = new Freeform([mousePos]);
const moveCallBack = function (e) {
const mousePos = {
x: e.offsetX,
y: e.offsetY,
};
currentShape.addPoint(mousePos);
clearCanvas();
drawShapes([...shapes, currentShape]);
};
const upCallBack = function () {
myCanvas.removeEventListener("pointermove", moveCallBack);
myCanvas.removeEventListener("pointerup", upCallBack);
shapes.push(currentShape);
};
myCanvas.addEventListener("pointermove", moveCallBack);
myCanvas.addEventListener("pointerup", upCallBack);
};
myCanvas.addEventListener("pointerdown", downCallBackForFreeform);
function changeTool(tool) {
myCanvas.removeEventListener("pointerdown", downCallBackForRect);
myCanvas.removeEventListener("pointerdown", downCallBackForFreeform);
switch (tool) {
case "rect":
myCanvas.addEventListener("pointerdown", downCallBackForRect);
break;
case "freeform":
myCanvas.addEventListener("pointerdown", downCallBackForFreeform);
break;
}
}
function drawShapes(shapes) {
for (const shape of shapes) {
shape.draw(ctx);
}
}
function clearCanvas() {
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
ctx.fillStyle = "gray";
ctx.fillRect(0, 0, myCanvas.width, myCanvas.height);
ctx.fillStyle = "white";
ctx.fillRect(
stageProps.left,
stageProps.top,
stageProps.width,
stageProps.height
);
}