Canvas transform(): Scale, Skew, Move and Combine Transforms

transform(a, b, c, d, e, f) multiplies a new 2D transformation matrix into the current Canvas transform. It can combine scaling, skewing and translation in one operation.

transform() parameters Top ↑

ParameterMain effect
aHorizontal scale
bVertical skew
cHorizontal skew
dVertical scale
eHorizontal translation
fVertical translation
ctx.transform(a, b, c, d, e, f);

Interactive transform matrix Top ↑

Interactive Canvas transform matrix demo.

transform() vs setTransform() Top ↑

transform() multiplies its matrix into the current transform, so effects accumulate. setTransform() first resets to the identity transform and then applies the supplied matrix.

ctx.translate(80, 20);
ctx.transform(1, 0, 0.5, 1, 0, 0); // combines with translation

ctx.setTransform(1, 0, 0, 1, 80, 20); // replaces current transform

resetTransform() and save()/restore() Top ↑

ctx.save();
ctx.transform(1, 0.3, 0.4, 1, 80, 20);
ctx.fillRect(20, 20, 100, 50);
ctx.restore();

// Or reset all transforms explicitly
ctx.resetTransform();

Common transform mistakes Top ↑

  • Calling transform() repeatedly without realizing matrices accumulate.
  • Clearing a transformed Canvas without resetting the transform first, which may clear a transformed region rather than the full bitmap.
  • Confusing transform() with setTransform().
  • Forgetting that rotation is easier to read using rotate() and movement using translate() when a full matrix is unnecessary.

Frequently asked questions Top ↑

What does transform() do? Top ↑

It multiplies a six-parameter 2D matrix into the current Canvas transformation matrix.

What is the difference between transform() and setTransform()? Top ↑

transform() combines with the current matrix; setTransform() replaces it after resetting to identity.

How do I remove all Canvas transforms? Top ↑

Use resetTransform(), or restore() a state saved before the transforms.

Can transform() skew shapes? Top ↑

Yes. The b and c matrix components can produce vertical and horizontal skew.

Should I use transform() for simple movement? Top ↑

translate(), rotate() and scale() are usually clearer for simple operations; transform() is useful when you need a combined matrix.

translate() | rotate() | Sine animation example | Canvas tutorial





plus2net.com










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer