55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
export interface CardBounds {
|
|
minX: number;
|
|
maxX: number;
|
|
minY: number;
|
|
maxY: number;
|
|
}
|
|
|
|
export interface ComputeBoundsOptions {
|
|
width: number;
|
|
height: number;
|
|
canvasWidth: number;
|
|
canvasHeight: number;
|
|
padding: number;
|
|
shelfWidth?: number;
|
|
}
|
|
|
|
export const computeCardBounds = ({
|
|
width,
|
|
height,
|
|
canvasWidth,
|
|
canvasHeight,
|
|
padding,
|
|
shelfWidth = 0,
|
|
}: ComputeBoundsOptions): CardBounds => {
|
|
const halfW = width / 2;
|
|
const halfH = height / 2;
|
|
const shelfOffset = Math.max(shelfWidth, 0);
|
|
|
|
return {
|
|
minX: padding + halfW,
|
|
maxX: Math.max(padding + halfW, canvasWidth - shelfOffset - padding - halfW),
|
|
minY: padding + halfH,
|
|
maxY: Math.max(padding + halfH, canvasHeight - padding - halfH),
|
|
};
|
|
};
|
|
|
|
export const constrainDimensions = (width: number, height: number, maxDimension: number) => {
|
|
if (width <= maxDimension && height <= maxDimension) {
|
|
return { width, height };
|
|
}
|
|
|
|
const aspect = width / height;
|
|
if (width > height) {
|
|
return {
|
|
width: maxDimension,
|
|
height: maxDimension / aspect
|
|
};
|
|
} else {
|
|
return {
|
|
width: maxDimension * aspect,
|
|
height: maxDimension
|
|
};
|
|
}
|
|
};
|