@annotorious/annotorious 3.4.2 → 3.4.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.
@@ -1,4 +1,5 @@
1
- import { MultiPolygonElement, MultiPolygonGeometry } from './MultiPolygon';
1
+ import { MultiPolygon, MultiPolygonElement, MultiPolygonGeometry } from './MultiPolygon';
2
2
  export declare const boundsFromMultiPolygonElements: (elements: MultiPolygonElement[]) => import('../Shape').Bounds;
3
3
  export declare const multipolygonElementToPath: (element: MultiPolygonElement) => string;
4
4
  export declare const getAllCorners: (geom: MultiPolygonGeometry) => [number, number][];
5
+ export declare const simplifyMultiPolygon: (multi: MultiPolygon, tolerance?: number) => MultiPolygon;
@@ -1 +1,2 @@
1
- export {};
1
+ import { Polygon } from './Polygon';
2
+ export declare const simplifyPolygon: (polygon: Polygon, tolerance?: number) => Polygon;
@@ -32,3 +32,4 @@ export declare const boundsFromPoints: (points: Array<[number, number]>) => Boun
32
32
  export declare const computePolygonArea: (points: [number, number][]) => number;
33
33
  export declare const isPointInPolygon: (points: [number, number][], x: number, y: number) => boolean;
34
34
  export declare const pointsToPath: (points: [number, number][], close?: boolean) => string;
35
+ export declare const simplifyPoints: (points: number[][], tolerance?: number) => [number, number][];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@annotorious/annotorious",
3
- "version": "3.4.2",
3
+ "version": "3.4.4",
4
4
  "description": "Add image annotation functionality to any web page with a few lines of JavaScript",
5
5
  "author": "Rainer Simon",
6
6
  "license": "BSD-3-Clause",
@@ -49,9 +49,10 @@
49
49
  "vitest": "^3.1.4"
50
50
  },
51
51
  "dependencies": {
52
- "@annotorious/core": "3.4.2",
52
+ "@annotorious/core": "3.4.4",
53
53
  "dequal": "^2.0.3",
54
54
  "rbush": "^4.0.1",
55
+ "simplify-js": "^1.2.4",
55
56
  "svg-pathdata": "^7.2.0",
56
57
  "uuid": "^11.1.0"
57
58
  }
@@ -50,7 +50,7 @@
50
50
 
51
51
  /** Determine visible midpoint, if any **/
52
52
  const onPointerMove = (evt: PointerEvent) => {
53
- if (selectedCorners.length > 0) {
53
+ if (selectedCorners.length > 0 || !midpoints.some(m => m.visible)) {
54
54
  visibleMidpoint = undefined;
55
55
  return;
56
56
  }
@@ -59,7 +59,7 @@
59
59
 
60
60
  /** Determine visible midpoint, if any **/
61
61
  const onPointerMove = (evt: PointerEvent) => {
62
- if (selectedCorners.length > 0) {
62
+ if (selectedCorners.length > 0 || !midpoints.some(m => m.visible)) {
63
63
  visibleMidpoint = undefined;
64
64
  return;
65
65
  }
@@ -1,7 +1,7 @@
1
1
  import { ShapeType } from '../Shape';
2
2
  import type { ShapeUtil } from '../shapeUtils';
3
- import { boundsFromPoints, computePolygonArea, isPointInPolygon, pointsToPath, registerShapeUtil } from '../shapeUtils';
4
- import type { MultiPolygon, MultiPolygonElement, MultiPolygonGeometry } from './MultiPolygon';
3
+ import { boundsFromPoints, computePolygonArea, isPointInPolygon, pointsToPath, registerShapeUtil, simplifyPoints } from '../shapeUtils';
4
+ import type { MultiPolygon, MultiPolygonElement, MultiPolygonGeometry, MultiPolygonRing } from './MultiPolygon';
5
5
 
6
6
  const MultiPolygonUtil: ShapeUtil<MultiPolygon> = {
7
7
 
@@ -70,4 +70,35 @@ export const getAllCorners = (geom: MultiPolygonGeometry) =>
70
70
  ]
71
71
  ), []);
72
72
 
73
+ export const simplifyMultiPolygon = (multi: MultiPolygon, tolerance = 1): MultiPolygon => {
74
+ const polygons = multi.geometry.polygons.map(polygon => {
75
+ const rings: MultiPolygonRing[] = polygon.rings.map(ring => {
76
+ const points = simplifyPoints(ring.points, tolerance);
77
+ return {
78
+ ...ring,
79
+ points
80
+ }
81
+ });
82
+
83
+ const bounds = boundsFromPoints(rings[0].points);
84
+
85
+ return {
86
+ ...polygon,
87
+ rings,
88
+ bounds
89
+ }
90
+ });
91
+
92
+ const bounds = boundsFromMultiPolygonElements(polygons);
93
+
94
+ return {
95
+ ...multi,
96
+ geometry: {
97
+ ...multi.geometry,
98
+ polygons,
99
+ bounds
100
+ }
101
+ }
102
+ }
103
+
73
104
  registerShapeUtil(ShapeType.MULTIPOLYGLON, MultiPolygonUtil);
@@ -1,6 +1,6 @@
1
1
  import { ShapeType } from '../Shape';
2
2
  import type { ShapeUtil } from '../shapeUtils';
3
- import { computePolygonArea, isPointInPolygon, registerShapeUtil } from '../shapeUtils';
3
+ import { boundsFromPoints, computePolygonArea, isPointInPolygon, registerShapeUtil, simplifyPoints } from '../shapeUtils';
4
4
  import type { Polygon } from './Polygon';
5
5
 
6
6
  const PolygonUtil: ShapeUtil<Polygon> = {
@@ -17,4 +17,18 @@ const PolygonUtil: ShapeUtil<Polygon> = {
17
17
 
18
18
  };
19
19
 
20
+ export const simplifyPolygon = (polygon: Polygon, tolerance = 1): Polygon => {
21
+ const points = simplifyPoints(polygon.geometry.points, tolerance);
22
+ const bounds = boundsFromPoints(points);
23
+
24
+ return {
25
+ ...polygon,
26
+ geometry: {
27
+ ...polygon.geometry,
28
+ bounds,
29
+ points
30
+ }
31
+ }
32
+ }
33
+
20
34
  registerShapeUtil(ShapeType.POLYGON, PolygonUtil);
@@ -1,3 +1,4 @@
1
+ import simplify from 'simplify-js';
1
2
  import type { Bounds, Shape, ShapeType } from './Shape';
2
3
 
3
4
  export interface ShapeUtil<T extends Shape> {
@@ -103,3 +104,8 @@ export const pointsToPath = (points: [number, number][], close: boolean = true):
103
104
 
104
105
  return d;
105
106
  }
107
+
108
+ export const simplifyPoints = (points: number[][], tolerance = 1): [number, number][] => {
109
+ const mapped = points.map(([x, y]) => ({ x, y }));
110
+ return simplify(mapped, tolerance, true).map(pt => [pt.x, pt.y]);
111
+ }