Skip to content

fitScaleDown

Places content in a box, like fitContain, but never enlarged (CSS object-fit: scale-down). Shortcut for fit(content, box, { mode: 'scale-down', align }).

Available since v3.1.5

import { fitScaleDown } from '@helpers4/geometry';
// or, from the all-in-one package (same code, one install):
import { fitScaleDown } from 'helpers4/geometry';
fitScaleDown(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: ‘scale-down’ }).

fitScaleDown({ width: 200, height: 100 }, { width: 100, height: 100 })
// => { a: 0.5, b: 0, c: 0, d: 0.5, e: 0, f: 25 }

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

fitScaleDown({ 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