Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue: fit:canvas may not generate in some cases #8750

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.d/20241128_112750_sekachev.bs_fixed_fit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Fixed

- fit:canvas event is not generated if to fit it from the controls sidebar
(<https://github.com/cvat-ai/cvat/pull/8750>)
24 changes: 15 additions & 9 deletions cvat-canvas/src/typescript/canvasModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,28 +687,34 @@ export class CanvasModelImpl extends MasterImpl implements CanvasModel {
public fit(): void {
const { angle } = this.data;

let updatedScale = this.data.scale;
if ((angle / 90) % 2) {
// 90, 270, ..
this.data.scale = Math.min(
updatedScale = Math.min(
this.data.canvasSize.width / this.data.imageSize.height,
this.data.canvasSize.height / this.data.imageSize.width,
);
} else {
this.data.scale = Math.min(
updatedScale = Math.min(
this.data.canvasSize.width / this.data.imageSize.width,
this.data.canvasSize.height / this.data.imageSize.height,
);
}

this.data.scale = Math.min(Math.max(this.data.scale, FrameZoom.MIN), FrameZoom.MAX);
this.data.top = this.data.canvasSize.height / 2 - this.data.imageSize.height / 2;
this.data.left = this.data.canvasSize.width / 2 - this.data.imageSize.width / 2;
updatedScale = Math.min(Math.max(updatedScale, FrameZoom.MIN), FrameZoom.MAX);
const updatedTop = this.data.canvasSize.height / 2 - this.data.imageSize.height / 2;
const updatedLeft = this.data.canvasSize.width / 2 - this.data.imageSize.width / 2;

// scale is changed during zooming or translating
// so, remember fitted scale to compute fit-relative scaling
this.data.fittedScale = this.data.scale;
if (updatedScale !== this.data.scale || updatedTop !== this.data.top || updatedLeft !== this.data.left) {
this.data.scale = updatedScale;
this.data.top = updatedTop;
this.data.left = updatedLeft;

this.notify(UpdateReasons.IMAGE_FITTED);
// scale is changed during zooming or translating
// so, remember fitted scale to compute fit-relative scaling
this.data.fittedScale = this.data.scale;
this.notify(UpdateReasons.IMAGE_FITTED);
}
}

public grid(stepX: number, stepY: number): void {
Expand Down
15 changes: 9 additions & 6 deletions cvat-canvas/src/typescript/canvasView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1651,12 +1651,6 @@ export class CanvasViewImpl implements CanvasView, Listener {
// Setup event handlers
this.canvas.addEventListener('dblclick', (e: MouseEvent): void => {
this.controller.fit();
this.canvas.dispatchEvent(
new CustomEvent('canvas.fit', {
bubbles: false,
cancelable: true,
}),
);
e.preventDefault();
});

Expand Down Expand Up @@ -1896,6 +1890,15 @@ export class CanvasViewImpl implements CanvasView, Listener {
}),
);
} else if ([UpdateReasons.IMAGE_ZOOMED, UpdateReasons.IMAGE_FITTED].includes(reason)) {
if (reason === UpdateReasons.IMAGE_FITTED) {
this.canvas.dispatchEvent(
new CustomEvent('canvas.fit', {
bubbles: false,
cancelable: true,
}),
);
}

this.moveCanvas();
this.transformCanvas();
} else if (reason === UpdateReasons.IMAGE_ROTATED) {
Expand Down
Loading