Skip to content

multiply

Multiplies matrices, in the mathematical order: the last one applies first. It is the order of a CSS transform list, so multiply(translate(10, 0), rotate(r)) is transform: translate(10px) rotate(r): rotate, then translate.

In other words, applyToPoint(multiply(m1, m2), p) equals applyToPoint(m1, applyToPoint(m2, p)).

Available since v3.1.5

import { multiply } from '@helpers4/geometry';
// or, from the all-in-one package (same code, one install):
import { multiply } from 'helpers4/geometry';
multiply(matrices: readonly Matrix[]): Matrix
ParameterTypeDescription
matricesreadonly Matrix[]The matrices, outermost first

Matrix — Their product, or the identity for none

The last matrix applies first: here, scale then translate.

// transform: translate(10px, 0) scale(2)
applyToPoint(multiply(translate(10, 0), scale(2)), { x: 1, y: 1 }) // => { x: 12, y: 2 }

Move the point to the origin, scale, move it back.

const m = multiply(translate(50, 50), scale(2), translate(-50, -50));
applyToPoint(m, { x: 60, y: 50 }) // => { x: 70, y: 50 }

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

View source on GitHub