@deepnoid/canvas 0.1.62 → 0.1.63

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.
@@ -58,6 +58,7 @@ export declare class AnnotationEngine {
58
58
  private drawAnnotations;
59
59
  getSelectedAnnotation(): Annotation | null;
60
60
  setSelectedAnnotation(target: Annotation | null): void;
61
+ clearAnnotations(): void;
61
62
  getPointerRenderState(): {
62
63
  mousePoint: import("../types").Point | null;
63
64
  } | null;
@@ -194,6 +194,10 @@ export class AnnotationEngine {
194
194
  this.drawAnnotationsCanvas();
195
195
  }
196
196
  }
197
+ clearAnnotations() {
198
+ this.annotations = [];
199
+ this.clearCanvas(this.annotationsCanvas);
200
+ }
197
201
  // ----------------- Interaction -----------------
198
202
  getPointerRenderState() {
199
203
  return (this.interactionController
@@ -34,14 +34,16 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
34
34
  enabled: enableHotkeys,
35
35
  });
36
36
  /* ---------- Image / Engine lifecycle ---------- */
37
+ const isInitialLoadRef = useRef(true);
38
+ const imageLoadingRef = useRef(false);
37
39
  useEffect(() => {
38
- const prevEngine = engineRef.current;
39
- const prevImageCanvasState = prevEngine?.getImageCanvasState();
40
- if (prevEngine) {
41
- prevEngine.destroy();
42
- engineRef.current = null;
43
- }
40
+ imageLoadingRef.current = true;
41
+ if (engineRef.current)
42
+ engineRef.current.clearAnnotations();
44
43
  if (!image?.trim()) {
44
+ engineRef.current?.destroy();
45
+ engineRef.current = null;
46
+ imageLoadingRef.current = false;
45
47
  return;
46
48
  }
47
49
  let cancelled = false;
@@ -50,6 +52,8 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
50
52
  if (cancelled || !imageCanvasRef.current || !annotationsCanvasRef.current) {
51
53
  return;
52
54
  }
55
+ const prevEngine = engineRef.current;
56
+ const prevImageCanvasState = prevEngine?.getImageCanvasState();
53
57
  const imageCanvasState = !resetOnImageChange && prevImageCanvasState
54
58
  ? prevImageCanvasState
55
59
  : {
@@ -64,8 +68,12 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
64
68
  dh: img.height,
65
69
  initZoom: 1,
66
70
  };
67
- const initialAnnotations = pendingAnnotationsRef.current ?? annotations ?? [];
71
+ const initialAnnotations = isInitialLoadRef.current
72
+ ? (pendingAnnotationsRef.current ?? annotations ?? [])
73
+ : (pendingAnnotationsRef.current ?? []);
68
74
  pendingAnnotationsRef.current = null;
75
+ isInitialLoadRef.current = false;
76
+ prevEngine?.destroy();
69
77
  engineRef.current = new AnnotationEngine({
70
78
  imageCanvas: imageCanvasRef.current,
71
79
  image: img,
@@ -81,12 +89,16 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
81
89
  onChange: () => forceRender((v) => v + 1),
82
90
  });
83
91
  engineRef.current.initImageCanvas(resetOnImageChange);
92
+ imageLoadingRef.current = false;
84
93
  onImageLoadSuccess?.();
85
94
  };
86
95
  img.onerror = () => {
87
96
  if (cancelled)
88
97
  return;
89
98
  pendingAnnotationsRef.current = null;
99
+ engineRef.current?.destroy();
100
+ engineRef.current = null;
101
+ imageLoadingRef.current = false;
90
102
  onImageLoadError?.(new Error(`Failed to load image: ${image}`));
91
103
  };
92
104
  img.src = image;
@@ -100,6 +112,10 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
100
112
  useEffect(() => {
101
113
  if (!annotations)
102
114
  return;
115
+ if (imageLoadingRef.current) {
116
+ pendingAnnotationsRef.current = annotations;
117
+ return;
118
+ }
103
119
  if (!engineRef.current) {
104
120
  pendingAnnotationsRef.current = annotations;
105
121
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepnoid/canvas",
3
- "version": "0.1.62",
3
+ "version": "0.1.63",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.cjs",