Skip to content
This repository has been archived by the owner on Mar 31, 2023. It is now read-only.

Commit

Permalink
feat(praparat): feat(praparat): add ability to zoom to fit
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
itigoore01 committed Oct 3, 2019
1 parent c4956ba commit 5451a37
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
18 changes: 17 additions & 1 deletion projects/praparat/src/lib/praparat.component.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
:host {
position: relative;
display: block;
width: 100%;
height: 100%;
}

.praparat-wrapper {
width: 100%;
height: 100%;
}

.praparat-wrapper,
.praparat-zoom-element {
position: absolute;
top: 0;
left: 0;
overflow: hidden;
z-index: 0;
}

.praparat-zoom-element {
transform-origin: 0 0;
will-change: transform;
// will-change: transform;
}
32 changes: 32 additions & 0 deletions projects/praparat/src/lib/praparat.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { DEFAULT_PRAPARAT_CONFIG, PraparatConfig } from './praparat-config';
*/
@Component({
selector: 'praparat',
exportAs: 'praparat',
templateUrl: './praparat.component.html',
styleUrls: ['./praparat.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
Expand Down Expand Up @@ -191,6 +192,37 @@ export class PraparatComponent implements OnDestroy, AfterViewInit {
});
}

zoomToFit(element: HTMLElement) {
const {
width: viewportWidth,
height: viewportHeight,
} = this.elementRef.nativeElement.getBoundingClientRect();

let {
top: targetTop,
left: targetLeft,
width: targetWidth,
height: targetHeight,
} = element.getBoundingClientRect();

const currentScale = this.model.scale;
const currentPan = this.model.panPoint;

targetTop = targetTop / currentScale - currentPan.y;
targetLeft = targetLeft / currentScale - currentPan.x;
targetWidth = targetWidth / currentScale;
targetHeight = targetHeight / currentScale;

this.model.zoom(Math.min(viewportWidth / targetWidth, viewportHeight / targetHeight));

const newScale = this.model.scale;

this.model.pan({
x: (viewportWidth - targetWidth * newScale) / newScale / 2 - targetLeft,
y: (viewportHeight - targetHeight * newScale) / newScale / 2 - targetTop,
});
}

private touchListToPoints(touchList: TouchList): Point[] {
const touches: Point[] = [];

Expand Down

0 comments on commit 5451a37

Please sign in to comment.