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

Receipts are displayed in the full size of canvas when not needed #42174

Merged
merged 8 commits into from
May 31, 2024
13 changes: 12 additions & 1 deletion src/components/MultiGestureCanvas/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import type {CanvasSize, ContentSize} from './types';

type ShouldResizeToFit = (canvasSize: CanvasSize, contentSize: ContentSize) => boolean;
type GetCanvasFitScale = (props: {canvasSize: CanvasSize; contentSize: ContentSize}) => {scaleX: number; scaleY: number; minScale: number; maxScale: number};

const shouldResizeToFit: ShouldResizeToFit = (canvasSize, contentSize) => {
// If the image is smaller than canvas should no fit to canvas scale
if (canvasSize && contentSize) {
return canvasSize.width < contentSize.width || canvasSize.height < contentSize.height;
}
return false;
};

const getCanvasFitScale: GetCanvasFitScale = ({canvasSize, contentSize}) => {
const shouldResize = shouldResizeToFit(canvasSize, contentSize);

const scaleX = canvasSize.width / contentSize.width;
const scaleY = canvasSize.height / contentSize.height;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get rid of shouldResizeToFit altogether

    const trueScaleX = canvasSize.width / contentSize.width;
    const trueScaleY = canvasSize.height / contentSize.height;
    const scaleX = trueScaleX > 1 ? 1 : trueScaleX;
    const scaleY = trueScaleY > 1 ? 1 : trueScaleY;

@samilabud, could you please test if this works?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even simpler, we can use an already existing NumberUtils.clamp instead of the ternary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that makes a lot of sense thank you!, it seems to be working:

receipt.resize.clamp.test.mp4

const minScale = Math.min(scaleX, scaleY);
const minScale = !shouldResize ? 1 : Math.min(scaleX, scaleY);
const maxScale = Math.max(scaleX, scaleY);

return {scaleX, scaleY, minScale, maxScale};
Expand Down
Loading