fit-dimensions
Version:
A tiny utility function to fit a source rectangle within a target rectangle using popular object-fit modes (contain, cover, fill, none, scale-down). Maintains aspect ratio as needed. Useful for images, videos, UI elements, and more.
22 lines (21 loc) • 892 B
TypeScript
export type FitDimensionsMode = 'contain' | 'cover' | 'fill' | 'none' | 'scale-down';
export interface FitDimensionsReturn {
width: number;
height: number;
x: number;
y: number;
}
/**
* @description Fit a source rectangle within a target rectangle
*
* @param srcWidth Source/original width
* @param srcHeight Source/original height
* @param maxWidth Target width
* @param maxHeight Target height
* @param mode One of *contain* | *cover* | *fill* | *none* | *scale-down*. (optional) (default: *contain*)
*
* @returns `{ width: number, height: number, x: number, y: number }` (`x` and `y` are the position used to center the fitted rectangle)
*
* @see https://github.com/GloryWong/fit-dimensions#readme
*/
export declare function fitDimensions(srcWidth: number, srcHeight: number, maxWidth: number, maxHeight: number, mode?: FitDimensionsMode): FitDimensionsReturn;