Skip to content

fitCover

Places content in a box, as small as possible while covering the whole box (CSS object-fit: cover). Shortcut for fit(content, box, { mode: 'cover', align }).

Available since v3.1.5

import { fitCover } from '@helpers4/geometry';
// or, from the all-in-one package (same code, one install):
import { fitCover } from 'helpers4/geometry';
fitCover(content: Size, box: Size, align?: FitAlign): Matrix
ParameterTypeDescription
contentSizeThe size of the content
boxSizeThe size of the box
alignFitAlignWhere to place it, centered by default — see FitAlign (optional)

Matrix — The content-to-box matrix (a/d scales, e/f position)

Same as fit(content, box, { mode: ‘cover’ }).

fitCover({ width: 200, height: 100 }, { width: 100, height: 100 })
// => { a: 1, b: 0, c: 0, d: 1, e: -50, f: 0 }

The third argument is the alignment, like CSS object-position.

fitCover({ width: 200, height: 100 }, { width: 100, height: 100 }, { x: 0, y: 0 })

Where fit places content in the space left over (or overflowing), as CSS object-position: 0 is left / top, 0.5 centered, 1 right / bottom.

interface FitAlign {
  x?: number;
  y?: number;
}

A 2D affine transformation, in the layout of CSS and SVG matrix(a, b, c, d, e, f):

x' = a·x + c·y + e
y' = b·x + d·y + f
interface Matrix {
  a: number;
  b: number;
  c: number;
  d: number;
  e: number;
  f: number;
}

A width and a height.

interface Size {
  height: number;
  width: number;
}

View source on GitHub