@deepnoid/canvas 0.1.10 → 0.1.12
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.
|
@@ -98,6 +98,18 @@ const XrayViewer = ({ image, hasZoom, coordinates, onImageLoadError, onImageLoad
|
|
|
98
98
|
onImageLoadError?.(new Error(errorMessage));
|
|
99
99
|
}
|
|
100
100
|
}, [calculateInitZoom, calculatorZoomPoint, onImageLoadError]);
|
|
101
|
+
const initWithPreservedState = useCallback((image) => {
|
|
102
|
+
try {
|
|
103
|
+
setDw(image.width);
|
|
104
|
+
setDh(image.height);
|
|
105
|
+
setImageError(null);
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
const errorMessage = error instanceof Error ? error.message : 'Canvas initialization failed';
|
|
109
|
+
setImageError(errorMessage);
|
|
110
|
+
onImageLoadError?.(new Error(errorMessage));
|
|
111
|
+
}
|
|
112
|
+
}, [onImageLoadError]);
|
|
101
113
|
const initCanvas = useCallback(async () => {
|
|
102
114
|
if (!image)
|
|
103
115
|
return;
|
|
@@ -130,17 +142,24 @@ const XrayViewer = ({ image, hasZoom, coordinates, onImageLoadError, onImageLoad
|
|
|
130
142
|
if (!image)
|
|
131
143
|
return;
|
|
132
144
|
const imageInfo = `${image.itemId}-${image.type}`;
|
|
145
|
+
const preserveViewState = hasZoom?.preserveViewState || false;
|
|
133
146
|
const loadImageAsync = async () => {
|
|
134
147
|
try {
|
|
135
148
|
setIsImageLoading(true);
|
|
136
149
|
setImageError(null);
|
|
137
150
|
const loadedImage = await loadImage(image.imagePath);
|
|
138
151
|
imageRef.current = loadedImage;
|
|
139
|
-
|
|
152
|
+
const isNewImage = prevImageInfo.current !== imageInfo || image.type === 'REALITY';
|
|
153
|
+
if (isNewImage) {
|
|
140
154
|
prevImageInfo.current = imageInfo;
|
|
141
155
|
const canvas_el = resolutionCanvas(viewer.current);
|
|
142
156
|
if (canvas_el) {
|
|
143
|
-
|
|
157
|
+
if (preserveViewState && initZoom !== 0) {
|
|
158
|
+
initWithPreservedState(loadedImage);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
init(canvas_el, loadedImage);
|
|
162
|
+
}
|
|
144
163
|
}
|
|
145
164
|
}
|
|
146
165
|
setImageOnloadCount((prev) => prev + 1);
|
|
@@ -150,6 +169,7 @@ const XrayViewer = ({ image, hasZoom, coordinates, onImageLoadError, onImageLoad
|
|
|
150
169
|
const errorMessage = error instanceof Error ? error.message : 'Image load failed';
|
|
151
170
|
setImageError(errorMessage);
|
|
152
171
|
imageRef.current = new Image();
|
|
172
|
+
prevImageInfo.current = undefined;
|
|
153
173
|
setZoom(0);
|
|
154
174
|
setInitZoom(0);
|
|
155
175
|
setMoveX(0);
|
|
@@ -162,6 +182,8 @@ const XrayViewer = ({ image, hasZoom, coordinates, onImageLoadError, onImageLoad
|
|
|
162
182
|
const canvas = viewer.current;
|
|
163
183
|
const ctx = canvas.getContext('2d');
|
|
164
184
|
if (ctx) {
|
|
185
|
+
canvas.width = canvas.offsetWidth;
|
|
186
|
+
canvas.height = canvas.offsetHeight;
|
|
165
187
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
166
188
|
}
|
|
167
189
|
}
|
|
@@ -179,7 +201,16 @@ const XrayViewer = ({ image, hasZoom, coordinates, onImageLoadError, onImageLoad
|
|
|
179
201
|
clearTimeout(loadTimeoutRef.current);
|
|
180
202
|
}
|
|
181
203
|
};
|
|
182
|
-
}, [
|
|
204
|
+
}, [
|
|
205
|
+
image,
|
|
206
|
+
loadImage,
|
|
207
|
+
init,
|
|
208
|
+
initWithPreservedState,
|
|
209
|
+
onImageLoadError,
|
|
210
|
+
onImageLoadSuccess,
|
|
211
|
+
hasZoom?.preserveViewState,
|
|
212
|
+
initZoom,
|
|
213
|
+
]);
|
|
183
214
|
useEffect(() => {
|
|
184
215
|
const _redraw = () => {
|
|
185
216
|
if (viewer.current && imageRef.current && !isImageLoading && !imageError) {
|
|
@@ -196,15 +227,21 @@ const XrayViewer = ({ image, hasZoom, coordinates, onImageLoadError, onImageLoad
|
|
|
196
227
|
useEffect(() => {
|
|
197
228
|
if (image && imageRef.current && !isImageLoading) {
|
|
198
229
|
const imageInfo = `${image.itemId}-${image.type}`;
|
|
230
|
+
const preserveViewState = hasZoom?.preserveViewState || false;
|
|
199
231
|
if (prevImageInfo.current === imageInfo) {
|
|
200
232
|
const canvas_el = resolutionCanvas(viewer.current);
|
|
201
233
|
if (canvas_el) {
|
|
202
|
-
|
|
203
|
-
|
|
234
|
+
if (preserveViewState && initZoom !== 0) {
|
|
235
|
+
setImageOnloadCount((prev) => prev + 1);
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
init(canvas_el, imageRef.current);
|
|
239
|
+
setImageOnloadCount((prev) => prev + 1);
|
|
240
|
+
}
|
|
204
241
|
}
|
|
205
242
|
}
|
|
206
243
|
}
|
|
207
|
-
}, [width, height, image, init, isImageLoading]);
|
|
244
|
+
}, [width, height, image, init, isImageLoading, hasZoom?.preserveViewState, initZoom]);
|
|
208
245
|
const handleWheel = (event) => {
|
|
209
246
|
if (!hasZoom || isImageLoading || imageError)
|
|
210
247
|
return;
|