forked from enenH/enenH.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CanvasUtils.js
136 lines (121 loc) · 4.66 KB
/
CanvasUtils.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class CanvasUtils {
constructor(canvas) {
this.canvas = canvas;
this.context = canvas.getContext("2d");
}
clear() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
drawLine(x1, y1, x2, y2, color, width = 1) {
this.context.beginPath();
this.context.moveTo(x1, y1);
this.context.lineTo(x2, y2);
this.context.strokeStyle = color;
this.context.lineWidth = width;
this.context.stroke();
}
drawRect(x, y, x1, y1, color, width = 1) {
this.context.strokeStyle = color;
this.context.lineWidth = width;
this.context.strokeRect(x, y, x1 - x, y1 - y);
}
drawRectFilled(x, y, x1, y1, color) {
this.context.fillStyle = color;
this.context.fillRect(x, y, x1 - x, y1 - y);
}
drawRectRounded(x, y, x1, y1, radius, color, width = 1) {
this.context.save();
this.context.beginPath();
this.context.moveTo(x + radius, y);
this.context.arcTo(x1, y, x1, y1, radius);
this.context.arcTo(x1, y1, x, y1, radius);
this.context.arcTo(x, y1, x, y, radius);
this.context.arcTo(x, y, x1, y, radius);
this.context.closePath();
this.context.strokeStyle = color;
this.context.lineWidth = width;
this.context.stroke();
this.context.restore();
}
drawRectRoundedFilled(x, y, x1, y1, radius, color) {
this.context.save();
this.context.beginPath();
this.context.moveTo(x + radius, y);
this.context.arcTo(x1, y, x1, y1, radius);
this.context.arcTo(x1, y1, x, y1, radius);
this.context.arcTo(x, y1, x, y, radius);
this.context.arcTo(x, y, x1, y, radius);
this.context.closePath();
this.context.fillStyle = color;
this.context.fill();
this.context.restore();
}
drawText(text, x, y, color, isCenter = true, outline = true, size = 12, font = 'Arial') {
this.context.font = `${size}px ${font}`;
this.context.textAlign = isCenter ? 'center' : 'left';
this.context.textBaseline = 'middle';
if (outline) {
this.context.strokeStyle = 'black';
this.context.strokeText(text, x, y);
}
this.context.fillStyle = color;
this.context.fillText(text, x, y);
}
drawImage(image, x, y, x1, y1) {
this.context.drawImage(image, x, y, x1 - x, y1 - y);
}
drawImageRounded(image, x, y, x1, y1, radius) {
this.context.save();
this.context.beginPath();
this.context.moveTo(x + radius, y);
this.context.arcTo(x1, y, x1, y1, radius);
this.context.arcTo(x1, y1, x, y1, radius);
this.context.arcTo(x, y1, x, y, radius);
this.context.arcTo(x, y, x1, y, radius);
this.context.closePath();
this.context.clip();
this.context.drawImage(image, x, y, x1 - x, y1 - y);
this.context.restore();
}
drawImageCircle(image, centerX, centerY, radius) {
this.context.save();
this.context.beginPath();
this.context.arc(centerX, centerY, radius, 0, 2 * Math.PI);
this.context.closePath();
this.context.clip();
this.context.drawImage(image, centerX - radius, centerY - radius, radius * 2, radius * 2);
this.context.restore();
}
drawCircle(x, y, radius, color, width = 1) {
this.context.beginPath();
this.context.arc(x, y, radius, 0, 2 * Math.PI);
this.context.strokeStyle = color;
this.context.lineWidth = width;
this.context.stroke();
}
drawCircleFilled(x, y, radius, color) {
this.context.beginPath();
this.context.arc(x, y, radius, 0, 2 * Math.PI);
this.context.fillStyle = color;
this.context.fill();
}
drawLines(points, color, width = 1) {
this.context.beginPath();
this.context.moveTo(points[0][0], points[0][1]);
for (let i = 1; i < points.length; i++) {
this.context.lineTo(points[i][0], points[i][1]);
}
this.context.strokeStyle = color;
this.context.lineWidth = width;
this.context.stroke();
}
drawArcAnimated(x, y, radius, color, width = 1, speed = 1) {
const time = new Date();
const angle = (time.getMilliseconds() / 1000 + time.getSeconds()) * speed;
this.context.beginPath();
this.context.arc(x, y, radius, angle, angle + 1.5 * Math.PI);
this.context.strokeStyle = color;
this.context.lineWidth = width;
this.context.stroke();
}
}