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
20 changes: 11 additions & 9 deletions src/components/MultiGestureCanvas/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ import type {CanvasSize, ContentSize} from './types';

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

/** Clamps a value between a lower and upper bound */
function clamp(value: number, lowerBound: number, upperBound: number) {
'worklet';

return Math.min(Math.max(lowerBound, value), upperBound);
}

const getCanvasFitScale: GetCanvasFitScale = ({canvasSize, contentSize}) => {
const scaleX = canvasSize.width / contentSize.width;
const scaleY = canvasSize.height / contentSize.height;
const trueScaleX = canvasSize.width / contentSize.width;
const trueScaleY = 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 scaleX = clamp(trueScaleX, 0, 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const scaleX = clamp(trueScaleX, 0, 1);
const scaleX = clamp(canvasSize.width / contentSize.width, 0, 1);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

const scaleY = clamp(trueScaleY, 0, 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const scaleY = clamp(trueScaleY, 0, 1);
const scaleY = clamp(canvasSize.height / contentSize.height, 0, 1);

const minScale = Math.min(scaleX, scaleY);
const maxScale = Math.max(scaleX, scaleY);

return {scaleX, scaleY, minScale, maxScale};
};

/** Clamps a value between a lower and upper bound */
function clamp(value: number, lowerBound: number, upperBound: number) {
'worklet';

return Math.min(Math.max(lowerBound, value), upperBound);
}

export {getCanvasFitScale, clamp};
Loading