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

Commit f3d5041

Browse files
feat(image-viewer): pan if scaled
1 parent 0cea2f1 commit f3d5041

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/core/src/lib/image-viewer/image-viewer.component.ts

+36-8
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ export class ImageViewerComponent implements OnInit {
300300
.pipe(
301301
tap((event: any) => this.pinchCenter = event.center),
302302
flatMap(() => pinchPanMove
303-
));
303+
));
304304

305305
this.pinchPan = pinchPan$
306306
.subscribe((res: any) => {
@@ -330,14 +330,42 @@ export class ImageViewerComponent implements OnInit {
330330
}
331331
const moveV = event.center.y - this.pinchCenter.y;
332332
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-
`);
338333
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;
341369
}
342370

343371
setBottom(bottom: number) {

0 commit comments

Comments
 (0)