How matrixTransform Applies SVGMatrix to SVGPoint

The matrixTransform method transforms the coordinates of an SVGPoint (or DOMPoint) by multiplying it with an SVGMatrix (or DOMMatrix). This article explains the underlying affine transformation mathematics, the calculation formulas used, and how this method maps points between different coordinate systems in SVG rendering.

Mathematical Representation

An SVG coordinate transformation uses a 3x3 affine transformation matrix to handle 2D operations like translation, rotation, scaling, and skewing. An SVGMatrix contains six component values: a, b, c, d, e, and f.

The matrix is structured as follows:

| a  c  e |
| b  d  f |
| 0  0  1 |

The SVGPoint represents a 2D coordinate \((x, y)\), augmented with a homogeneous coordinate of \(1\):

| x |
| y |
| 1 |

The Transformation Formula

When point.matrixTransform(matrix) is executed, the method performs standard matrix multiplication between the 3x3 matrix and the 3x1 vector:

| x' |   | a  c  e |   | x |
| y' | = | b  d  f | * | y |
| 1  |   | 0  0  1 |   | 1 |

This matrix multiplication evaluates to the following equations for the new coordinates \((x', y')\):

Component Roles

Practical Example

Consider a point at coordinates \((10, 20)\) transformed by a matrix that scales by \(2\) and translates by \((5, 10)\):

Applying matrixTransform:

The method returns a new SVGPoint with the coordinates \((25, 50)\).

Common Application: Coordinate Space Conversion

The primary use of matrixTransform in web applications is mapping screen coordinates (such as mouse event positions) to the local coordinate system of an SVG element.

This is accomplished by: 1. Creating an SVGPoint using the client coordinates (clientX, clientY). 2. Obtaining the Current Transformation Matrix using element.getScreenCTM(). 3. Inverting the matrix using matrix.inverse(). 4. Calling point.matrixTransform(invertedMatrix) to produce coordinates aligned with the SVG element’s internal coordinate grid.