Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 50 ยท 2D Transformations

Reflection, Shear and Inverse Transforms

Learn reflection and shear matrices, how to invert any affine transform, and how Canvas setTransform uses the same math, with live examples.

Translation, scaling and rotation cover most movement, but two more transformations appear constantly: reflection, which mirrors a shape, and shear, which slants it like italic text. Both fit the same 3 by 3 matrix scheme from homogeneous coordinates.

The third topic of this lesson looks backwards. Sooner or later you must undo a transformation: you click on a rotated, scaled drawing and need to know which point of the original shape you hit, or you want to restore an object to its starting position. That needs the inverse of a matrix. At the end, you see how the HTML Canvas API exposes exactly this math through setTransform.

Reflection

A reflection produces the mirror image of a shape. The mirror can be a line or a point. Reflections use scale factors of -1, so each of them is a diagonal or near-diagonal matrix:

TEXT
About the x-axis       About the y-axis       About the origin
| 1   0  0 |           | -1  0  0 |           | -1   0  0 |
| 0  -1  0 |           |  0  1  0 |           |  0  -1  0 |
| 0   0  1 |           |  0  0  1 |           |  0   0  1 |

Reflecting about the x-axis keeps x and negates y: a point above the axis jumps to the same distance below it. Reflecting about the y-axis negates x. Reflecting about the origin negates both, which is the same as rotating by 180ยฐ.

For a diagonal mirror line the matrices swap coordinates:

TEXT
About the line y = x         About the line y = -x
| 0  1  0 |                  |  0  -1  0 |
| 1  0  0 |                  | -1   0  0 |
| 0  0  1 |                  |  0   0  1 |

Reflecting about y = x sends (3, 1) to (1, 3): the coordinates trade places. In general, the reflection about a line through the origin at angle ฮฑ is

TEXT
| cos 2ฮฑ   sin 2ฮฑ  0 |
| sin 2ฮฑ  -cos 2ฮฑ  0 |
|   0        0     1 |

Check it: ฮฑ = 0 (the x-axis) gives the first matrix above, and ฮฑ = 45ยฐ gives the y = x matrix. To mirror about a line that does not pass through the origin, use the sandwich pattern you know: translate the line to the origin, reflect, translate back.

A reflection has a side effect that is easy to miss: it reverses the winding order of polygons. A triangle listed counter-clockwise becomes clockwise. Matrices with a negative determinant (the signed area scale factor) mirror shapes. Later, in back-face culling, this matters because renderers decide which side of a triangle is visible from its winding.

Shear

A shear slides each point in one direction by an amount proportional to its position in the other direction. An x-shear with factor shx is

TEXT
x' = x + shx * y
y' = y
Matrix form Effect
[1 shx 0; 0 1 0; 0 0 1] x-shear: slants vertical edges sideways
[1 0 0; shy 1 0; 0 0 1] y-shear: slants horizontal edges up or down

The row y = 0 stays where it is, and the higher a point is, the farther it slides. A square becomes a parallelogram: the base stays put and the top moves sideways. The area of the shape does not change (the determinant is 1), but angles do. Typographers use an x-shear of about 0.2 to make a fake italic from an upright font, and 3D games use shears for cheap lean effects.

flowchart LR
  S["Square"] -->|"x-shear 0.5"| P["Parallelogram<br/>same base, same height"]
  S -->|"reflect about y-axis"| M["Mirrored square"]
  S -->|"reflect about y = x"| D["Diagonal mirror"]

Shear slants a shape without changing its area; reflection flips it.

Try the transformations

Click the buttons to apply each transformation to the letter "F". The grey outline is the original and the blue shape is the result. The matrix is shown beside the picture. Because the F is asymmetric, you can see at once whether it was mirrored (reflections reverse its handedness) or only rotated (which never mirrors it).

Reflections and shears

Look at the determinant: both axis reflections and both diagonal reflections are -1, the origin reflection is +1 (a 180ยฐ rotation does not mirror), and shears are +1.

Inverse transforms

The inverse of a transformation M is the transformation Mโปยน that undoes it, so that Mโปยน * M = I, the identity matrix. Each basic transformation has an easy inverse:

