image-annotation-drawer 1.0.0 → 1.0.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.
@@ -1,165 +0,0 @@
1
- /**
2
- * 标注管理模块
3
- * 负责矩形、多边形标注的存储、绘制、选中、移动、调整大小等功能
4
- */
5
- import type { Rect, Polygon, Point, Operate, SelectedAnnotation, ActiveHandle, SelectionStyle, ColorConfig, AnnotationStyle, LineStyle, VertexStyle } from './types';
6
- import type { ViewportManager } from './viewport';
7
- export declare class AnnotationManager {
8
- private viewport;
9
- recordList: Operate<Rect | Polygon>[];
10
- operate: Operate<Rect | Polygon>;
11
- isDrawing: boolean;
12
- drawStartPoint: Point;
13
- tempPolygonPoint: Point | null;
14
- selectedAnnotation: SelectedAnnotation | null;
15
- isMovingAnnotation: boolean;
16
- isResizing: boolean;
17
- activeHandle: ActiveHandle | null;
18
- annotationMoveStart: Point;
19
- originalRect: Rect | null;
20
- originalPolygon: Polygon[] | null;
21
- private deleteHistory;
22
- strokeStyle: string;
23
- lineWidth: number;
24
- lineStyle: LineStyle;
25
- vertexStyle: VertexStyle;
26
- selectionStyle: SelectionStyle;
27
- private colorConfig;
28
- constructor(viewport: ViewportManager);
29
- /**
30
- * 设置颜色配置
31
- */
32
- setColorConfig(config: string | ColorConfig): void;
33
- /**
34
- * 获取颜色配置
35
- */
36
- getColorConfig(): ColorConfig;
37
- /**
38
- * 应用颜色配置到当前样式
39
- */
40
- private applyColorConfig;
41
- /**
42
- * 获取指定类型的颜色
43
- */
44
- getColorForType(type: 'rect' | 'polygon' | 'default'): string;
45
- /**
46
- * 获取当前样式(用于创建新标注时保存)
47
- */
48
- getCurrentStyle(): AnnotationStyle;
49
- /**
50
- * 设置边线样式
51
- */
52
- setLineStyle(style: LineStyle): void;
53
- /**
54
- * 设置顶点样式
55
- */
56
- setVertexStyle(style: Partial<VertexStyle>): void;
57
- /**
58
- * 获取边线样式
59
- */
60
- getLineStyle(): LineStyle;
61
- /**
62
- * 获取顶点样式
63
- */
64
- getVertexStyle(): VertexStyle;
65
- /**
66
- * 获取标注的样式(如果不存在则返回当前样式)
67
- */
68
- getAnnotationStyle(annotation: Operate<Rect | Polygon>): AnnotationStyle;
69
- /**
70
- * 开始绘制矩形
71
- */
72
- startRectDrawing(startPoint: Point): void;
73
- /**
74
- * 更新矩形绘制
75
- */
76
- updateRectDrawing(currentPoint: Point): void;
77
- /**
78
- * 完成矩形绘制
79
- */
80
- finishRectDrawing(): boolean;
81
- /**
82
- * 开始绘制多边形
83
- */
84
- startPolygonDrawing(startPoint: Point): void;
85
- /**
86
- * 添加多边形点
87
- */
88
- addPolygonPoint(point: Point): boolean;
89
- /**
90
- * 更新多边形临时点
91
- */
92
- updatePolygonTempPoint(point: Point): void;
93
- /**
94
- * 完成多边形绘制
95
- */
96
- finishPolygonDrawing(): boolean;
97
- /**
98
- * 取消当前绘制
99
- */
100
- cancelDrawing(): void;
101
- /**
102
- * 撤销操作
103
- */
104
- withdraw(): boolean;
105
- /**
106
- * 选中指定索引的标注
107
- */
108
- selectAnnotation(index: number): boolean;
109
- /**
110
- * 取消选中
111
- */
112
- deselectAnnotation(): void;
113
- /**
114
- * 删除选中的标注
115
- */
116
- deleteSelectedAnnotation(): boolean;
117
- /**
118
- * 开始移动标注
119
- */
120
- startMovingAnnotation(e: MouseEvent): boolean;
121
- /**
122
- * 移动选中的标注
123
- */
124
- moveSelectedAnnotation(dx: number, dy: number): boolean;
125
- /**
126
- * 完成标注移动
127
- */
128
- finishMovingAnnotation(): void;
129
- /**
130
- * 开始调整大小
131
- */
132
- startResizing(handle: ActiveHandle, _startPoint: Point): boolean;
133
- /**
134
- * 调整矩形大小
135
- */
136
- resizeRect(currentPoint: Point): boolean;
137
- /**
138
- * 调整多边形顶点位置
139
- */
140
- resizePolygon(currentPoint: Point): boolean;
141
- /**
142
- * 获取点击位置对应的标注
143
- */
144
- getAnnotationAtPoint(imgCoords: Point): SelectedAnnotation | null;
145
- /**
146
- * 获取点击位置的控制点
147
- */
148
- getHandleAtPoint(offsetX: number, offsetY: number): ActiveHandle | null;
149
- /**
150
- * 清除所有标注
151
- */
152
- clear(): void;
153
- /**
154
- * 获取所有标注数据
155
- */
156
- getAnnotations(): Operate<Rect | Polygon>[];
157
- /**
158
- * 获取当前选中的标注信息
159
- */
160
- getSelectedAnnotation(): {
161
- index: number;
162
- type: "rect" | "polygon" | "text";
163
- data: Operate<Rect | Polygon>;
164
- } | null;
165
- }
@@ -1,113 +0,0 @@
1
- /**
2
- * 事件处理模块
3
- * 负责所有用户交互事件的处理
4
- */
5
- import type { DrawType, Point } from './types';
6
- import type { ViewportManager } from './viewport';
7
- import type { AnnotationManager } from './annotations';
8
- import type { TextAnnotationManager } from './text-annotation';
9
- import type { Renderer } from './renderer';
10
- export declare class EventHandler {
11
- private canvas;
12
- private viewport;
13
- private annotationManager;
14
- private textManager;
15
- private renderer;
16
- private getDrawType;
17
- private getBgImage;
18
- private renderCallback;
19
- private setDrawTypeCallback;
20
- isDragging: boolean;
21
- dragStart: Point;
22
- justFinishedMove: boolean;
23
- justDeselected: boolean;
24
- constructor(canvas: HTMLCanvasElement, viewport: ViewportManager, annotationManager: AnnotationManager, textManager: TextAnnotationManager, renderer: Renderer, getDrawType: () => DrawType, getBgImage: () => HTMLImageElement | null, renderCallback: () => void, setDrawTypeCallback: (type: DrawType) => void);
25
- /**
26
- * 处理滚轮缩放
27
- */
28
- handleWheel(e: WheelEvent): void;
29
- /**
30
- * 处理鼠标按下
31
- */
32
- handleMouseDown(e: MouseEvent): void;
33
- /**
34
- * 拖拽模式下检查文本点击
35
- */
36
- private checkTextClickInDragMode;
37
- /**
38
- * 文本模式下的点击处理
39
- */
40
- private handleTextModeClick;
41
- /**
42
- * 矩形模式下的点击处理
43
- */
44
- private handleRectModeClick;
45
- /**
46
- * 多边形模式下的点击处理
47
- */
48
- private handlePolygonModeClick;
49
- /**
50
- * 纯移动检测:检测是否点击了矩形
51
- */
52
- private handleRectModeClickForMove;
53
- /**
54
- * 纯移动检测:检测是否点击了多边形
55
- */
56
- private handlePolygonModeClickForMove;
57
- /**
58
- * 无模式下的点击处理
59
- */
60
- private handleNoModeClick;
61
- /**
62
- * 检查多边形点是否有效(避免重复点)
63
- */
64
- private checkPolygonPointValid;
65
- /**
66
- * 处理鼠标移动
67
- */
68
- handleMouseMove(e: MouseEvent): void;
69
- /**
70
- * 处理标注移动或调整大小
71
- */
72
- private handleAnnotationMove;
73
- /**
74
- * 更新鼠标光标样式
75
- */
76
- private updateCursor;
77
- /**
78
- * 更新矩形模式光标
79
- */
80
- private updateRectModeCursor;
81
- /**
82
- * 更新多边形模式光标
83
- */
84
- private updatePolygonModeCursor;
85
- /**
86
- * 更新拖拽模式光标
87
- */
88
- private updateDragModeCursor;
89
- /**
90
- * 更新无模式光标
91
- */
92
- private updateNoModeCursor;
93
- /**
94
- * 处理鼠标释放
95
- */
96
- handleMouseUp(_e: MouseEvent): void;
97
- /**
98
- * 处理鼠标离开
99
- */
100
- handleMouseLeave(e: MouseEvent): void;
101
- /**
102
- * 处理鼠标双击
103
- */
104
- handleDoubleClick(e: MouseEvent): void;
105
- /**
106
- * 处理鼠标单击(用于多边形绘制)
107
- */
108
- handleClick(e: MouseEvent): void;
109
- /**
110
- * 处理键盘按下
111
- */
112
- handleKeyDown(e: KeyboardEvent): void;
113
- }
@@ -1,74 +0,0 @@
1
- /**
2
- * 渲染模块
3
- * 负责所有绘制操作,包括背景图片、标注、选中高亮、文本标注等
4
- */
5
- import type { ViewportManager } from './viewport';
6
- import type { AnnotationManager } from './annotations';
7
- import type { TextAnnotationManager } from './text-annotation';
8
- export declare class Renderer {
9
- private ctx;
10
- private viewport;
11
- private annotationManager;
12
- private textManager;
13
- private canvas;
14
- constructor(ctx: CanvasRenderingContext2D, viewport: ViewportManager, annotationManager: AnnotationManager, textManager: TextAnnotationManager, canvas: HTMLCanvasElement);
15
- /**
16
- * 主渲染方法
17
- */
18
- render(bgImage: HTMLImageElement | null): void;
19
- /**
20
- * 绘制背景图片
21
- */
22
- private drawBackgroundImage;
23
- /**
24
- * 绘制标注(矩形、多边形)
25
- */
26
- private drawAnnotations;
27
- /**
28
- * 设置边线虚线样式
29
- */
30
- private setLineDash;
31
- /**
32
- * 绘制矩形
33
- * @param rect - 矩形数据
34
- * @param isSelected - 是否被选中(用于顶点颜色)
35
- */
36
- private drawRect;
37
- /**
38
- * 绘制多边形
39
- * @param polygon - 多边形顶点数据
40
- * @param closed - 是否闭合
41
- * @param vertexStyle - 顶点样式
42
- */
43
- private drawPolygon;
44
- /**
45
- * 绘制多边形顶点标记
46
- * @param polygon - 多边形顶点数据
47
- * @param vertexStyle - 顶点样式
48
- */
49
- private drawPolygonVertices;
50
- /**
51
- * 绘制当前正在绘制的多边形
52
- */
53
- private drawCurrentPolygon;
54
- /**
55
- * 绘制选中状态的标注高亮
56
- */
57
- private drawSelectionHighlight;
58
- /**
59
- * 绘制矩形选中高亮
60
- */
61
- private drawRectSelectionHighlight;
62
- /**
63
- * 绘制多边形选中高亮
64
- */
65
- private drawPolygonSelectionHighlight;
66
- /**
67
- * 绘制文本标注
68
- */
69
- private drawTextAnnotations;
70
- /**
71
- * 导出时绘制所有内容到指定上下文
72
- */
73
- drawForExport(ctx: CanvasRenderingContext2D, bgImage: HTMLImageElement, originalWidth: number, originalHeight: number): void;
74
- }
@@ -1,149 +0,0 @@
1
- /**
2
- * 文本标注模块
3
- * 负责文本标注的添加、编辑、移动、删除等功能
4
- */
5
- import type { TextAnnotation, TextStyle, Point, TextInputStyle } from './types';
6
- import type { ViewportManager } from './viewport';
7
- export declare class TextAnnotationManager {
8
- private viewport;
9
- private container;
10
- private ctx;
11
- private renderCallback?;
12
- textAnnotations: TextAnnotation[];
13
- editingTextIndex: number | null;
14
- currentEditingIndex: number | null;
15
- isTextMoving: boolean;
16
- textMoveStart: Point;
17
- textBeforeEditing: string;
18
- selectedTextIndex: number | null;
19
- textInput: HTMLInputElement | null;
20
- private deleteHistory;
21
- private defaultInputStyle;
22
- textStyle: TextStyle;
23
- constructor(viewport: ViewportManager, container: HTMLElement, ctx: CanvasRenderingContext2D, renderCallback?: (() => void) | undefined);
24
- /**
25
- * 创建文本输入框
26
- */
27
- private createTextInput;
28
- /**
29
- * 应用输入框样式
30
- */
31
- private applyInputStyle;
32
- /**
33
- * 设置文本样式
34
- */
35
- setTextStyle(style: Partial<TextStyle>): void;
36
- /**
37
- * 设置输入框样式
38
- */
39
- setInputStyle(style: Partial<TextInputStyle>): void;
40
- /**
41
- * 设置选中态样式
42
- */
43
- setSelectionStyle(style: {
44
- selectedBorderColor?: string;
45
- selectedBackgroundColor?: string;
46
- }): void;
47
- /**
48
- * 获取当前文本样式(用于创建新标注时保存)
49
- */
50
- getCurrentStyle(): Pick<TextStyle, 'font' | 'color' | 'backgroundColor'>;
51
- /**
52
- * 添加文本标注
53
- */
54
- addTextAnnotation(x: number, y: number, text?: string): number;
55
- /**
56
- * 更新文本标注内容
57
- */
58
- updateTextAnnotation(index: number, text: string): boolean;
59
- /**
60
- * 移动文本标注位置
61
- */
62
- moveTextAnnotation(index: number, x: number, y: number): boolean;
63
- /**
64
- * 删除文本标注
65
- */
66
- removeTextAnnotation(index: number): boolean;
67
- /**
68
- * 清除所有文本标注
69
- */
70
- clearTextAnnotations(): void;
71
- /**
72
- * 开始编辑文本标注
73
- */
74
- startEditing(index: number): boolean;
75
- /**
76
- * 完成编辑
77
- */
78
- finishEditing(): boolean;
79
- /**
80
- * 取消编辑
81
- */
82
- cancelEditing(): boolean;
83
- /**
84
- * 删除正在编辑的文本标注
85
- */
86
- deleteEditingAnnotation(): boolean;
87
- /**
88
- * 重置编辑状态
89
- */
90
- private resetEditingState;
91
- /**
92
- * 调整输入框宽度
93
- */
94
- private adjustInputWidth;
95
- /**
96
- * 开始移动文本标注
97
- */
98
- startMoving(e: MouseEvent, index: number): boolean;
99
- /**
100
- * 移动文本标注
101
- */
102
- moveAnnotation(e: MouseEvent): boolean;
103
- /**
104
- * 完成移动
105
- */
106
- finishMoving(): void;
107
- /**
108
- * 检查是否点击了文本标注
109
- */
110
- checkTextClick(offsetX: number, offsetY: number): number | null;
111
- /**
112
- * 检查是否点击了文本标注(用于移动)
113
- */
114
- checkTextClickForMove(e: MouseEvent): {
115
- index: number;
116
- handled: boolean;
117
- };
118
- /**
119
- * 获取所有文本标注
120
- */
121
- getTextAnnotations(): TextAnnotation[];
122
- /**
123
- * 撤销文本标注
124
- */
125
- withdraw(): boolean;
126
- /**
127
- * 选中文本标注
128
- */
129
- selectTextAnnotation(index: number): boolean;
130
- /**
131
- * 取消选中文本标注
132
- */
133
- deselectTextAnnotation(): void;
134
- /**
135
- * 删除选中的文本标注
136
- */
137
- deleteSelectedTextAnnotation(): boolean;
138
- /**
139
- * 获取选中的文本标注信息
140
- */
141
- getSelectedTextAnnotation(): {
142
- index: number;
143
- data: TextAnnotation;
144
- } | null;
145
- /**
146
- * 销毁(清理 DOM)
147
- */
148
- destroy(): void;
149
- }
@@ -1,147 +0,0 @@
1
- /**
2
- * 类型定义模块
3
- * 包含所有标注库的类型定义
4
- */
5
- /** 点坐标 */
6
- export type Point = {
7
- x: number;
8
- y: number;
9
- };
10
- /** 矩形标注 */
11
- export type Rect = {
12
- start: Point;
13
- width: number;
14
- height: number;
15
- };
16
- /** 多边形顶点 */
17
- export type Polygon = {
18
- point: Point;
19
- };
20
- /** 文本标注 */
21
- export type TextAnnotation = {
22
- position: {
23
- x: number;
24
- y: number;
25
- };
26
- text: string;
27
- width: number;
28
- height: number;
29
- /** 文本标注的独立样式 */
30
- style?: Pick<TextStyle, 'font' | 'color' | 'backgroundColor'>;
31
- };
32
- /** 标注类型联合 */
33
- export type Shape = Rect | Polygon | TextAnnotation;
34
- /** 边线样式 */
35
- export type LineStyle = 'solid' | 'dashed' | 'dotted';
36
- /** 顶点样式 */
37
- export type VertexStyle = {
38
- size: number;
39
- fillColor: string;
40
- strokeColor: string;
41
- strokeWidth: number;
42
- shape: 'circle' | 'square' | 'diamond';
43
- };
44
- /** 标注样式 */
45
- export type AnnotationStyle = {
46
- strokeColor: string;
47
- lineWidth: number;
48
- lineStyle?: LineStyle;
49
- vertexStyle?: VertexStyle;
50
- };
51
- /** 操作记录 */
52
- export type Operate<T extends Shape> = {
53
- type: "rect" | "polygon" | "drag" | "text" | "";
54
- data: T[];
55
- status: "fullfilled" | "pending";
56
- style?: AnnotationStyle;
57
- };
58
- /** 绘制类型 */
59
- export type DrawType = "rect" | "polygon" | "drag" | "text" | "";
60
- /** 抽屉选项 */
61
- export type DrawerOptions = {
62
- id: string;
63
- drawType?: DrawType;
64
- useEvents?: boolean;
65
- /** 标注颜色配置 */
66
- annotationColor?: string | ColorConfig;
67
- /** 边线样式 */
68
- lineStyle?: LineStyle;
69
- /** 顶点样式 */
70
- vertexStyle?: Partial<VertexStyle>;
71
- /** 文本样式 */
72
- textStyle?: Partial<TextStyle>;
73
- };
74
- /** 选中标注信息 */
75
- export type SelectedAnnotation = {
76
- index: number;
77
- type: "rect" | "polygon" | "text";
78
- };
79
- /** 控制点信息 */
80
- export type ActiveHandle = {
81
- type: "rect-corner" | "polygon-vertex";
82
- index: number;
83
- };
84
- /** 事件处理器类型 */
85
- export type WheelEventListener = (event: WheelEvent) => void;
86
- export type MouseEventListener = (event: MouseEvent) => void;
87
- export type KeyboardEventListener = (event: KeyboardEvent) => void;
88
- /** 抽屉事件处理器集合 */
89
- export type DrawerEventHandlers = {
90
- wheel: WheelEventListener;
91
- mousedown: MouseEventListener;
92
- mousemove: MouseEventListener;
93
- mouseup: MouseEventListener;
94
- mouseleave: MouseEventListener;
95
- dblclick: MouseEventListener;
96
- click: MouseEventListener;
97
- keydown: KeyboardEventListener;
98
- };
99
- /** 文本输入框样式 */
100
- export type TextInputStyle = {
101
- border: string;
102
- borderRadius: string;
103
- padding: string;
104
- fontSize: string;
105
- fontFamily: string;
106
- backgroundColor: string;
107
- color: string;
108
- boxShadow: string;
109
- minWidth: string;
110
- maxWidth: string;
111
- };
112
- /** 文本样式配置 */
113
- export type TextStyle = {
114
- font: string;
115
- color: string;
116
- padding: number;
117
- backgroundColor: string;
118
- borderRadius: number;
119
- selectedBorderColor: string;
120
- selectedBackgroundColor: string;
121
- /** 文本输入框样式 */
122
- inputStyle?: Partial<TextInputStyle>;
123
- };
124
- /** 选中样式配置 */
125
- export type SelectionStyle = {
126
- strokeColor: string;
127
- fillColor: string;
128
- handleSize: number;
129
- handleColor: string;
130
- };
131
- /** 颜色配置 */
132
- export type ColorConfig = {
133
- rect?: string;
134
- polygon?: string;
135
- text?: string;
136
- default?: string;
137
- };
138
- /** 视口尺寸 */
139
- export type ViewportSize = {
140
- width: number;
141
- height: number;
142
- };
143
- /** 视图偏移 */
144
- export type ViewportOffset = {
145
- x: number;
146
- y: number;
147
- };
@@ -1,31 +0,0 @@
1
- /**
2
- * 工具函数模块
3
- * 包含几何计算、坐标转换等通用工具函数
4
- */
5
- import type { Point, Rect, Polygon } from './types';
6
- /**
7
- * 检测点是否在矩形内
8
- * 支持负宽高的情况(从右下往左上画)
9
- */
10
- export declare function isPointInRect(point: Point, rect: Rect): boolean;
11
- /**
12
- * 检测点是否在多边形内(射线法)
13
- */
14
- export declare function isPointInPolygon(point: Point, polygon: Polygon[]): boolean;
15
- /**
16
- * 从 URL 解析图片类型
17
- * 支持 png, jpg, jpeg, webp, gif, bmp, svg 等常见格式
18
- */
19
- export declare function getImageTypeFromUrl(urlString: string): string;
20
- /**
21
- * 将 base64 字符串转换为 File 对象
22
- */
23
- export declare function base64ToFile(base64Data: string, filename: string): File;
24
- /**
25
- * 将 base64 字符串转换为 Blob 对象(用于上传)
26
- */
27
- export declare function base64ToBlob(base64: string): Promise<Blob>;
28
- /**
29
- * 计算缩放增量
30
- */
31
- export declare function getZoomDelta(currentScale: number, zoomIn: boolean): number;