Skip to content

Commit 91c501e

Browse files
committed
Pas adjustments
1 parent 92359c9 commit 91c501e

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

src/image-viewer-zoom-gesture.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DIRECTION_HORIZONTAL, DIRECTION_VERTICAL } from 'ionic-angular/gestures
44

55
import { ImageViewerComponent } from './image-viewer.component';
66

7-
const MAX_SCALE = 2;
7+
const MAX_SCALE = 3;
88

99
export class ImageViewerZoomGesture extends Gesture {
1010
private adjustScale = 1;
@@ -15,6 +15,9 @@ export class ImageViewerZoomGesture extends Gesture {
1515
private currentDeltaX = 0;
1616
private currentDeltaY = 0;
1717

18+
private allowedXMargin = 0;
19+
private allowedYMargin = 0;
20+
1821
constructor(private component: ImageViewerComponent, element: any, private platform: Platform, private renderer: Renderer) {
1922
super(element.nativeElement);
2023

@@ -24,6 +27,7 @@ export class ImageViewerZoomGesture extends Gesture {
2427
this.listen();
2528
this.on('pinch', (e) => this.onPinch(e));
2629
this.on('pinchend', (e) => this.onPinchEnd(e));
30+
this.on('panstart', (e) => this.onPanStart(e));
2731
this.on('pan', (e) => this.onPan(e));
2832
this.on('panend', (e) => this.onPanEnd(e));
2933
this.on('doubletap', (e) => this.onDoubleTap(e));
@@ -62,42 +66,39 @@ export class ImageViewerZoomGesture extends Gesture {
6266
this.adjustDeltaY = this.currentDeltaY;
6367
}
6468

65-
onPan(event) {
69+
onPanStart(event) {
6670
if (!this.component.isZoomed) {
6771
return;
6872
}
6973

70-
this.currentDeltaX = Math.floor(this.adjustDeltaX + event.deltaX);
71-
this.currentDeltaY = Math.floor(this.adjustDeltaY + event.deltaY);
74+
const originalImageWidth = this.element.offsetWidth;
75+
const originalImageHeight = this.element.offsetHeight;
7276

73-
this.setImageContainerTransform();
77+
this.allowedXMargin = ((originalImageWidth * this.currentScale) - originalImageWidth) / 4;
78+
this.allowedYMargin = ((originalImageHeight * this.currentScale) - originalImageHeight) / 4;
7479
}
7580

76-
onPanEnd(event) {
81+
onPan(event) {
7782
if (!this.component.isZoomed) {
7883
return;
7984
}
8085

81-
const imageWidth = this.element.offsetWidth;
82-
const imageHeight = this.element.offsetHeight;
86+
this.currentDeltaX = this.boundAdjustement(Math.floor(this.adjustDeltaX + event.deltaX), this.allowedXMargin);
87+
this.currentDeltaY = this.boundAdjustement(Math.floor(this.adjustDeltaY + event.deltaY), this.allowedYMargin);
8388

84-
const isOverOnX = (this.currentDeltaX > imageWidth) || (this.currentDeltaX < -imageWidth);
85-
const isOverOnY = (this.currentDeltaY < -imageHeight) || (this.currentDeltaY > imageHeight);
86-
87-
if (isOverOnX || isOverOnY) {
88-
const correctedX = Math.min(imageWidth, Math.max(this.currentDeltaX, -imageWidth));
89-
const correctedY = Math.min(imageHeight, Math.min(this.currentDeltaX, -imageHeight));
89+
this.setImageContainerTransform();
90+
}
9091

91-
new Animation(this.platform, this.element)
92-
.fromTo('translateX', `${this.currentDeltaX}px`, `${correctedX}px`)
93-
.fromTo('translateY', `${this.currentDeltaY}px`, `${correctedY}px`)
94-
.fromTo('scale', this.currentScale, this.currentScale)
95-
.easing('ease-in')
96-
.duration(50)
97-
.play();
92+
boundAdjustement(adjustement, bound) {
93+
if (adjustement > bound || adjustement < -bound) {
94+
return Math.min(bound, Math.max(adjustement, -bound));
95+
}
96+
return adjustement;
97+
}
9898

99-
this.currentDeltaX = correctedX;
100-
this.currentDeltaY = correctedY;
99+
onPanEnd(event) {
100+
if (!this.component.isZoomed) {
101+
return;
101102
}
102103

103104
this.adjustDeltaX = this.currentDeltaX;
@@ -107,7 +108,7 @@ export class ImageViewerZoomGesture extends Gesture {
107108
onDoubleTap(event) {
108109
this.component.isZoomed = !this.component.isZoomed;
109110
if (this.component.isZoomed) {
110-
this.currentScale = MAX_SCALE;
111+
this.currentScale = 2;
111112
} else {
112113
this.currentScale = 1;
113114

0 commit comments

Comments
 (0)