bug of image viewer is fixed

This commit is contained in:
richard-loafle 2020-02-12 13:34:46 +09:00
parent 01fef0930e
commit 2665252d8c
2 changed files with 49 additions and 5 deletions

View File

@ -171,7 +171,7 @@
*ngIf="fileDownloadUrl" *ngIf="fileDownloadUrl"
[src]="fileDownloadUrl" [src]="fileDownloadUrl"
[style.width]="'auto'" [style.width]="'auto'"
[style.height]="getImageHeight(imageContainer.clientHeight)" [style.height]="imageHeight + 'px'"
(load)="onLoadFileDownloadUrl(downloadImage)" (load)="onLoadFileDownloadUrl(downloadImage)"
/> />
</div> </div>

View File

@ -5,7 +5,9 @@ import {
Output, Output,
EventEmitter, EventEmitter,
ChangeDetectorRef, ChangeDetectorRef,
ChangeDetectionStrategy ChangeDetectionStrategy,
ViewChild,
ElementRef
} from '@angular/core'; } from '@angular/core';
import { ucapAnimations } from '../../animations'; import { ucapAnimations } from '../../animations';
import { FileEventJson } from '@ucap-webmessenger/protocol-event'; import { FileEventJson } from '@ucap-webmessenger/protocol-event';
@ -31,10 +33,17 @@ export class ImageViewerComponent implements OnInit {
@Output() @Output()
download = new EventEmitter<FileDownloadItem>(); download = new EventEmitter<FileDownloadItem>();
@ViewChild('imageContainer', { static: false })
imageContainer: ElementRef<HTMLElement>;
@ViewChild('downloadImage', { static: false })
downloadImage: ElementRef<HTMLElement>;
fileDownloadItem: FileDownloadItem; fileDownloadItem: FileDownloadItem;
naturalWidth = 0; naturalWidth = 0;
naturalHeight = 0; naturalHeight = 0;
imageHeight = 0;
zoomRatio = 100; zoomRatio = 100;
@ -58,6 +67,8 @@ export class ImageViewerComponent implements OnInit {
this.naturalWidth = img.naturalWidth; this.naturalWidth = img.naturalWidth;
this.naturalHeight = img.naturalHeight; this.naturalHeight = img.naturalHeight;
this.setImageHeight();
this.changeDetectorRef.detectChanges(); this.changeDetectorRef.detectChanges();
} }
@ -67,6 +78,8 @@ export class ImageViewerComponent implements OnInit {
} }
this.zoomRatio -= 10; this.zoomRatio -= 10;
this.setImageHeight();
this.changeDetectorRef.detectChanges(); this.changeDetectorRef.detectChanges();
} }
onClickZoomIn() { onClickZoomIn() {
@ -75,21 +88,52 @@ export class ImageViewerComponent implements OnInit {
} }
this.zoomRatio += 10; this.zoomRatio += 10;
this.setImageHeight();
this.changeDetectorRef.detectChanges(); this.changeDetectorRef.detectChanges();
} }
onClickZoomReset() { onClickZoomReset() {
this.zoomRatio = 100; this.zoomRatio = 100;
this.setImageHeight();
this.changeDetectorRef.detectChanges(); this.changeDetectorRef.detectChanges();
} }
getImageHeight(containerHeight: number): string { setImageHeight() {
const realContainerHeight = containerHeight - 20; const realContainerHeight =
this.imageContainer.nativeElement.clientHeight - 20;
const oriHeight = const oriHeight =
this.naturalHeight > realContainerHeight this.naturalHeight > realContainerHeight
? realContainerHeight ? realContainerHeight
: this.naturalHeight; : this.naturalHeight;
return oriHeight * (this.zoomRatio / 100) + 'px'; const oriWidth = (oriHeight * this.naturalWidth) / this.naturalHeight;
const imageHeight = oriHeight * (this.zoomRatio / 100);
const imageWidth = oriWidth * (this.zoomRatio / 100);
let imageTop =
(this.imageContainer.nativeElement.clientHeight - imageHeight) / 2;
let imageLeft =
(this.imageContainer.nativeElement.clientWidth - imageWidth) / 2;
let scrollTop = 0;
if (0 > imageTop) {
scrollTop = Math.abs(imageTop);
imageTop = 0;
}
let scrollLeft = 0;
if (0 > imageLeft) {
scrollLeft = Math.abs(imageLeft);
imageLeft = 0;
}
this.downloadImage.nativeElement.style.top = `${imageTop}px`;
this.downloadImage.nativeElement.style.left = `${imageLeft}px`;
this.imageContainer.nativeElement.scrollTop = scrollTop;
this.imageContainer.nativeElement.scrollLeft = scrollLeft;
this.imageHeight = imageHeight;
} }
} }