Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
feat(image-viewer): pan if scaled
Browse files Browse the repository at this point in the history
  • Loading branch information
garygrossgarten committed Nov 27, 2018
1 parent 0cea2f1 commit f3d5041
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 36 additions & 8 deletions projects/core/src/lib/image-viewer/image-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class ImageViewerComponent implements OnInit {
.pipe(
tap((event: any) => this.pinchCenter = event.center),
flatMap(() => pinchPanMove
));
));

this.pinchPan = pinchPan$
.subscribe((res: any) => {
Expand Down Expand Up @@ -330,14 +330,42 @@ export class ImageViewerComponent implements OnInit {
}
const moveV = event.center.y - this.pinchCenter.y;
const moveH = event.center.x - this.pinchCenter.x;
const newTop = this.top + moveV;
const newLeft = this.left + moveH;
console.log(`
lastTop: ${this.top}, lastLeft: ${this.left}, moveV: ${moveV}, moveH: ${moveH}, newTop: ${newTop}, newLeft: ${newLeft}
`);
this.pinchCenter = event.center;
this.setTop(newTop);
this.setLeft(newLeft);
const newX = this.restrictRawPosX(this.left + moveH);
const newY = this.restrictRawPosY(this.top + moveV);

this.setTop(newY);
this.setLeft(newX);
}

restrictRawPosX(pos) {
const viewportDim = this.platform.width();
const imageWidth = this.getCurrentImageWidth() / this.scale;
const borderPos = (this.getCurrentImageWidth() - Math.min(viewportDim, imageWidth)) / 2;
if (pos < borderPos * -1) {
return borderPos * -1;
} else if (pos > borderPos) {
return borderPos;
}
return pos;
}

restrictRawPosY(pos) {
const viewportDim = this.platform.height();
const imageHeight = this.getCurrentImageHeight() / this.scale;
const offset = this.platform.height() / 2;
let borderPos = pos;
if (this.getCurrentImageHeight() > this.platform.height()) {
borderPos = (this.getCurrentImageHeight() - Math.max(viewportDim, imageHeight)) / 2;
} else {
borderPos = (this.getCurrentImageHeight() - Math.min(viewportDim, imageHeight)) / 2;
}
if (pos < borderPos * -1 + offset) {
return borderPos * -1 + offset;
} else if (pos > borderPos + offset) {
return borderPos + offset;
}
return pos;
}

setBottom(bottom: number) {
Expand Down

0 comments on commit f3d5041

Please sign in to comment.