fix jumpy image load

This commit is contained in:
Sanster 2022-04-09 22:45:48 +08:00
parent 5183c21ad3
commit e570e85e64

View File

@ -73,10 +73,11 @@ export default function Editor(props: EditorProps) {
const [showOriginal, setShowOriginal] = useState(false) const [showOriginal, setShowOriginal] = useState(false)
const [isInpaintingLoading, setIsInpaintingLoading] = useState(false) const [isInpaintingLoading, setIsInpaintingLoading] = useState(false)
const [scale, setScale] = useState<number>(1) const [scale, setScale] = useState<number>(1)
const [minScale, setMinScale] = useState<number>() const [minScale, setMinScale] = useState<number>(1.0)
const [sizeLimit, setSizeLimit] = useState<number>(1080) const [sizeLimit, setSizeLimit] = useState<number>(1080)
const windowSize = useWindowSize() const windowSize = useWindowSize()
const viewportRef = useRef<ReactZoomPanPinchRef | undefined | null>() const viewportRef = useRef<ReactZoomPanPinchRef | undefined | null>()
const [centered, setCentered] = useState(false)
const [isDraging, setIsDraging] = useState(false) const [isDraging, setIsDraging] = useState(false)
const [isMultiStrokeKeyPressed, setIsMultiStrokeKeyPressed] = useState(false) const [isMultiStrokeKeyPressed, setIsMultiStrokeKeyPressed] = useState(false)
@ -214,31 +215,38 @@ export default function Editor(props: EditorProps) {
// Draw once the original image is loaded // Draw once the original image is loaded
useEffect(() => { useEffect(() => {
if (!original) { if (!isOriginalLoaded) {
return return
} }
if (isOriginalLoaded) { const rW = windowSize.width / original.naturalWidth
const rW = windowSize.width / original.naturalWidth const rH = (windowSize.height - TOOLBAR_SIZE) / original.naturalHeight
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 imageSizeLimit = Math.max(original.width, original.height) let s = 1.0
setSizeLimit(imageSizeLimit) if (rW < 1 || rH < 1) {
s = Math.min(rW, rH)
if (context?.canvas) {
context.canvas.width = original.naturalWidth
context.canvas.height = original.naturalHeight
}
draw()
} }
}, [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 // Zoom reset
const resetZoom = useCallback(() => { const resetZoom = useCallback(() => {
@ -522,10 +530,6 @@ export default function Editor(props: EditorProps) {
} }
} }
if (!original || !scale || !minScale) {
return <></>
}
return ( return (
<div <div
className="editor-container" className="editor-container"
@ -543,7 +547,7 @@ export default function Editor(props: EditorProps) {
wheel={{ step: 0.05 }} wheel={{ step: 0.05 }}
centerZoomedOut centerZoomedOut
alignmentAnimation={{ disabled: true }} alignmentAnimation={{ disabled: true }}
centerOnInit // centerOnInit
limitToBounds={false} limitToBounds={false}
doubleClick={{ disabled: true }} doubleClick={{ disabled: true }}
initialScale={minScale} initialScale={minScale}
@ -554,6 +558,9 @@ export default function Editor(props: EditorProps) {
> >
<TransformComponent <TransformComponent
contentClass={isInpaintingLoading ? 'editor-canvas-loading' : ''} contentClass={isInpaintingLoading ? 'editor-canvas-loading' : ''}
contentStyle={{
visibility: centered ? 'visible' : 'hidden',
}}
> >
<div className="editor-canvas-container"> <div className="editor-canvas-container">
<canvas <canvas