Transformation Inverse
Translate by (tx, ty) Translate by (-tx, -ty)
Scale by (sx, sy) Scale by (1/sx, 1/sy) (fails if a factor is 0)
Rotate by ฮธ Rotate by -ฮธ, which is also the transpose of the matrix
Reflect The same reflection (mirroring twice gives the original)
Shear by shx Shear by -shx

For a chain, the inverse reverses the order: to undo "rotate, then translate" you first undo the translation and then undo the rotation. In symbols, (B * A)โปยน = Aโปยน * Bโปยน. It is like getting dressed: you put on socks and then shoes, so you take off shoes first and socks second.

For an arbitrary affine matrix with a 2 by 2 part [a b; c d] and translation (tx, ty), the inverse comes from the 2 by 2 inverse and the offset:

TEXT
det = a*d - b*c
Aโปยน = (1/det) * [ d  -b ; -c  a ]
t'  = -Aโปยน * (tx, ty)

If det = 0 the transformation squashes the plane into a line or a point (for example a scale by 0), information is lost, and no inverse exists. A matrix like that is called singular.

Where inverses are needed

  • Picking. The mouse position is in screen space. To find which point of an object was clicked, multiply the mouse point by the inverse of the object's matrix and test it against the original shape.
  • Undo and animation. To return to the start pose, apply the inverse.
  • Cameras. A camera's view matrix is the inverse of the matrix that places the camera in the world. We use this in the camera and view transformation.
  • Texture mapping. Renderers often walk through destination pixels and use the inverse to find the source pixel to read.

The console example below builds a matrix, inverts it with the formulas above, and checks the results.

JS Inverting an affine matrix

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Canvas setTransform

The Canvas 2D API stores one transformation matrix for everything you draw. It is exactly the 3 by 3 affine matrix of this chapter, with six numbers:

JavaScript
ctx.setTransform(a, b, c, d, e, f);   // replace the current matrix
ctx.transform(a, b, c, d, e, f);      // multiply it into the current matrix

Canvas applies x' = a*x + c*y + e and y' = b*x + d*y + f. Note the order: b and c are swapped compared with the row-major layout we used so far, because Canvas lists the matrix column by column. The calls translate, scale and rotate are shortcuts that build the matrix for you. ctx.transform multiplies the new matrix on the right of the current one, so the last call acts first on your shape's points. This is the same order rule as in the last lesson. For instance translate(100, 50) followed by rotate(0.5) rotates the shape about its own origin and then moves it there.

The second demo draws a shape with ctx.transform (a rotation, a shear and a move) in a canvas whose own base matrix flips y, so you can work in Cartesian units. When you move the mouse, the code inverts the full matrix to convert the mouse position into the shape's local coordinates and checks whether you are over the square.

Canvas transform and picking with the inverse

The square is slanted and turned on the screen, yet the hit test is a simple range check on the local coordinates. The inverse matrix did the hard part.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which matrix reflects a shape about the y-axis?

    Reflecting about the y-axis negates x and keeps y.
  2. 2. What does the determinant of a reflection matrix equal?

    A reflection mirrors the shape, which shows up as a determinant of -1. It also reverses the winding order of polygons.
  3. 3. What is the inverse of a rotation matrix by ฮธ?

    Rotating back by -ฮธ undoes the turn, and for rotation matrices the inverse equals the transpose.
  4. 4. You applied "scale, then rotate, then translate". In which order must you undo these steps?

    The inverse of a product reverses the order: (C B A)^-1 = A^-1 B^-1 C^-1.
  5. 5. Why can the matrix for "scale x by 0" not be inverted?

    A zero determinant means different points map to the same point, so there is no unique way back.

Summary

  • Reflections use -1 factors (axes, origin, lines like y = x) and have a determinant of -1 unless they are a 180ยฐ rotation; they reverse polygon winding.
  • Shear slants a shape by adding a multiple of one coordinate to the other without changing area.
  • The inverse undoes a transform: negate translations, reciprocate scales, negate angles, and reverse the order for a product.
  • An affine matrix is invertible when its 2x2 determinant is not zero.
  • ctx.setTransform(a, b, c, d, e, f) uses the same math with the matrix listed column by column; the inverse is the tool for picking.

Next, we map a region of the world onto a region of the screen: Window-to-Viewport Mapping.