@@ -33,6 +33,11 @@ function OpenSeadragonComponent({
3333 bounds : viewport . getBounds ( ) ,
3434 flip : viewport . getFlip ( ) ,
3535 rotation : viewport . getRotation ( ) ,
36+ worldBounds : ( ( ) => {
37+ const homeBounds = viewport . viewer . world . getHomeBounds ( ) ;
38+
39+ return [ homeBounds . x , homeBounds . y , homeBounds . width , homeBounds . height ] ;
40+ } ) ( ) ,
3641 x : Math . round ( viewport . centerSpringX . target . value ) ,
3742 y : Math . round ( viewport . centerSpringY . target . value ) ,
3843 zoom : viewport . zoomSpring . target . value ,
@@ -105,17 +110,41 @@ function OpenSeadragonComponent({
105110 const bounds = viewerConfig . bounds || worldBounds ;
106111 if ( bounds && ! viewerConfig . x && ! viewerConfig . y && ! viewerConfig . zoom ) {
107112 const rect = new Openseadragon . Rect ( ...bounds ) ;
108- if ( rect . equals ( viewport . getBounds ( ) ) ) {
113+ if ( ! rect . equals ( viewport . getBounds ( ) ) ) {
109114 viewport . fitBounds ( rect , false ) ;
110115 }
111116 }
112117 } , [ initialViewportSet , setInitialBounds , viewerConfig , viewerRef , worldBounds ] ) ;
113118
119+ useEffect ( ( ) => {
120+ if ( ! osdConfig . preserveViewport ) return ;
121+ if ( ! viewerConfig ?. worldBounds || ! worldBounds ) return ;
122+
123+ const viewer = viewerRef . current ;
124+ if ( ! viewer ) return ;
125+ const { viewport } = viewer ;
126+
127+ const [ _x , _y , width , height ] = viewerConfig . worldBounds ;
128+ const [ _x1 , _y1 , width1 , height1 ] = worldBounds ;
129+
130+ const previousAspectRatio = ( 1.0 * width ) / height ;
131+ const newAspectRatio = ( 1.0 * width1 ) / height1 ;
132+
133+ if ( ( previousAspectRatio < ( 1 - osdConfig . resetViewportAfterAspectRatioDelta ) * newAspectRatio )
134+ || ( previousAspectRatio > ( 1 + osdConfig . resetViewportAfterAspectRatioDelta ) * newAspectRatio ) ) {
135+ const rect = new Openseadragon . Rect ( ...worldBounds ) ;
136+ if ( ! rect . equals ( viewport . getBounds ( ) ) ) {
137+ viewport . fitBounds ( rect , false ) ;
138+ }
139+ }
140+ } , [ osdConfig , viewerConfig , worldBounds , viewerRef ] ) ;
141+
114142 // initialize OSD stuff when this component is mounted
115143 useEffect ( ( ) => {
116144 const viewer = Openseadragon ( {
117145 element : ref . current ,
118146 ...osdConfig ,
147+ preserveViewportAspectRatio : undefined ,
119148 } ) ;
120149
121150 viewer . addHandler ( 'canvas-drag' , ( ) => {
0 commit comments