kfb-view 2.1.20 → 2.2.2

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.
@@ -5,6 +5,8 @@ import {
5
5
  import {ViewerCommon} from '../common/common';
6
6
  import * as Tools from '../../tool';
7
7
  import {Combination} from '../../tool/Combination';
8
+ import {EVENT_CANCEL_SELECT_LABEL} from '../../const/event';
9
+ import * as EVENTS from '../../const/event';
8
10
 
9
11
  /**
10
12
  * 用来显示标注线的canvas
@@ -25,6 +27,7 @@ export class Shape extends ViewerCommon {
25
27
  this[mark] = new Tools[mark];
26
28
  });
27
29
  this.combination = new Combination();
30
+ this.delaytimer = [];
28
31
  }
29
32
 
30
33
  /**
@@ -32,15 +35,6 @@ export class Shape extends ViewerCommon {
32
35
  * @param {Object} e
33
36
  */
34
37
  onCanvasKey(e) {
35
- const {originalEvent} = e;
36
- e.preventDefaultAction = true;
37
- if (originalEvent.code === 'Escape') {
38
- this.labelList.forEach((item) => {
39
- item.select = false;
40
- item.show = true;
41
- });
42
- this.change();
43
- }
44
38
  }
45
39
 
46
40
  /**
@@ -57,31 +51,26 @@ export class Shape extends ViewerCommon {
57
51
  */
58
52
  change() {
59
53
  this.clearCanvas();
54
+ const bounds = this.viewport.getBounds();
60
55
  // 区域标注列表
61
56
  const regionLabelList = [];
62
- this.labelList.forEach(
63
- (item) => {
64
- if (REGION_TYPES.includes(item.tool) || item.isClose && item.tool ===
65
- POLYGON) {
66
- regionLabelList.push({
67
- ...item,
68
- child: [],
69
- self: item,
70
- });
71
- }
72
- });
57
+ const labelList = this.labelList.filter((item) => {
58
+ if (item.show === false) return false;
59
+ if (!this[item.tool]) return false;
60
+ if ((REGION_TYPES.includes(item.tool) || item.isClose && item.tool ===
61
+ POLYGON) && item.fillStyle) {
62
+ regionLabelList.push({
63
+ ...item,
64
+ child: [],
65
+ self: item,
66
+ });
67
+ }
68
+ return true;
69
+ });
73
70
  // strokeStyle, lineWidth, fillStyle 相同认为是一个路径下的图形,一起绘制,优化性能
74
71
  const sameFixWidthLabel = {}; // star, flag, star, font lineWidth固定为2
75
72
  const sameNormalLabel = {};
76
- this.labelList.forEach((item) => {
77
- if (item.show === false) return;
78
- if (!this[item.tool]) return;
79
- if (!this.isInCanvas(item.region)) return;
80
- /* const point = this.imageToViewerElementCoordinates(item.points[0].x,
81
- item.points[0].y);
82
- if ((point.x > this.canvas.width ||
83
- point.y > this.canvas.height ||
84
- point.x < 0 || point.y < 0)) return;*/
73
+ labelList.forEach((item) => {
85
74
  const key = `${item.strokeStyle ?? ''}_${item.lineWidth ??
86
75
  ''}_${item.fillStyle ?? ''}`;
87
76
  const regionKey = `${item.strokeStyle ?? ''}_${item.lineWidth ?? ''}`;
@@ -106,11 +95,43 @@ export class Shape extends ViewerCommon {
106
95
  parent.child.push(item);
107
96
  }
108
97
  });
98
+ if (this.delaytimer) {
99
+ clearTimeout(this.delaytimer);
100
+ this.delaytimer = undefined;
101
+ }
102
+ this.deepDrawLabel(sameFixWidthLabel, sameNormalLabel, 1, bounds);
103
+ regionLabelList.forEach((item) => {
104
+ if (!this.isInCanvas(item.region, bounds)) return false;
105
+ this.combination.setContent(this.canvas, item);
106
+ this.combination.draw({
107
+ points: this.imageToViewerElementPoints(item.points),
108
+ tool: item.tool,
109
+ }, item.child.map(({points, tool}) => ({
110
+ points: this.imageToViewerElementPoints(points),
111
+ tool,
112
+ })), this.viewport.getRotation());
113
+ });
114
+ }
115
+
116
+ deepDrawLabel(sameFixWidthLabel, sameNormalLabel, count, bounds) {
117
+ let currentDrawCount = 0; // 当前已绘制数量
109
118
  const ctx = this.canvas.getContext('2d');
110
- Object.values(sameFixWidthLabel).forEach((labels) => {
119
+ const remainFixLabel = {};
120
+ Object.keys(sameFixWidthLabel).forEach((key) => {
121
+ const labels = sameFixWidthLabel[key];
111
122
  ctx.beginPath();
112
123
  labels.forEach((item) => {
113
- this.drawLabel(item);
124
+ if (currentDrawCount >= 5000) { // 一次最多绘制5000个标注,多余标注延迟绘制
125
+ if (remainFixLabel[key]) {
126
+ remainFixLabel[key].push(item);
127
+ } else {
128
+ remainFixLabel[key] = [item];
129
+ }
130
+ } else {
131
+ if (!this.isInCanvas(item.region, bounds)) return false;
132
+ this.drawLabel(item);
133
+ }
134
+ currentDrawCount++;
114
135
  });
115
136
  const firstLabel = labels?.[0];
116
137
  if (firstLabel?.fillStyle) {
@@ -120,35 +141,38 @@ export class Shape extends ViewerCommon {
120
141
  ctx.strokeStyle = firstLabel.strokeStyle;
121
142
  ctx.stroke();
122
143
  });
123
- Object.values(sameNormalLabel).forEach((labels) => {
144
+ const remainNormalLabel = {};
145
+ Object.keys(sameNormalLabel).forEach((key) => {
146
+ const labels = sameNormalLabel[key];
124
147
  ctx.beginPath();
125
148
  labels.forEach((item) => {
126
- this.drawLabel(item);
149
+ if (currentDrawCount >= 5000) { // 一次最多绘制5000个标注,多余标注延迟绘制
150
+ if (remainNormalLabel[key]) {
151
+ remainNormalLabel[key].push(item);
152
+ } else {
153
+ remainNormalLabel[key] = [item];
154
+ }
155
+ } else {
156
+ if (!this.isInCanvas(item.region, bounds)) return false;
157
+ this.drawLabel(item);
158
+ }
159
+ currentDrawCount++;
127
160
  });
128
161
  const firstLabel = labels?.[0];
129
162
  ctx.lineWidth = firstLabel.lineWidth;
130
163
  ctx.strokeStyle = firstLabel.strokeStyle;
131
164
  ctx.stroke();
132
165
  });
133
-
134
- regionLabelList.forEach((item) => {
135
- if (item.show === false) return;
136
- if (!this[item.tool]) return;
137
- if (!this.isInCanvas(item.region)) return;
138
- /* const point = this.imageToViewerElementCoordinates(item.points[0].x,
139
- item.points[0].y);
140
- if ((point.x > this.canvas.width ||
141
- point.y > this.canvas.height ||
142
- point.x < 0 || point.y < 0)) return;*/
143
- this.combination.setContent(this.canvas, item);
144
- this.combination.draw({
145
- points: this.imageToViewerElementPoints(item.points),
146
- tool: item.tool,
147
- }, item.child.map(({points, tool}) => ({
148
- points: this.imageToViewerElementPoints(points),
149
- tool,
150
- })), this.viewport.getRotation());
151
- });
166
+ if (Object.keys(remainFixLabel).length === 0 &&
167
+ Object.keys(remainNormalLabel).length === 0) {
168
+ console.log(`end deep draw label, draw count: ${count}`);
169
+ this.delaytimer = undefined;
170
+ } else {
171
+ this.delaytimer = setTimeout(() => {
172
+ this.deepDrawLabel(remainFixLabel, remainNormalLabel, count + 1,
173
+ bounds);
174
+ }, 100);
175
+ }
152
176
  }
153
177
 
154
178
  drawLabel(item) {
@@ -105,7 +105,7 @@ export class Tailoring extends ViewerCommon {
105
105
  ctx.stroke();
106
106
  ctx.beginPath();
107
107
  ctx.fillStyle = this.color;
108
- ctx.font = '18px';
108
+ ctx.font = '18px Arial';
109
109
  ctx.fillText(
110
110
  `区域大小为:${rightTop.x - leftTop.x}*${rightBottom.y - rightTop.y}`,
111
111
  leftTop.x, leftTop.y - 50);
@@ -219,11 +219,9 @@ export class Tailoring extends ViewerCommon {
219
219
  * @param {Object} e
220
220
  */
221
221
  onCanvasKey(e) {
222
- if (this.isInTailoring) {
223
- const {originalEvent} = e;
224
- if (originalEvent.code === 'Escape') {
225
- this.stopTailoring();
226
- }
222
+ const {originalEvent} = e;
223
+ if (originalEvent.key === 'Escape') {
224
+ this.stopTailoring();
227
225
  }
228
226
  }
229
227
 
@@ -1,3 +1,4 @@
1
+
1
2
  export const EVENT_START_PAINTING = 'start-painting';
2
3
  export const EVENT_IN_PAINTING = 'in-painting';
3
4
  export const EVENT_END_PAINTING = 'end-painting';
@@ -13,4 +14,6 @@ export const EVENT_CACHE_BACK = 'cache-back';
13
14
  export const EVENT_CACHE_FORWARD = 'cache-forward';
14
15
  export const EVENT_HIDDEN_LABEL = 'hidden-label';
15
16
  export const EVENT_DELETE_LABEL = 'delete-label';
17
+ export const EVENT_DELETE_POLYGON_POINT = 'delete-polygon-point';
18
+ export const EVENT_ADD_POLYGON_POINT = 'add-polygon-point';
16
19
  export const EVENT_NAVIGATOR_VESTIGE = 'navigator-vestige';
package/src/tool/Brush.js CHANGED
@@ -42,7 +42,7 @@ class Brush {
42
42
  * @param {Object} point
43
43
  * @param {Object} config 参数,参见thumb
44
44
  */
45
- drawPoint(point, config) {
45
+ drawThumb(point, config) {
46
46
  this.thumb.draw(point, config);
47
47
  }
48
48
 
@@ -74,6 +74,7 @@ class Combination extends Brush {
74
74
  ctx.drawOval(0, 0, Math.abs(region.width / 2),
75
75
  Math.abs(region.height / 2), true);
76
76
  }
77
+ ctx.closePath();
77
78
  ctx.restore();
78
79
  });
79
80
  if (this.options.fillStyle) {
@@ -28,12 +28,9 @@ class Polygon extends Brush {
28
28
  }
29
29
  }
30
30
 
31
- drawPoints(points) {
31
+ drawPoints(points, config) {
32
32
  points.forEach((point) => {
33
- this.thumb.draw(point, {
34
- thumbRadius: this.options.thumbRadius ?? 5,
35
- strokeStyle: this.options.thumbColor,
36
- });
33
+ this.drawThumb(point, config);
37
34
  });
38
35
  }
39
36
  }
package/src/tool/Thumb.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const ThumbConfig = {
2
2
  lineWidth: 2,
3
- thumbRadius: 5,
3
+ radius: 5,
4
4
  fillStyle: 'rgba(255, 255, 255, 1)',
5
5
  strokeStyle: '#01d0b0',
6
6
  };
@@ -51,7 +51,7 @@ class Thumb {
51
51
  config = this.getConfig(config);
52
52
  ctx.beginPath();
53
53
  ctx.lineWidth = config.lineWidth;
54
- ctx.arc(x, y, config.thumbRadius, 0, Math.PI * 2, !1);
54
+ ctx.arc(x, y, config.radius, 0, Math.PI * 2, !1);
55
55
  ctx.fillStyle = config.fillStyle;
56
56
  ctx.fill();
57
57
  ctx.strokeStyle = config.strokeStyle;
@@ -132,9 +132,8 @@ function isPointInPolygon(p, poly) {
132
132
  if ((sy < py && ty >= py) || (sy >= py && ty < py)) {
133
133
  // 线段上与射线 Y 坐标相同的点的 X 坐标
134
134
  const x = sx + (py - sy) * (tx - sx) / (ty - sy);
135
-
136
- // 点在多边形的边上
137
- if (x === px) {
135
+ // 点在多边形的边上,8像素的偏差
136
+ if (Math.abs(x - px) < 10) {
138
137
  return true;
139
138
  }
140
139
 
@@ -149,6 +148,29 @@ function isPointInPolygon(p, poly) {
149
148
  return flag;
150
149
  }
151
150
 
151
+ /**
152
+ * 判断一个点是否在一条线段上
153
+ * @param {Object} p
154
+ * @param {Object[]} points
155
+ * @return {boolean}
156
+ */
157
+ function isPointInLine(p, points) {
158
+ const startPoint = points[0];
159
+ const endPoint = points[1];
160
+ // 判断线段两端点是否在射线两侧
161
+ if ((startPoint.y < p.y && endPoint.y >= p.y) ||
162
+ (startPoint.y >= p.y && endPoint.y < p.y)) {
163
+ // 线段上与射线 Y 坐标相同的点的 X 坐标
164
+ const x = startPoint.x + (p.y - startPoint.y) *
165
+ (endPoint.x - startPoint.x) / (endPoint.y - startPoint.y);
166
+ // 点在多边形的边上,8像素的偏差
167
+ if (Math.abs(x - p.x) < 8) {
168
+ return true;
169
+ }
170
+ }
171
+ return false;
172
+ }
173
+
152
174
  /**
153
175
  * points转换成Region
154
176
  * @param {Object[]} points - 绘制的点
@@ -366,6 +388,7 @@ export {
366
388
  pointsToRegion,
367
389
  regionToPoint,
368
390
  isPointInPolygon,
391
+ isPointInLine,
369
392
  isNegNumber,
370
393
  baseNumber,
371
394
  acreage,
package/src/view.js CHANGED
@@ -6,7 +6,7 @@ import {pointsToRegion} from './util/calculate';
6
6
  import * as COMPONENTS from './const/component';
7
7
  import * as EVENTS from './const/event';
8
8
  import * as Components from './components';
9
- import {MARKS} from './const/mark';
9
+ import {MARKS, POLYGON} from './const/mark';
10
10
  import {LabelModel} from './model/label.model';
11
11
 
12
12
  /**
@@ -27,19 +27,25 @@ export default class KfbView extends EventEmitter {
27
27
  * @param {string} config.navigator.width navigator宽度
28
28
  * @param {string} config.navigator.height navigator高度
29
29
  * @param {string} config.navigator.thumbnail 缩略图地址
30
- * @param {boolean=} config.navigator.vestige 是否显示缩略图浏览痕迹
31
- * @param {number=} config.navigator.vestigeDelay 浏览痕迹颜色时间
32
- * @param {string=} config.navigator.vestigeColor 浏览痕迹颜色
33
- * @param {number=} config.navigator.vestigeZoom 浏览痕迹触发倍率
30
+ * @param {Object} config.navigator.vestige 缩略图浏览痕迹配置
31
+ * @param {boolean=} config.navigator.vestige.show 是否显示缩略图浏览痕迹
32
+ * @param {number=} config.navigator.vestige.delay 浏览痕迹颜色时间
33
+ * @param {string[]=} config.navigator.vestige.colors 浏览痕迹颜色
34
+ * @param {number[]=} config.navigator.vestige.zooms 浏览痕迹触发倍率
34
35
  * @param {Object} config.pxConversion 像素转换参数
35
36
  * @param {number} config.pxConversion.imageCapRes 像素转换倍率
36
37
  * @param {string} config.pxConversion.unit 单位
37
38
  * @param {Object} config.area 交互区域,触发点击、拖动等事件
38
39
  * @param {boolean=} config.area.disabled 是否禁用area
39
40
  * @param {boolean=} config.area.drag 是否允许拖动
40
- * @param {boolean=} config.area.thumb 选中区域时是否显示标注点
41
- * @param {boolean=} config.area.thumbRadius 选中区域标注点的半径
42
- * @param {boolean=} config.area.thumbColor 选中区域标注点的颜色
41
+ * @param {Object} config.thumb 选中区域时标注点配置
42
+ * @param {boolean=} config.thumb.show 选中区域时是否标注点
43
+ * @param {number=} config.thumb.radius 选中区域标注点的半径
44
+ * @param {String=} config.thumb.color 选中区域标注点的颜色
45
+ * @param {String=} config.thumb.bgColor 选中区域标注点的背景颜色
46
+ * @param {number=} config.thumb.activeRadius 标注点激活状态下的半径
47
+ * @param {boolean=} config.thumb.activeColor 标注点激活状态下颜色
48
+ * @param {boolean=} config.thumb.activeBgColor 标注点激活状态下背景颜色
43
49
  * @param {Object} config.rotation 旋转图像参数
44
50
  * @param {boolean=} config.rotation.disabled 是否禁用旋转
45
51
  * @param {Object} config.tailoring 裁剪图像参数
@@ -254,7 +260,18 @@ function initComponentsOptions(kv, type) {
254
260
  viewer: kv.viewer,
255
261
  cache: kv.cache,
256
262
  canvas: createCanvas(kv),
257
- options: {...config[type], ...pxConversion},
263
+ options: {
264
+ ...config[type], ...pxConversion, thumb: config.thumb ? {
265
+ radius: 5,
266
+ activeRadius: 7,
267
+ activeBgColor: '#01d0b0',
268
+ ...config.thumb,
269
+ } : {
270
+ radius: 5,
271
+ activeRadius: 7,
272
+ activeBgColor: '#01d0b0',
273
+ },
274
+ },
258
275
  };
259
276
  }
260
277
 
@@ -287,8 +304,7 @@ function initEvent(kv) {
287
304
  e.preventDefaultAction = true;
288
305
  kv.tailoring.onCanvasDrag(e.position);
289
306
  } else if (kv.area?.movePoint) {
290
- e.preventDefaultAction = true;
291
- kv.area.onCanvasDrag(e.position);
307
+ kv.area.onCanvasDrag(e.position, e);
292
308
  kv.change();
293
309
  }
294
310
  });
@@ -308,12 +324,17 @@ function initEvent(kv) {
308
324
  e.preventDefaultAction = true;
309
325
  const {originalEvent} = e;
310
326
  handlerCacheEvent(originalEvent, kv);
311
- handlerLabelEvent(originalEvent, kv);
312
- kv.area?.onCanvasKey?.(e);
313
- kv.shape?.onCanvasKey?.(e);
314
- kv.board?.onCanvasKey?.(e);
315
- kv.tailoring?.onCanvasKey?.(e);
316
- kv.rotation?.onCanvasKey?.(e);
327
+ if (kv.board?.isInDraw) {
328
+ kv.board?.onCanvasKey?.(e);
329
+ } else if (kv.rotation?.isInRotation) {
330
+ kv.rotation?.onCanvasKey?.(e);
331
+ } else if (kv.tailoring?.isInTailoring) {
332
+ kv.tailoring?.onCanvasKey?.(e);
333
+ } else {
334
+ handlerLabelEvent(originalEvent, kv);
335
+ kv.area?.change?.(e);
336
+ kv.shape?.change?.(e);
337
+ }
317
338
  }, 100));
318
339
 
319
340
  kv.viewer.addHandler('canvas-double-click', (e) => {
@@ -391,17 +412,36 @@ function handlerCacheEvent(e, kv) {
391
412
  }
392
413
 
393
414
  function handlerLabelEvent(e, kv) {
415
+ const index = kv.labelList.findIndex((item) => item.select);
416
+ const label = kv.labelList[index];
394
417
  if (e.key === 'h') {
395
- const label = kv.labelList.find((label) => label.select);
396
418
  if (!label) return;
397
419
  label.show = false;
398
420
  kv.$emit(EVENTS.EVENT_HIDDEN_LABEL, label);
399
- }
400
- if (e.key === 'Delete' || e.key === 'Backspace') {
401
- const index = kv.labelList.findIndex((label) => label.select);
402
- if (!~index) return;
403
- kv.$emit(EVENTS.EVENT_DELETE_LABEL, kv.labelList[index]);
404
- kv.labelList.splice(index, 1);
421
+ } else if (e.key === 'Delete' || e.key === 'Backspace') {
422
+ if (!label) return;
423
+ if (kv.area?.movePoint?.label?.tool === POLYGON) {
424
+ const movePoint = kv.area.movePoint;
425
+ movePoint.label.points.splice(movePoint.position, 1);
426
+ if (movePoint.label.points.length === 0) {
427
+ kv.$emit(EVENTS.EVENT_DELETE_LABEL, label);
428
+ kv.labelList.splice(index, 1);
429
+ } else {
430
+ kv.$emit(EVENTS.EVENT_DELETE_POLYGON_POINT, movePoint.label);
431
+ }
432
+ kv.area.movePoint = undefined;
433
+ } else {
434
+ kv.$emit(EVENTS.EVENT_DELETE_LABEL, label);
435
+ kv.labelList.splice(index, 1);
436
+ }
437
+ } else if (e.key === 'Escape') {
438
+ if (label) {
439
+ kv.$emit(EVENTS.EVENT_CANCEL_SELECT_LABEL, label);
440
+ }
441
+ kv.labelList.forEach((item) => {
442
+ item.select = false;
443
+ item.show = true;
444
+ });
405
445
  }
406
446
  }
407
447