kfb-view 3.1.2 → 3.1.4
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/lib/kfb-view.js +1 -1
- package/package.json +1 -1
- package/src/components/common/common.js +17 -6
- package/src/tool/Font.js +4 -1
- package/src/util/calculate.js +29 -2
package/package.json
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import {EventEmitter} from '../../util/event-emitter';
|
|
2
2
|
import {Thumb} from '../../tool/Thumb';
|
|
3
3
|
import {
|
|
4
|
-
acreage,
|
|
5
|
-
|
|
4
|
+
acreage,
|
|
5
|
+
getAngle,
|
|
6
|
+
perimeter,
|
|
7
|
+
pointsToRegion,
|
|
8
|
+
getRectPoint,
|
|
9
|
+
isPointInEllipse,
|
|
10
|
+
isPointInMatrix,
|
|
11
|
+
isPointInPolygon,
|
|
12
|
+
calculatePolygonArea,
|
|
6
13
|
} from '../../util/calculate';
|
|
7
14
|
import {Point, Rect} from '../../plugin/openseadragon/openseadragon';
|
|
8
15
|
import {
|
|
@@ -253,8 +260,8 @@ export class ViewerCommon extends EventEmitter {
|
|
|
253
260
|
}
|
|
254
261
|
break;
|
|
255
262
|
case ELLIPSE: {
|
|
256
|
-
const w = Math.abs(endPoint.x - startPoint.x);
|
|
257
|
-
const h = Math.abs(endPoint.y - startPoint.y);
|
|
263
|
+
const w = Math.abs(endPoint.x - startPoint.x) / 2;
|
|
264
|
+
const h = Math.abs(endPoint.y - startPoint.y) / 2;
|
|
258
265
|
const pt = perimeter(ELLIPSE, startPoint, endPoint,
|
|
259
266
|
this.options.imageCapRes);
|
|
260
267
|
const ac = acreage(ELLIPSE, startPoint, endPoint,
|
|
@@ -316,7 +323,9 @@ export class ViewerCommon extends EventEmitter {
|
|
|
316
323
|
angle: 0,
|
|
317
324
|
texts: [
|
|
318
325
|
`周长:${(pt * this.options.imageCapRes).toFixed(
|
|
319
|
-
2)}${this.options.unit}
|
|
326
|
+
2)}${this.options.unit}`,
|
|
327
|
+
`面积:${calculatePolygonArea(points,
|
|
328
|
+
this.options.imageCapRes).toFixed(2)}${this.options.unit}²`],
|
|
320
329
|
};
|
|
321
330
|
}
|
|
322
331
|
break;
|
|
@@ -360,7 +369,9 @@ export class ViewerCommon extends EventEmitter {
|
|
|
360
369
|
const ctx = this.canvas.getContext('2d');
|
|
361
370
|
ctx.beginPath();
|
|
362
371
|
ctx.font = `${size}px Arial`;
|
|
363
|
-
|
|
372
|
+
const texts = text.split('\n');
|
|
373
|
+
const maxText = texts.sort((a, b) => a.length > b.length ? -1 : 1)[0];
|
|
374
|
+
return ctx.measureText(maxText).width;
|
|
364
375
|
}
|
|
365
376
|
|
|
366
377
|
/**
|
package/src/tool/Font.js
CHANGED
|
@@ -30,7 +30,10 @@ class Font extends Brush {
|
|
|
30
30
|
ctx.rotate(angle / 180 * Math.PI);
|
|
31
31
|
ctx.scale(scale, scale);
|
|
32
32
|
ctx.font = `${this.options.fontSize}px Arial`;
|
|
33
|
-
|
|
33
|
+
const texts = text.split('\n');
|
|
34
|
+
texts.forEach((v, index) => {
|
|
35
|
+
ctx.fillText(v, 0, 0 + index * this.options.fontSize);
|
|
36
|
+
});
|
|
34
37
|
ctx.restore();
|
|
35
38
|
}
|
|
36
39
|
}
|
package/src/util/calculate.js
CHANGED
|
@@ -395,6 +395,32 @@ function acreage(type, startPoint, endPoint, imageCapRes = 1) {
|
|
|
395
395
|
return ag;
|
|
396
396
|
}
|
|
397
397
|
|
|
398
|
+
function calculatePolygonArea(points, ratio) {
|
|
399
|
+
const numVertices = points.length;
|
|
400
|
+
|
|
401
|
+
if (numVertices < 3) {
|
|
402
|
+
// 无法构成多边形,返回0
|
|
403
|
+
return 0;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
let area = 0;
|
|
407
|
+
|
|
408
|
+
for (let i = 0; i < numVertices; i++) {
|
|
409
|
+
const currentVertex = points[i];
|
|
410
|
+
const nextVertex = points[(i + 1) % numVertices]; // 下一个顶点
|
|
411
|
+
|
|
412
|
+
const x1 = currentVertex.x * ratio;
|
|
413
|
+
const y1 = currentVertex.y * ratio;
|
|
414
|
+
const x2 = nextVertex.x * ratio;
|
|
415
|
+
const y2 = nextVertex.y * ratio;
|
|
416
|
+
|
|
417
|
+
area += (x1 * y2 - x2 * y1); // 通过叉乘计算每个三角形的面积
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// 最终的面积可能为负数,取绝对值
|
|
421
|
+
return Math.abs(area / 2);
|
|
422
|
+
}
|
|
423
|
+
|
|
398
424
|
/**
|
|
399
425
|
* 几何图形的周长
|
|
400
426
|
* @param {string} type
|
|
@@ -414,8 +440,8 @@ function perimeter(type, startPoint, endPoint, imageCapRes = 1) {
|
|
|
414
440
|
pt = (distX + distY) * 2;
|
|
415
441
|
}
|
|
416
442
|
if (type === 'Ellipse') {
|
|
417
|
-
const a = distX >= distY ? distX : distY;
|
|
418
|
-
const b = distX >= distY ? distY : distX;
|
|
443
|
+
const a = (distX >= distY ? distX : distY) / 2;
|
|
444
|
+
const b = (distX >= distY ? distY : distX) / 2;
|
|
419
445
|
pt = 4 * (a - b) + 2 * Math.PI * b;
|
|
420
446
|
}
|
|
421
447
|
/* if (type === 'polygon') {
|
|
@@ -440,4 +466,5 @@ export {
|
|
|
440
466
|
getPoint,
|
|
441
467
|
getAngle,
|
|
442
468
|
getDistance,
|
|
469
|
+
calculatePolygonArea,
|
|
443
470
|
};
|