@@ -300,7 +300,7 @@ export class ImageViewerComponent implements OnInit {
300
300
. pipe (
301
301
tap ( ( event : any ) => this . pinchCenter = event . center ) ,
302
302
flatMap ( ( ) => pinchPanMove
303
- ) ) ;
303
+ ) ) ;
304
304
305
305
this . pinchPan = pinchPan$
306
306
. subscribe ( ( res : any ) => {
@@ -330,14 +330,42 @@ export class ImageViewerComponent implements OnInit {
330
330
}
331
331
const moveV = event . center . y - this . pinchCenter . y ;
332
332
const moveH = event . center . x - this . pinchCenter . x ;
333
- const newTop = this . top + moveV ;
334
- const newLeft = this . left + moveH ;
335
- console . log ( `
336
- lastTop: ${ this . top } , lastLeft: ${ this . left } , moveV: ${ moveV } , moveH: ${ moveH } , newTop: ${ newTop } , newLeft: ${ newLeft }
337
- ` ) ;
338
333
this . pinchCenter = event . center ;
339
- this . setTop ( newTop ) ;
340
- this . setLeft ( newLeft ) ;
334
+ const newX = this . restrictRawPosX ( this . left + moveH ) ;
335
+ const newY = this . restrictRawPosY ( this . top + moveV ) ;
336
+
337
+ this . setTop ( newY ) ;
338
+ this . setLeft ( newX ) ;
339
+ }
340
+
341
+ restrictRawPosX ( pos ) {
342
+ const viewportDim = this . platform . width ( ) ;
343
+ const imageWidth = this . getCurrentImageWidth ( ) / this . scale ;
344
+ const borderPos = ( this . getCurrentImageWidth ( ) - Math . min ( viewportDim , imageWidth ) ) / 2 ;
345
+ if ( pos < borderPos * - 1 ) {
346
+ return borderPos * - 1 ;
347
+ } else if ( pos > borderPos ) {
348
+ return borderPos ;
349
+ }
350
+ return pos ;
351
+ }
352
+
353
+ restrictRawPosY ( pos ) {
354
+ const viewportDim = this . platform . height ( ) ;
355
+ const imageHeight = this . getCurrentImageHeight ( ) / this . scale ;
356
+ const offset = this . platform . height ( ) / 2 ;
357
+ let borderPos = pos ;
358
+ if ( this . getCurrentImageHeight ( ) > this . platform . height ( ) ) {
359
+ borderPos = ( this . getCurrentImageHeight ( ) - Math . max ( viewportDim , imageHeight ) ) / 2 ;
360
+ } else {
361
+ borderPos = ( this . getCurrentImageHeight ( ) - Math . min ( viewportDim , imageHeight ) ) / 2 ;
362
+ }
363
+ if ( pos < borderPos * - 1 + offset ) {
364
+ return borderPos * - 1 + offset ;
365
+ } else if ( pos > borderPos + offset ) {
366
+ return borderPos + offset ;
367
+ }
368
+ return pos ;
341
369
}
342
370
343
371
setBottom ( bottom : number ) {
0 commit comments