kfb-view 2.1.12 → 2.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.idea/workspace.xml +99 -79
- package/example/index.js +6 -3
- package/lib/kfb-view.js +1 -1
- package/package.json +1 -1
- package/src/components/board/index.js +19 -2
- package/src/components/rotation/index.js +2 -0
- package/src/components/shape/index.js +41 -9
- package/src/model/label.model.js +8 -3
- package/src/tool/Arrow.js +2 -9
- package/src/tool/Bilateral.js +2 -9
- package/src/tool/Brush.js +14 -8
- package/src/tool/Combination.js +4 -3
- package/src/tool/Dot.js +3 -5
- package/src/tool/Ellipse.js +6 -14
- package/src/tool/Flag.js +2 -8
- package/src/tool/Font.js +2 -3
- package/src/tool/Line.js +2 -4
- package/src/tool/Polygon.js +6 -23
- package/src/tool/Rectangle.js +5 -14
- package/src/tool/Star.js +3 -6
- package/src/util/canvas.js +1 -4
- package/test.html +315 -0
package/package.json
CHANGED
|
@@ -140,7 +140,9 @@ export class Board extends ViewerCommon {
|
|
|
140
140
|
this.points = [point];
|
|
141
141
|
}
|
|
142
142
|
this.clearCanvas();
|
|
143
|
+
this[tool].startDraw();
|
|
143
144
|
this[tool].draw(this.imageToViewerElementPoints(this.points));
|
|
145
|
+
this[tool].endDraw();
|
|
144
146
|
this.$emit(EVENT_START_PAINTING, this.getPaintOptions());
|
|
145
147
|
}
|
|
146
148
|
|
|
@@ -166,7 +168,9 @@ export class Board extends ViewerCommon {
|
|
|
166
168
|
this.points = [point, point];
|
|
167
169
|
}
|
|
168
170
|
this.clearCanvas();
|
|
171
|
+
this[tool].startDraw();
|
|
169
172
|
this[tool].draw(this.imageToViewerElementPoints(this.points));
|
|
173
|
+
this[tool].endDraw();
|
|
170
174
|
this.$emit(EVENT_IN_PAINTING, this.getPaintOptions());
|
|
171
175
|
}
|
|
172
176
|
|
|
@@ -244,7 +248,9 @@ export class Board extends ViewerCommon {
|
|
|
244
248
|
this.points.pop();
|
|
245
249
|
this.points.push(point);
|
|
246
250
|
this.clearCanvas();
|
|
251
|
+
this[tool].startDraw();
|
|
247
252
|
this[tool].draw(this.imageToViewerElementPoints(this.points));
|
|
253
|
+
this[tool].endDraw();
|
|
248
254
|
this.$emit(EVENT_IN_PAINTING, this.getPaintOptions());
|
|
249
255
|
}
|
|
250
256
|
}
|
|
@@ -282,8 +288,17 @@ export class Board extends ViewerCommon {
|
|
|
282
288
|
* @return {boolean}
|
|
283
289
|
*/
|
|
284
290
|
isInROI(point) {
|
|
285
|
-
|
|
286
|
-
this[this.tool].options.isROI
|
|
291
|
+
if (this.options.ROI) {
|
|
292
|
+
return this[this.tool].options.isROI ||
|
|
293
|
+
this.isContainerLabel(point, this.ROIList);
|
|
294
|
+
} else if (this.ROIList.length > 0) {
|
|
295
|
+
return this[this.tool].options.isROI ||
|
|
296
|
+
this.isContainerLabel(point, this.ROIList);
|
|
297
|
+
} else {
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
/* return !this.options.ROI || this.isContainerLabel(point, this.ROIList) ||
|
|
301
|
+
this[this.tool].options.isROI;*/
|
|
287
302
|
}
|
|
288
303
|
|
|
289
304
|
/**
|
|
@@ -310,7 +325,9 @@ export class Board extends ViewerCommon {
|
|
|
310
325
|
if (!this.isInDraw || this.points.length === 0) return;
|
|
311
326
|
const points = this.imageToViewerElementPoints(this.points);
|
|
312
327
|
this.clearCanvas();
|
|
328
|
+
this[this.tool].startDraw();
|
|
313
329
|
this[this.tool].draw(points);
|
|
330
|
+
this[this.tool].endDraw();
|
|
314
331
|
this.$emit(EVENT_IN_PAINTING, this.getPaintOptions());
|
|
315
332
|
}
|
|
316
333
|
}
|
|
@@ -38,8 +38,10 @@ class Rotation extends ViewerCommon {
|
|
|
38
38
|
drawRect(startPoint, endPoint) {
|
|
39
39
|
const rect = this.viewport.imageToViewportRectangle(0, 0, this.imgWidth,
|
|
40
40
|
this.imgHeight);
|
|
41
|
+
this.rect.startDraw();
|
|
41
42
|
this.rect.draw([startPoint, endPoint], this.viewport.getRotation(),
|
|
42
43
|
this.viewport.viewportToViewerElementRectangle(rect));
|
|
44
|
+
this.rect.endDraw();
|
|
43
45
|
this.drawThumbPoint();
|
|
44
46
|
}
|
|
45
47
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
NO_NORMAL_REGION_TYPES,
|
|
2
|
+
HAS_REGION_TYPES, MARKS,
|
|
3
|
+
NO_NORMAL_REGION_TYPES, POINT_TYPES, POLYGON, REGION_TYPES,
|
|
4
4
|
} from '../../const/mark';
|
|
5
5
|
import {ViewerCommon} from '../common/common';
|
|
6
6
|
import * as Tools from '../../tool';
|
|
@@ -59,20 +59,35 @@ export class Shape extends ViewerCommon {
|
|
|
59
59
|
this.clearCanvas();
|
|
60
60
|
// 区域标注列表
|
|
61
61
|
const regionLabelList = this.labelList.filter(
|
|
62
|
-
({tool, isClose}) =>
|
|
62
|
+
({tool, isClose}) => REGION_TYPES.includes(tool) || isClose && tool ===
|
|
63
|
+
POLYGON).
|
|
63
64
|
map((item) => ({
|
|
64
65
|
...item,
|
|
65
66
|
child: [],
|
|
66
67
|
self: item,
|
|
67
68
|
}));
|
|
69
|
+
// strokeStyle, lineWidth, fillStyle 相同认为是一个路径下的图形,一起绘制,优化性能
|
|
70
|
+
const sameFixWidthLabel = {}; // star, flag, star, font lineWidth固定为2
|
|
71
|
+
const sameNormalLabel = {};
|
|
68
72
|
this.labelList.forEach((item) => {
|
|
69
73
|
if (!this.isInCanvas(item.region)) return;
|
|
70
74
|
if (item.show === false) return;
|
|
71
75
|
if (!this[item.tool]) return;
|
|
72
|
-
|
|
73
|
-
|
|
76
|
+
const key = `${item.strokeStyle ?? ''}_${item.lineWidth ??
|
|
77
|
+
''}_${item.fillStyle ?? ''}`;
|
|
78
|
+
const regionKey = `${item.strokeStyle ?? ''}_${item.lineWidth ?? ''}`;
|
|
79
|
+
if (POINT_TYPES.includes(item.tool)) {
|
|
80
|
+
if (sameFixWidthLabel[key]) {
|
|
81
|
+
sameFixWidthLabel[key].push(item);
|
|
82
|
+
} else {
|
|
83
|
+
sameFixWidthLabel[key] = [item];
|
|
84
|
+
}
|
|
74
85
|
} else {
|
|
75
|
-
|
|
86
|
+
if (sameNormalLabel[regionKey]) {
|
|
87
|
+
sameNormalLabel[regionKey].push(item);
|
|
88
|
+
} else {
|
|
89
|
+
sameNormalLabel[regionKey] = [item];
|
|
90
|
+
}
|
|
76
91
|
}
|
|
77
92
|
const parent = regionLabelList.find(
|
|
78
93
|
(parent) => parent.self !== item && item.points.every(
|
|
@@ -81,6 +96,26 @@ export class Shape extends ViewerCommon {
|
|
|
81
96
|
parent.child.push(item);
|
|
82
97
|
}
|
|
83
98
|
});
|
|
99
|
+
Object.values(sameFixWidthLabel).forEach((labels) => {
|
|
100
|
+
const ctx = this.canvas.getContext('2d');
|
|
101
|
+
ctx.beginPath();
|
|
102
|
+
labels.forEach((item) => {
|
|
103
|
+
this.drawLabel(item);
|
|
104
|
+
});
|
|
105
|
+
ctx.stroke();
|
|
106
|
+
if (labels?.[0]?.fillStyle) {
|
|
107
|
+
ctx.fill();
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
Object.values(sameNormalLabel).forEach((labels) => {
|
|
111
|
+
const ctx = this.canvas.getContext('2d');
|
|
112
|
+
ctx.beginPath();
|
|
113
|
+
labels.forEach((item) => {
|
|
114
|
+
this.drawLabel(item);
|
|
115
|
+
});
|
|
116
|
+
ctx.stroke();
|
|
117
|
+
});
|
|
118
|
+
|
|
84
119
|
regionLabelList.forEach((item) => {
|
|
85
120
|
if (!this.isInCanvas(item.region)) return;
|
|
86
121
|
if (item.show === false) return;
|
|
@@ -103,9 +138,6 @@ export class Shape extends ViewerCommon {
|
|
|
103
138
|
if (!NO_NORMAL_REGION_TYPES.includes(item.tool)) {
|
|
104
139
|
this[item.tool].draw(points, this.viewport.getRotation(),
|
|
105
140
|
this.imageToViewerElementRectangle(item.region));
|
|
106
|
-
} else if (item.tool === POLYGON) {
|
|
107
|
-
this[item.tool].draw(points);
|
|
108
|
-
this[item.tool].drawEndPoints(points);
|
|
109
141
|
} else {
|
|
110
142
|
this[item.tool].draw(points, scale);
|
|
111
143
|
}
|
package/src/model/label.model.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
import {DOT, FLAG, STAR} from '../const/mark';
|
|
2
|
+
|
|
1
3
|
export class LabelModel {
|
|
2
4
|
constructor(data = {}) {
|
|
3
5
|
if (!data) data = {};
|
|
4
6
|
this.id = data.id; // id标识
|
|
5
|
-
this.lineWidth = data.lineWidth
|
|
6
|
-
|
|
7
|
+
this.lineWidth = !data.lineWidth ?
|
|
8
|
+
(data.tool === STAR ? 15 :
|
|
9
|
+
data.tool === FLAG ? 30 : data.tool === DOT ? 10 : 2) :
|
|
10
|
+
data.lineWidth; // 标注宽度
|
|
11
|
+
this.strokeStyle = data.strokeStyle || '#027AFF'; // 标注颜色
|
|
7
12
|
this.fillStyle = data.fillStyle; // 填充颜色
|
|
8
13
|
this.description = data.description; // 描述
|
|
9
14
|
this.fontSize = data.fontSize; // 字号
|
|
@@ -17,7 +22,7 @@ export class LabelModel {
|
|
|
17
22
|
this.move = data.move; // 是否可移动
|
|
18
23
|
this.resize = data.resize; // 是否可拖动大小
|
|
19
24
|
this.isROI = data.isROI ?? false; // 是否是ROI
|
|
20
|
-
this.isClose = data.isClose ?? true; //
|
|
25
|
+
this.isClose = data.isClose ?? true; // 是否是闭合标注
|
|
21
26
|
this.select = data.select ?? false; // 是否是选中状态
|
|
22
27
|
this.show = data.show ?? true; // 是否显示
|
|
23
28
|
this.__other__ = data.__other__ || {}; // 其他信息
|
package/src/tool/Arrow.js
CHANGED
|
@@ -10,7 +10,6 @@ class Arrow extends Brush {
|
|
|
10
10
|
*/
|
|
11
11
|
constructor() {
|
|
12
12
|
super();
|
|
13
|
-
this.lineWidth = 2;
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
/**
|
|
@@ -23,17 +22,11 @@ class Arrow extends Brush {
|
|
|
23
22
|
const startPoint = points[0];
|
|
24
23
|
const endPoint = points[points.length - 1];
|
|
25
24
|
const ctx = this.canvas.getContext('2d');
|
|
26
|
-
ctx.
|
|
27
|
-
ctx.
|
|
28
|
-
ctx.strokeStyle = this.options.strokeStyle || '#0000FF';
|
|
25
|
+
ctx.lineWidth = this.options.lineWidth;
|
|
26
|
+
ctx.strokeStyle = this.options.strokeStyle;
|
|
29
27
|
ctx.moveTo(startPoint.x, startPoint.y);
|
|
30
28
|
ctx.lineTo(endPoint.x, endPoint.y);
|
|
31
|
-
ctx.stroke();
|
|
32
|
-
ctx.closePath();
|
|
33
|
-
ctx.beginPath();
|
|
34
29
|
ctx.drawArrow(startPoint, endPoint);
|
|
35
|
-
ctx.stroke();
|
|
36
|
-
ctx.closePath();
|
|
37
30
|
}
|
|
38
31
|
}
|
|
39
32
|
|
package/src/tool/Bilateral.js
CHANGED
|
@@ -10,7 +10,6 @@ class Bilateral extends Brush {
|
|
|
10
10
|
*/
|
|
11
11
|
constructor() {
|
|
12
12
|
super();
|
|
13
|
-
this.lineWidth = 2;
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
/**
|
|
@@ -23,18 +22,12 @@ class Bilateral extends Brush {
|
|
|
23
22
|
const startPoint = points[0];
|
|
24
23
|
const endPoint = points[points.length - 1];
|
|
25
24
|
const ctx = this.canvas.getContext('2d');
|
|
26
|
-
ctx.
|
|
27
|
-
ctx.
|
|
28
|
-
ctx.strokeStyle = this.options.strokeStyle || '#0000FF';
|
|
25
|
+
ctx.lineWidth = this.options.lineWidth;
|
|
26
|
+
ctx.strokeStyle = this.options.strokeStyle;
|
|
29
27
|
ctx.moveTo(startPoint.x, startPoint.y);
|
|
30
28
|
ctx.lineTo(endPoint.x, endPoint.y);
|
|
31
|
-
ctx.stroke();
|
|
32
|
-
ctx.closePath();
|
|
33
|
-
ctx.beginPath();
|
|
34
29
|
ctx.drawArrow(startPoint, endPoint);
|
|
35
30
|
ctx.drawArrow(endPoint, startPoint);
|
|
36
|
-
ctx.stroke();
|
|
37
|
-
ctx.closePath();
|
|
38
31
|
}
|
|
39
32
|
}
|
|
40
33
|
|
package/src/tool/Brush.js
CHANGED
|
@@ -10,11 +10,7 @@ class Brush {
|
|
|
10
10
|
*/
|
|
11
11
|
constructor() {
|
|
12
12
|
this.thumb = new Thumb();
|
|
13
|
-
this.options = {
|
|
14
|
-
lineWidth: 2,
|
|
15
|
-
fillStyle: '',
|
|
16
|
-
strokeStyle: '#0000ff',
|
|
17
|
-
};
|
|
13
|
+
this.options = {};
|
|
18
14
|
}
|
|
19
15
|
|
|
20
16
|
/**
|
|
@@ -26,13 +22,23 @@ class Brush {
|
|
|
26
22
|
this.canvas = canvas;
|
|
27
23
|
this.thumb.setCanvas(canvas);
|
|
28
24
|
this.options = {
|
|
29
|
-
lineWidth: 2,
|
|
30
|
-
fillStyle: '',
|
|
31
|
-
strokeStyle: '#0000ff',
|
|
32
25
|
...options,
|
|
33
26
|
};
|
|
34
27
|
}
|
|
35
28
|
|
|
29
|
+
startDraw() {
|
|
30
|
+
const ctx = this.canvas.getContext('2d');
|
|
31
|
+
ctx.beginPath();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
endDraw() {
|
|
35
|
+
const ctx = this.canvas.getContext('2d');
|
|
36
|
+
ctx.stroke();
|
|
37
|
+
if (this.options.fillStyle) {
|
|
38
|
+
ctx.fill();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
36
42
|
/**
|
|
37
43
|
* 画点
|
|
38
44
|
* @param {Object} point
|
package/src/tool/Combination.js
CHANGED
|
@@ -42,7 +42,8 @@ class Combination extends Brush {
|
|
|
42
42
|
points[0].y + region.height - centerPoint.y);
|
|
43
43
|
ctx.lineTo(points[0].x - centerPoint.x, points[0].y - centerPoint.y);
|
|
44
44
|
} else if (tool === ELLIPSE) {
|
|
45
|
-
ctx.drawOval(0, 0, region.width / 2,
|
|
45
|
+
ctx.drawOval(0, 0, Math.abs(region.width / 2),
|
|
46
|
+
Math.abs(region.height / 2), false);
|
|
46
47
|
}
|
|
47
48
|
ctx.closePath();
|
|
48
49
|
ctx.restore();
|
|
@@ -70,9 +71,9 @@ class Combination extends Brush {
|
|
|
70
71
|
points[0].y - centerPoint.y);
|
|
71
72
|
ctx.lineTo(points[0].x - centerPoint.x, points[0].y - centerPoint.y);
|
|
72
73
|
} else if (tool === ELLIPSE) {
|
|
73
|
-
ctx.drawOval(0, 0, region.width / 2,
|
|
74
|
+
ctx.drawOval(0, 0, Math.abs(region.width / 2),
|
|
75
|
+
Math.abs(region.height / 2), true);
|
|
74
76
|
}
|
|
75
|
-
ctx.closePath();
|
|
76
77
|
ctx.restore();
|
|
77
78
|
});
|
|
78
79
|
if (this.options.fillStyle) {
|
package/src/tool/Dot.js
CHANGED
|
@@ -21,20 +21,18 @@ class Dot extends Brush {
|
|
|
21
21
|
* @param {number} scale
|
|
22
22
|
*/
|
|
23
23
|
draw(points, scale = 1) {
|
|
24
|
-
let dist = this.options.lineWidth
|
|
24
|
+
let dist = this.options.lineWidth;
|
|
25
25
|
dist *= scale;
|
|
26
26
|
const point = points[0];
|
|
27
27
|
const ctx = this.canvas.getContext('2d');
|
|
28
28
|
const {x, y} = point;
|
|
29
29
|
ctx.lineWidth = 2;
|
|
30
|
-
ctx.
|
|
30
|
+
ctx.moveTo(x + dist, y);
|
|
31
31
|
ctx.arc(x, y, dist, 0, Math.PI * 2, !1);
|
|
32
32
|
if (this.options.fillStyle) {
|
|
33
33
|
ctx.fillStyle = this.options.fillStyle;
|
|
34
|
-
ctx.fill();
|
|
35
34
|
}
|
|
36
|
-
ctx.strokeStyle = this.options.strokeStyle
|
|
37
|
-
ctx.stroke();
|
|
35
|
+
ctx.strokeStyle = this.options.strokeStyle;
|
|
38
36
|
}
|
|
39
37
|
}
|
|
40
38
|
|
package/src/tool/Ellipse.js
CHANGED
|
@@ -24,8 +24,12 @@ class Ellipse extends Brush {
|
|
|
24
24
|
const startPoint = points[0];
|
|
25
25
|
const endPoint = points[points.length - 1];
|
|
26
26
|
const ctx = this.canvas.getContext('2d');
|
|
27
|
+
ctx.lineWidth = this.options.lineWidth;
|
|
28
|
+
ctx.strokeStyle = this.options.strokeStyle;
|
|
29
|
+
if (this.options.fillStyle) {
|
|
30
|
+
ctx.fillStyle = this.options.fillStyle;
|
|
31
|
+
}
|
|
27
32
|
ctx.save();
|
|
28
|
-
ctx.beginPath();
|
|
29
33
|
const centerPoint = {
|
|
30
34
|
x: (startPoint.x + endPoint.x) / 2,
|
|
31
35
|
y: (startPoint.y + endPoint.y) / 2,
|
|
@@ -38,23 +42,11 @@ class Ellipse extends Brush {
|
|
|
38
42
|
Math.abs(endPoint.y - startPoint.y);
|
|
39
43
|
ctx.translate(centerPoint.x, centerPoint.y);
|
|
40
44
|
ctx.rotate(rotation * Math.PI / 180);
|
|
41
|
-
ctx.lineWidth = this.options.lineWidth || 2;
|
|
42
|
-
ctx.strokeStyle = this.options.strokeStyle || '#0000FF';
|
|
43
45
|
if (rotation < 90 && rotation >= 0 || rotation > 180 && rotation < 270) {
|
|
44
46
|
ctx.drawOval(0, 0, width / 2, height / 2);
|
|
45
|
-
if (this.options.fillStyle) {
|
|
46
|
-
ctx.fillStyle = this.options.fillStyle;
|
|
47
|
-
ctx.drawOval(0, 0, width / 2, height / 2);
|
|
48
|
-
ctx.fill();
|
|
49
|
-
}
|
|
50
47
|
} else {
|
|
51
|
-
|
|
52
|
-
ctx.fillStyle = this.options.fillStyle;
|
|
53
|
-
ctx.drawOval(0, 0, height / 2, width / 2);
|
|
54
|
-
ctx.fill();
|
|
55
|
-
}
|
|
48
|
+
ctx.drawOval(0, 0, height / 2, width / 2);
|
|
56
49
|
}
|
|
57
|
-
ctx.stroke();
|
|
58
50
|
ctx.restore();
|
|
59
51
|
}
|
|
60
52
|
}
|
package/src/tool/Flag.js
CHANGED
|
@@ -13,16 +13,14 @@ class Flag extends Brush {
|
|
|
13
13
|
* @param {number} scale
|
|
14
14
|
*/
|
|
15
15
|
draw(points, scale = 1) {
|
|
16
|
-
let dist = this.options.lineWidth
|
|
16
|
+
let dist = this.options.lineWidth;
|
|
17
17
|
dist *= scale;
|
|
18
18
|
const point = points[0];
|
|
19
19
|
const ctx = this.canvas.getContext('2d');
|
|
20
|
-
ctx.beginPath();
|
|
21
20
|
ctx.lineWidth = 2;
|
|
22
|
-
ctx.strokeStyle = this.options.strokeStyle
|
|
21
|
+
ctx.strokeStyle = this.options.strokeStyle;
|
|
23
22
|
ctx.moveTo(point.x, point.y);
|
|
24
23
|
ctx.lineTo(point.x, point.y - dist);
|
|
25
|
-
ctx.stroke();
|
|
26
24
|
const _dist = dist / 5;
|
|
27
25
|
const p1 = {
|
|
28
26
|
x: point.x + _dist,
|
|
@@ -41,10 +39,8 @@ class Flag extends Brush {
|
|
|
41
39
|
y: point.y - dist,
|
|
42
40
|
};
|
|
43
41
|
ctx.bezierCurve([p1, p2, p3, p4]);
|
|
44
|
-
ctx.stroke();
|
|
45
42
|
ctx.moveTo(p4.x, p4.y);
|
|
46
43
|
ctx.lineTo(p4.x, p4.y + dist / 2);
|
|
47
|
-
ctx.stroke();
|
|
48
44
|
const p11 = {
|
|
49
45
|
x: point.x + _dist,
|
|
50
46
|
y: point.y - dist / 2,
|
|
@@ -62,10 +58,8 @@ class Flag extends Brush {
|
|
|
62
58
|
y: point.y - dist / 2,
|
|
63
59
|
};
|
|
64
60
|
ctx.bezierCurve([p11, p22, p33, p44]);
|
|
65
|
-
ctx.stroke();
|
|
66
61
|
ctx.moveTo(p1.x, p1.y);
|
|
67
62
|
ctx.lineTo(p11.x, p11.y);
|
|
68
|
-
ctx.stroke();
|
|
69
63
|
}
|
|
70
64
|
}
|
|
71
65
|
|
package/src/tool/Font.js
CHANGED
|
@@ -24,15 +24,14 @@ class Font extends Brush {
|
|
|
24
24
|
const angle = this.options.angle || 0;
|
|
25
25
|
const text = this.options.text || '编辑文字';
|
|
26
26
|
const ctx = this.canvas.getContext('2d');
|
|
27
|
-
ctx.
|
|
27
|
+
ctx.fillStyle = this.options.fillStyle || this.options.strokeStyle ||
|
|
28
|
+
'#FDFDFD';
|
|
28
29
|
ctx.save();
|
|
29
30
|
ctx.translate(point.x, point.y);
|
|
30
31
|
ctx.rotate(angle / 180 * Math.PI);
|
|
31
32
|
ctx.scale(scale, scale);
|
|
32
33
|
ctx.font = `${this.options.fontSize || 14}px Arial`;
|
|
33
|
-
ctx.fillStyle = this.options.strokeStyle || '#FDFDFD';
|
|
34
34
|
ctx.fillText(text, 0, 0);
|
|
35
|
-
ctx.stroke();
|
|
36
35
|
ctx.restore();
|
|
37
36
|
}
|
|
38
37
|
}
|
package/src/tool/Line.js
CHANGED
|
@@ -22,9 +22,8 @@ class Line extends Brush {
|
|
|
22
22
|
const startPoint = points[0];
|
|
23
23
|
const endPoint = points[points.length - 1];
|
|
24
24
|
const ctx = this.canvas.getContext('2d');
|
|
25
|
-
ctx.
|
|
26
|
-
ctx.
|
|
27
|
-
ctx.strokeStyle = this.options.strokeStyle || '#0000FF';
|
|
25
|
+
ctx.lineWidth = this.options.lineWidth;
|
|
26
|
+
ctx.strokeStyle = this.options.strokeStyle;
|
|
28
27
|
ctx.moveTo(startPoint.x, startPoint.y);
|
|
29
28
|
ctx.lineTo(endPoint.x, endPoint.y);
|
|
30
29
|
const deg = Math.atan2((endPoint.y - startPoint.y),
|
|
@@ -35,7 +34,6 @@ class Line extends Brush {
|
|
|
35
34
|
ctx.lineTo(startPoint.x - distX, startPoint.y + distY);
|
|
36
35
|
ctx.moveTo(endPoint.x + distX, endPoint.y - distY);
|
|
37
36
|
ctx.lineTo(endPoint.x - distX, endPoint.y + distY);
|
|
38
|
-
ctx.stroke();
|
|
39
37
|
}
|
|
40
38
|
}
|
|
41
39
|
|
package/src/tool/Polygon.js
CHANGED
|
@@ -5,22 +5,6 @@ import {Brush} from './Brush';
|
|
|
5
5
|
* @class
|
|
6
6
|
*/
|
|
7
7
|
class Polygon extends Brush {
|
|
8
|
-
/**
|
|
9
|
-
* 绘制结束点
|
|
10
|
-
* @param {Object[]} points - 绘制的点
|
|
11
|
-
* @param {number} points[].x - 绘制的点x坐标.
|
|
12
|
-
* @param {number} points[].y - 绘制的点y坐标.
|
|
13
|
-
*/
|
|
14
|
-
drawEndPoints(points) {
|
|
15
|
-
const ctx = this.canvas.getContext('2d');
|
|
16
|
-
ctx.beginPath();
|
|
17
|
-
ctx.lineWidth = this.options.lineWidth || 2;
|
|
18
|
-
ctx.strokeStyle = this.options.strokeStyle || '#0000FF';
|
|
19
|
-
ctx.moveTo(points[points.length - 1].x, points[points.length - 1].y);
|
|
20
|
-
ctx.lineTo(points[0].x, points[0].y);
|
|
21
|
-
ctx.stroke();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
8
|
/**
|
|
25
9
|
* 绘制
|
|
26
10
|
* @param {Object[]} points - 绘制的点
|
|
@@ -29,19 +13,18 @@ class Polygon extends Brush {
|
|
|
29
13
|
*/
|
|
30
14
|
draw(points) {
|
|
31
15
|
const ctx = this.canvas.getContext('2d');
|
|
32
|
-
ctx.
|
|
33
|
-
ctx.
|
|
34
|
-
ctx.strokeStyle = this.options.strokeStyle || '#0000FF';
|
|
16
|
+
ctx.lineWidth = this.options.lineWidth;
|
|
17
|
+
ctx.strokeStyle = this.options.strokeStyle;
|
|
35
18
|
ctx.moveTo(points[0].x, points[0].y);
|
|
36
19
|
points.forEach((point) => {
|
|
37
20
|
ctx.lineTo(point.x, point.y);
|
|
38
21
|
});
|
|
39
|
-
if (this.options.
|
|
22
|
+
if (this.options.isClose) {
|
|
40
23
|
ctx.lineTo(points[0].x, points[0].y);
|
|
41
|
-
|
|
42
|
-
|
|
24
|
+
if (this.options.fillStyle) {
|
|
25
|
+
ctx.fillStyle = this.options.fillStyle;
|
|
26
|
+
}
|
|
43
27
|
}
|
|
44
|
-
ctx.stroke();
|
|
45
28
|
}
|
|
46
29
|
}
|
|
47
30
|
|
package/src/tool/Rectangle.js
CHANGED
|
@@ -24,8 +24,12 @@ class Rectangle extends Brush {
|
|
|
24
24
|
const startPoint = points[0];
|
|
25
25
|
const endPoint = points[points.length - 1];
|
|
26
26
|
const ctx = this.canvas.getContext('2d');
|
|
27
|
+
ctx.lineWidth = this.options.lineWidth;
|
|
28
|
+
ctx.strokeStyle = this.options.strokeStyle;
|
|
29
|
+
if (this.options.fillStyle) {
|
|
30
|
+
ctx.fillStyle = this.options.fillStyle;
|
|
31
|
+
}
|
|
27
32
|
ctx.save();
|
|
28
|
-
ctx.beginPath();
|
|
29
33
|
const centerPoint = {
|
|
30
34
|
x: (startPoint.x + endPoint.x) / 2,
|
|
31
35
|
y: (startPoint.y + endPoint.y) / 2,
|
|
@@ -37,26 +41,13 @@ class Rectangle extends Brush {
|
|
|
37
41
|
Math.abs(region.height) :
|
|
38
42
|
Math.abs(endPoint.y - startPoint.y);
|
|
39
43
|
ctx.translate(centerPoint.x, centerPoint.y);
|
|
40
|
-
ctx.lineWidth = this.options.lineWidth || 2;
|
|
41
|
-
ctx.strokeStyle = this.options.strokeStyle || '#0000FF';
|
|
42
44
|
if (rotation < 90 && rotation >= 0 || rotation > 180 && rotation < 270) {
|
|
43
45
|
ctx.rotate(rotation * Math.PI / 180);
|
|
44
46
|
ctx.rect(-width / 2, -height / 2, width, height);
|
|
45
|
-
if (this.options.fillStyle) {
|
|
46
|
-
ctx.fillStyle = this.options.fillStyle;
|
|
47
|
-
ctx.rect(-width / 2, -height / 2, width, height);
|
|
48
|
-
ctx.fill();
|
|
49
|
-
}
|
|
50
47
|
} else {
|
|
51
48
|
ctx.rotate(rotation * Math.PI / 180);
|
|
52
49
|
ctx.rect(-height / 2, -width / 2, height, width);
|
|
53
|
-
if (this.options.fillStyle) {
|
|
54
|
-
ctx.fillStyle = this.options.fillStyle;
|
|
55
|
-
ctx.rect(-height / 2, -width / 2, height, width);
|
|
56
|
-
ctx.fill();
|
|
57
|
-
}
|
|
58
50
|
}
|
|
59
|
-
ctx.stroke();
|
|
60
51
|
ctx.restore();
|
|
61
52
|
}
|
|
62
53
|
}
|
package/src/tool/Star.js
CHANGED
|
@@ -21,7 +21,7 @@ class Star extends Brush {
|
|
|
21
21
|
* @param {number} scale
|
|
22
22
|
*/
|
|
23
23
|
draw(points, scale = 1) {
|
|
24
|
-
let dist = this.options.lineWidth
|
|
24
|
+
let dist = this.options.lineWidth;
|
|
25
25
|
const point = points[0];
|
|
26
26
|
dist *= scale;
|
|
27
27
|
this.endPoint = {x: point.x, y: point.y};
|
|
@@ -67,8 +67,7 @@ class Star extends Brush {
|
|
|
67
67
|
y: point.y - dist + short_side * Math.cos(angle),
|
|
68
68
|
};
|
|
69
69
|
const ctx = this.canvas.getContext('2d');
|
|
70
|
-
ctx.
|
|
71
|
-
ctx.lineWidth = 1;
|
|
70
|
+
ctx.lineWidth = 2;
|
|
72
71
|
ctx.moveTo(p1.x, p1.y);
|
|
73
72
|
ctx.lineTo(p2.x, p2.y);
|
|
74
73
|
ctx.lineTo(p3.x, p3.y);
|
|
@@ -80,11 +79,9 @@ class Star extends Brush {
|
|
|
80
79
|
ctx.lineTo(p9.x, p9.y);
|
|
81
80
|
ctx.lineTo(p10.x, p10.y);
|
|
82
81
|
ctx.lineTo(p1.x, p1.y);
|
|
83
|
-
ctx.strokeStyle = this.options.strokeStyle
|
|
84
|
-
ctx.stroke();
|
|
82
|
+
ctx.strokeStyle = this.options.strokeStyle;
|
|
85
83
|
if (this.options.fillStyle) {
|
|
86
84
|
ctx.fillStyle = this.options.fillStyle;
|
|
87
|
-
ctx.fill();
|
|
88
85
|
}
|
|
89
86
|
}
|
|
90
87
|
}
|
package/src/util/canvas.js
CHANGED
|
@@ -60,6 +60,7 @@ CanvasRenderingContext2D.prototype.drawOval = function drawOval(
|
|
|
60
60
|
this.save();
|
|
61
61
|
// 选择a、b中的较大者作为arc方法的半径参数
|
|
62
62
|
const r = (a > b) ? a : b;
|
|
63
|
+
console.log(r);
|
|
63
64
|
const ratioX = a / r; // 横轴缩放比率
|
|
64
65
|
const ratioY = b / r; // 纵轴缩放比率
|
|
65
66
|
this.scale(ratioX, ratioY); // 进行缩放(均匀压缩)
|
|
@@ -153,20 +154,17 @@ CanvasRenderingContext2D.prototype.bezierCurve = function(...rest) {
|
|
|
153
154
|
throw new SyntaxError('Parameter length greater than 1');
|
|
154
155
|
}
|
|
155
156
|
if (points.length === 2) {
|
|
156
|
-
this.beginPath();
|
|
157
157
|
this.moveTo(points[0].x, points[0].y);
|
|
158
158
|
this.lineTo(points[1].x, points[1].y);
|
|
159
159
|
this.stroke();
|
|
160
160
|
}
|
|
161
161
|
if (points.length === 3) {
|
|
162
|
-
this.beginPath();
|
|
163
162
|
const startPoint = points[0];
|
|
164
163
|
this.moveTo(startPoint.x, startPoint.y);
|
|
165
164
|
this.quadraticCurveTo(points[1].x, points[1].y, points[2].x,
|
|
166
165
|
points[2].y);
|
|
167
166
|
this.stroke();
|
|
168
167
|
} else if (points.length === 4) {
|
|
169
|
-
this.beginPath();
|
|
170
168
|
const startPoint = points[0];
|
|
171
169
|
this.moveTo(startPoint.x, startPoint.y);
|
|
172
170
|
this.bezierCurveTo(points[1].x, points[1].y, points[2].x, points[2].y,
|
|
@@ -177,7 +175,6 @@ CanvasRenderingContext2D.prototype.bezierCurve = function(...rest) {
|
|
|
177
175
|
const calculationPoints = [...new Array(101)].map(
|
|
178
176
|
(item, index) => bezierCurve.calculationPoint(index / 100));
|
|
179
177
|
for (let t = 1; t < 101; t++) {
|
|
180
|
-
this.beginPath();
|
|
181
178
|
this.moveTo(calculationPoints[t - 1].x, calculationPoints[t - 1].y);
|
|
182
179
|
this.lineTo(calculationPoints[t].x, calculationPoints[t].y);
|
|
183
180
|
this.stroke();
|