Skip to content

Commit

Permalink
Merge pull request #5 from kctang/master
Browse files Browse the repository at this point in the history
Support image panning in zoom mode on touch-based device (Android)
  • Loading branch information
nikini authored Jan 27, 2017
2 parents 95240d5 + a308278 commit 62382c0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/zoomable-image/zoomable-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export class ZoomableImage implements OnInit, OnDestroy {
width: 0,
height: 0,
};
private panCenterStart = {
x: 0, y: 0,
};

private imageElement: any;

Expand Down Expand Up @@ -90,6 +93,7 @@ export class ZoomableImage implements OnInit, OnDestroy {
this.gesture.on('pinch', e => this.pinchEvent(e));
this.gesture.on('pinchstart', e => this.pinchStartEvent(e));
this.gesture.on('pinchend', e => this.pinchEndEvent(e));
this.gesture.on('pan', e => this.panEvent(e));

// Scroll event
this.scrollListener = this.scrollEvent.bind(this);
Expand Down Expand Up @@ -215,6 +219,22 @@ export class ZoomableImage implements OnInit, OnDestroy {
this.animateScale(scale);
}

private panEvent(event) {
// calculate center x,y since pan started
const x = Math.max(Math.floor(this.panCenterStart.x + event.deltaX), 0);
const y = Math.max(Math.floor(this.panCenterStart.y + event.deltaY), 0);

this.centerStart.x = x;
this.centerStart.y = y;

if (event.isFinal) {
this.panCenterStart.x = x;
this.panCenterStart.y = y;
}

this.displayScale();
}

/**
* When the user is scrolling
*
Expand All @@ -236,6 +256,8 @@ export class ZoomableImage implements OnInit, OnDestroy {

this.centerStart.x = Math.max(event.center.x - this.position.x * this.scale, 0);
this.centerStart.y = Math.max(event.center.y - this.position.y * this.scale, 0);
this.panCenterStart.x = Math.max(event.center.x - this.position.x * this.scale, 0);
this.panCenterStart.y = Math.max(event.center.y - this.position.y * this.scale, 0);

this.centerRatio.x = Math.min((this.centerStart.x + this.scroll.x) / realImageWidth, 1);
this.centerRatio.y = Math.min((this.centerStart.y + this.scroll.y) / realImageHeight, 1);
Expand Down

0 comments on commit 62382c0

Please sign in to comment.