Skip to content

rectToRect

The translation and scale that put one rectangle exactly on another, for animating or fitting between two boxes. An axis where from has no extent is only translated (scale 1).

Available since v3.1.10

import { rectToRect } from '@helpers4/geometry';
// or, from the all-in-one package (same code, one install):
import { rectToRect } from 'helpers4/geometry';
rectToRect(from: Rect, to: Rect): Matrix
ParameterTypeDescription
fromRectThe rectangle to move
toRectThe rectangle to reach

Matrix — The matrix that maps from onto to

The matrix scales and moves the first rectangle exactly onto the second.

rectToRect({ x: 0, y: 0, width: 10, height: 10 }, { x: 5, y: 5, width: 20, height: 10 })
// => { a: 2, b: 0, c: 0, d: 1, e: 5, f: 5 }

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;
}

An axis-aligned rectangle: its top-left corner and its size. Width and height are expected to be non-negative.

interface Rect {
  height: number;
  width: number;
  x: number;
  y: number;
}

View source on GitHub