Skip to content

fit

Places content in a container like CSS object-fit and object-position, e.g. to show a design frame in a viewport of another size.

The result is the matrix from content coordinates to container coordinates: a and d are the scales, e and f the position of the content’s top-left corner. Map points with Matrix::apply, combine it with Matrix::multiply, or write it with Matrix::to_css. Sizes are expected to be positive.

use helpers4::geometry::fit;

Cargo feature geometry (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features geometry

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.9", default-features = false, features = ["geometry"] }
pub fn fit(content: Size, container: Size, mode: FitMode, align: FitAlign) -> Matrix
ParameterTypeDescription
contentSizeThe size of the content.
containerSizeThe size of the container.
modeFitModeHow to size the content.
alignFitAlignWhere to place it.

Matrix — The content-to-container matrix.

use helpers4::geometry::{fit, FitAlign, FitMode, Matrix, Size};

let m = fit(Size::new(200.0, 100.0), Size::new(100.0, 100.0), FitMode::Contain, FitAlign::CENTER);
assert_eq!(m, Matrix::new(0.5, 0.0, 0.0, 0.5, 0.0, 25.0));

let left = fit(Size::new(200.0, 100.0), Size::new(100.0, 100.0), FitMode::Cover, FitAlign::new(0.0, 0.5));
assert_eq!(left, Matrix::new(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)); // cropped on the right

src/geometry/fit.rs