@deepnoid/canvas 0.1.62 → 0.1.64
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.
|
@@ -34,14 +34,14 @@ 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
|
-
|
|
39
|
-
const prevImageCanvasState = prevEngine?.getImageCanvasState();
|
|
40
|
-
if (prevEngine) {
|
|
41
|
-
prevEngine.destroy();
|
|
42
|
-
engineRef.current = null;
|
|
43
|
-
}
|
|
40
|
+
imageLoadingRef.current = true;
|
|
44
41
|
if (!image?.trim()) {
|
|
42
|
+
engineRef.current?.destroy();
|
|
43
|
+
engineRef.current = null;
|
|
44
|
+
imageLoadingRef.current = false;
|
|
45
45
|
return;
|
|
46
46
|
}
|
|
47
47
|
let cancelled = false;
|
|
@@ -50,6 +50,8 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
|
|
|
50
50
|
if (cancelled || !imageCanvasRef.current || !annotationsCanvasRef.current) {
|
|
51
51
|
return;
|
|
52
52
|
}
|
|
53
|
+
const prevEngine = engineRef.current;
|
|
54
|
+
const prevImageCanvasState = prevEngine?.getImageCanvasState();
|
|
53
55
|
const imageCanvasState = !resetOnImageChange && prevImageCanvasState
|
|
54
56
|
? prevImageCanvasState
|
|
55
57
|
: {
|
|
@@ -64,8 +66,12 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
|
|
|
64
66
|
dh: img.height,
|
|
65
67
|
initZoom: 1,
|
|
66
68
|
};
|
|
67
|
-
const initialAnnotations =
|
|
69
|
+
const initialAnnotations = isInitialLoadRef.current
|
|
70
|
+
? (pendingAnnotationsRef.current ?? annotations ?? [])
|
|
71
|
+
: (pendingAnnotationsRef.current ?? []);
|
|
68
72
|
pendingAnnotationsRef.current = null;
|
|
73
|
+
isInitialLoadRef.current = false;
|
|
74
|
+
prevEngine?.destroy();
|
|
69
75
|
engineRef.current = new AnnotationEngine({
|
|
70
76
|
imageCanvas: imageCanvasRef.current,
|
|
71
77
|
image: img,
|
|
@@ -81,12 +87,16 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
|
|
|
81
87
|
onChange: () => forceRender((v) => v + 1),
|
|
82
88
|
});
|
|
83
89
|
engineRef.current.initImageCanvas(resetOnImageChange);
|
|
90
|
+
imageLoadingRef.current = false;
|
|
84
91
|
onImageLoadSuccess?.();
|
|
85
92
|
};
|
|
86
93
|
img.onerror = () => {
|
|
87
94
|
if (cancelled)
|
|
88
95
|
return;
|
|
89
96
|
pendingAnnotationsRef.current = null;
|
|
97
|
+
engineRef.current?.destroy();
|
|
98
|
+
engineRef.current = null;
|
|
99
|
+
imageLoadingRef.current = false;
|
|
90
100
|
onImageLoadError?.(new Error(`Failed to load image: ${image}`));
|
|
91
101
|
};
|
|
92
102
|
img.src = image;
|
|
@@ -100,6 +110,10 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
|
|
|
100
110
|
useEffect(() => {
|
|
101
111
|
if (!annotations)
|
|
102
112
|
return;
|
|
113
|
+
if (imageLoadingRef.current) {
|
|
114
|
+
pendingAnnotationsRef.current = annotations;
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
103
117
|
if (!engineRef.current) {
|
|
104
118
|
pendingAnnotationsRef.current = annotations;
|
|
105
119
|
return;
|