Skip to content

Commit

Permalink
Fix: Prevent code from running unless there's a change in img element…
Browse files Browse the repository at this point in the history
… size on resize window
  • Loading branch information
loiddy committed Jul 27, 2024
1 parent dbd76d9 commit 7bf39eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,20 @@ export class ImageCropperComponent implements OnChanges, OnInit {
if (!this.state.loadedImage) {
return;
}
if (this.hidden) {
if (this.state.hidden) {
this.resizedWhileHidden = true;
} else {
const oldMaxSize = {...this.state.maxSize};
this.setMaxSize();
cropperPosition.resizeCropperAccordingToNewMaxSize(this.state, oldMaxSize);
cropperSizeBounds.setAllInternalSizes(this.state);
this.settingsUpdated.emit(this.state.getDeepCopyOfSettings());
this.cd.markForCheck();
if (oldMaxSize.width !== this.state.maxSize.width) {
cropperPosition.resizeCropperAccordingToNewMaxSize(
this.state,
this.state.maxSize.width / oldMaxSize.width
);
cropperSizeBounds.setAllInternalSizes(this.state);
this.settingsUpdated.emit(this.state.getDeepCopyOfSettings());
this.cd.markForCheck();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CropperPosition, MoveStart, BasicEvent, Dimensions } from '../interfaces';
import { CropperPosition, MoveStart, BasicEvent } from '../interfaces';
import { HammerInput } from './hammer.utils';
import { ImageCropperState } from '../state/image-cropper-state';

Expand Down Expand Up @@ -320,13 +320,11 @@ export function aspectRatioIsCorrect(state: ImageCropperState): boolean {
return currentCropAspectRatio === state.aspectRatio;
}

export function resizeCropperAccordingToNewMaxSize(state: ImageCropperState, oldMaxSize: Dimensions): void {
if (oldMaxSize.width !== state.maxSize.width || oldMaxSize.height !== state.maxSize.height) {
state.cropper.x1 = state.cropper.x1 * state.maxSize.width / oldMaxSize.width;
state.cropper.x2 = state.cropper.x2 * state.maxSize.width / oldMaxSize.width;
state.cropper.y1 = state.cropper.y1 * state.maxSize.height / oldMaxSize.height;
state.cropper.y2 = state.cropper.y2 * state.maxSize.height / oldMaxSize.height;
}
export function resizeCropperAccordingToNewMaxSize(state: ImageCropperState, ratio: number): void {
state.cropper.x1 *= ratio;
state.cropper.x2 *= ratio;
state.cropper.y1 *= ratio;
state.cropper.y2 *= ratio;
}

export function getClientX(event: Event | BasicEvent | TouchEvent | HammerInput): number {
Expand Down

0 comments on commit 7bf39eb

Please sign in to comment.