@deepnoid/canvas 0.1.67 → 0.1.69

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.
@@ -12,6 +12,8 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
12
12
  const annotationsCanvasRef = useRef(null);
13
13
  const engineRef = useRef(null);
14
14
  const pendingAnnotationsRef = useRef(null);
15
+ const imageLoadingRef = useRef(false);
16
+ const currentImageRef = useRef(image);
15
17
  const [, forceRender] = useState(0);
16
18
  /* ---------- Resize ---------- */
17
19
  useResizeObserver({
@@ -34,10 +36,18 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
34
36
  enabled: enableHotkeys,
35
37
  });
36
38
  /* ---------- Image / Engine lifecycle ---------- */
37
- const imageLoadingRef = useRef(false);
39
+ const engineIdRef = useRef(0);
38
40
  useEffect(() => {
41
+ const isImageChanged = currentImageRef.current !== image;
42
+ const prevImageCanvasState = engineRef.current?.getImageCanvasState();
43
+ if (isImageChanged) {
44
+ engineRef.current?.destroy();
45
+ engineRef.current = null;
46
+ pendingAnnotationsRef.current = null;
47
+ currentImageRef.current = image;
48
+ engineIdRef.current++;
49
+ }
39
50
  imageLoadingRef.current = true;
40
- pendingAnnotationsRef.current = null;
41
51
  if (!image?.trim()) {
42
52
  engineRef.current?.destroy();
43
53
  engineRef.current = null;
@@ -50,8 +60,6 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
50
60
  if (cancelled || !imageCanvasRef.current || !annotationsCanvasRef.current) {
51
61
  return;
52
62
  }
53
- const prevEngine = engineRef.current;
54
- const prevImageCanvasState = prevEngine?.getImageCanvasState();
55
63
  const imageCanvasState = !resetOnImageChange && prevImageCanvasState
56
64
  ? prevImageCanvasState
57
65
  : {
@@ -67,16 +75,19 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
67
75
  initZoom: 1,
68
76
  };
69
77
  const initialAnnotations = pendingAnnotationsRef.current ?? [];
70
- console.log('> AnnotationCanvas - initialAnnotations : ', initialAnnotations, ', pendingAnnotationsRef.current : ', pendingAnnotationsRef.current, ', annotations prop : ', annotations);
71
78
  pendingAnnotationsRef.current = null;
72
- prevEngine?.destroy();
79
+ const currentEngineId = engineIdRef.current;
73
80
  engineRef.current = new AnnotationEngine({
74
81
  imageCanvas: imageCanvasRef.current,
75
82
  image: img,
76
83
  imageCanvasState,
77
84
  annotationsCanvas: annotationsCanvasRef.current,
78
85
  annotations: initialAnnotations,
79
- setAnnotations: (next) => setAnnotations?.(next),
86
+ setAnnotations: (next) => {
87
+ if (engineIdRef.current === currentEngineId) {
88
+ setAnnotations?.(next);
89
+ }
90
+ },
80
91
  drawing,
81
92
  editable,
82
93
  panZoomEnabled,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepnoid/canvas",
3
- "version": "0.1.67",
3
+ "version": "0.1.69",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.cjs",