From e570e85e64d77320f1465997db5b325a08604abe Mon Sep 17 00:00:00 2001 From: Sanster Date: Sat, 9 Apr 2022 22:45:48 +0800 Subject: [PATCH] fix jumpy image load --- .../app/src/components/Editor/Editor.tsx | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/lama_cleaner/app/src/components/Editor/Editor.tsx b/lama_cleaner/app/src/components/Editor/Editor.tsx index 0b2d554..d987307 100644 --- a/lama_cleaner/app/src/components/Editor/Editor.tsx +++ b/lama_cleaner/app/src/components/Editor/Editor.tsx @@ -73,10 +73,11 @@ export default function Editor(props: EditorProps) { const [showOriginal, setShowOriginal] = useState(false) const [isInpaintingLoading, setIsInpaintingLoading] = useState(false) const [scale, setScale] = useState(1) - const [minScale, setMinScale] = useState() + const [minScale, setMinScale] = useState(1.0) const [sizeLimit, setSizeLimit] = useState(1080) const windowSize = useWindowSize() const viewportRef = useRef() + const [centered, setCentered] = useState(false) const [isDraging, setIsDraging] = useState(false) const [isMultiStrokeKeyPressed, setIsMultiStrokeKeyPressed] = useState(false) @@ -214,31 +215,38 @@ export default function Editor(props: EditorProps) { // Draw once the original image is loaded useEffect(() => { - if (!original) { + if (!isOriginalLoaded) { return } - if (isOriginalLoaded) { - const rW = windowSize.width / original.naturalWidth - const rH = (windowSize.height - TOOLBAR_SIZE) / original.naturalHeight - if (rW < 1 || rH < 1) { - const s = Math.min(rW, rH) - setMinScale(s) - setScale(s) - } else { - setMinScale(1) - } + const rW = windowSize.width / original.naturalWidth + const rH = (windowSize.height - TOOLBAR_SIZE) / original.naturalHeight - const imageSizeLimit = Math.max(original.width, original.height) - setSizeLimit(imageSizeLimit) - - if (context?.canvas) { - context.canvas.width = original.naturalWidth - context.canvas.height = original.naturalHeight - } - draw() + let s = 1.0 + if (rW < 1 || rH < 1) { + s = Math.min(rW, rH) } - }, [context?.canvas, draw, original, isOriginalLoaded, windowSize]) + setMinScale(s) + setScale(s) + + const imageSizeLimit = Math.max(original.width, original.height) + setSizeLimit(imageSizeLimit) + + if (context?.canvas) { + context.canvas.width = original.naturalWidth + context.canvas.height = original.naturalHeight + } + viewportRef.current?.centerView(s, 1) + setCentered(true) + draw() + }, [ + context?.canvas, + draw, + viewportRef, + original, + isOriginalLoaded, + windowSize, + ]) // Zoom reset const resetZoom = useCallback(() => { @@ -522,10 +530,6 @@ export default function Editor(props: EditorProps) { } } - if (!original || !scale || !minScale) { - return <> - } - return (