HTML Canvas Transform

Beginner
9 min read
Updated: Nov 2026
5 examples
Live preview

Definition

On an <canvas>, every draw goes through a current transformation matrix. Helpers like translate, rotate, and scale change that matrix. transform(a, b, c, d, e, f) multiplies it by a custom matrix; setTransform(…) replaces it.

Use the matrix API for skew, one-shot resets, or combining effects. Prefer the named helpers when you only need move, turn, or zoom.

Baseline · Widely available.

transform and setTransform are stable Canvas 2D features across modern browsers since about July 2015.

Baseline · resetTransform() (Jan 2020+).

resetTransform() is widely available in modern browsers since about January 2020. Equivalent: setTransform(1, 0, 0, 1, 0, 0).

Multiplies, does not replace.

transform() stacks on the current matrix. Calling it twice compounds. Use setTransform / resetTransform / save+restore when you need a clean slate.

Syntax

transform.js
// Multiply current matrix
ctx.transform(a, b, c, d, e, f);

// Replace matrix (identity → then apply)
ctx.setTransform(a, b, c, d, e, f);

// Back to identity
ctx.resetTransform();
// or: ctx.setTransform(1, 0, 0, 1, 0, 0);

Mapped point: (ax + cy + e, bx + dy + f). Identity is (1, 0, 0, 1, 0, 0).

Key Methods

Core transform tools:

Method / paramDescriptionTry it
transform(a,b,c,d,e,f)Multiply current matrix by this matrixTry it
setTransform(a,b,c,d,e,f)Reset to identity, then applyTry it
a, dScale X / Y (when b, c are 0)Try it
b, cSkew (vertical / horizontal shear)Try it
e, fTranslate X / YTry it
resetTransform()Identity matrix in one callTry it

Global Attributes

The <canvas> tag also supports the Global Attributes in HTML.

Live Preview

Adjust horizontal skew (c). Gray is untransformed; blue uses transform(1, 0, c, 1, 0, 0).

0.40

Each draw starts from identity via setTransform, then applies one skew.

Canvas

transform(1, 0, 0.4, 1, 0, 0)

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Skew with transform

Shear a rectangle by changing b and c.

HTML
<canvas id="demo" width="300" height="160" aria-label="Skew rectangle with transform"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.fillStyle = "#cbd5e1";
  ctx.fillRect(40, 40, 100, 80);

  ctx.transform(1, 0.15, 0.5, 1, 40, 20);
  ctx.fillStyle = "#2563eb";
  ctx.fillRect(40, 40, 100, 80);
</script>
Try It Yourself

How It Works

  1. Gray rect uses the identity matrix.
  2. transform(1, 0.15, 0.5, 1, 40, 20) adds skew + a small translate.
  3. b / c shear vertical / horizontal.
  4. Blue rect is drawn in that skewed space.

Example 2 — setTransform Replaces

Reset cleanly so a second matrix does not compound the first.

HTML
<canvas id="demo" width="300" height="160" aria-label="setTransform vs transform multiply"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.transform(1, 0, 0.4, 1, 0, 0);
  ctx.fillStyle = "#94a3b8";
  ctx.fillRect(30, 50, 80, 60);

  // Replaces (does not multiply on top)
  ctx.setTransform(1, 0, -0.4, 1, 0, 0);
  ctx.fillStyle = "#16a34a";
  ctx.fillRect(170, 50, 80, 60);
</script>
Try It Yourself

How It Works

  1. First transform skews to the right for the gray box.
  2. setTransform starts from identity again.
  3. Opposite skew applies only to the green box.
  4. Without setTransform, the second call would multiply both skews.

Example 3 — Scale via a and d

When b and c are 0, a/d act like scale X/Y.

HTML
<canvas id="demo" width="300" height="160" aria-label="Scale with setTransform matrix"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.fillStyle = "#cbd5e1";
  ctx.fillRect(20, 20, 40, 30);

  ctx.setTransform(2.5, 0, 0, 2, 20, 20);
  ctx.fillStyle = "#7c3aed";
  ctx.fillRect(20, 20, 40, 30);
</script>
Try It Yourself

How It Works

  1. Identity draw shows the small gray rect.
  2. setTransform(2.5, 0, 0, 2, 20, 20) scales and translates.
  3. Same local coords become a larger purple rect.
  4. Same idea as scale(), but expressed as a matrix.

Example 4 — Translate via e and f

Keep scale at 1 and move with the last two arguments.

HTML
<canvas id="demo" width="300" height="160" aria-label="Translate with setTransform"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.fillStyle = "#94a3b8";
  ctx.fillRect(20, 50, 70, 50);

  ctx.setTransform(1, 0, 0, 1, 120, 20);
  ctx.fillStyle = "#ea580c";
  ctx.fillRect(20, 50, 70, 50);
</script>
Try It Yourself

How It Works

  1. Gray rect at the original position.
  2. e = 120, f = 20 shift the origin.
  3. Same fillRect(20, 50, …) lands farther right/down.
  4. Equivalent to translate(120, 20) after a reset.

Example 5 — resetTransform

Clear a skewed matrix so the next shape is upright again.

HTML
<canvas id="demo" width="300" height="160" aria-label="resetTransform after skew"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.transform(1, 0, 0.55, 1, 0, 0);
  ctx.fillStyle = "#2563eb";
  ctx.fillRect(30, 40, 90, 70);

  if (ctx.resetTransform) {
    ctx.resetTransform();
  } else {
    ctx.setTransform(1, 0, 0, 1, 0, 0);
  }

  ctx.fillStyle = "#16a34a";
  ctx.fillRect(170, 40, 90, 70);
</script>
Try It Yourself

How It Works

  1. Skew and draw the blue rect.
  2. resetTransform() returns to identity (fallback shown for older engines).
  3. Green rect is axis-aligned again.
  4. Also common: wrap temporary matrices in save()/restore().

Edge Cases & Pitfalls

Most matrix bugs are compounding transforms or confusing helpers with transform().

stack

transform compounds

Each transform() multiplies the current matrix. Unexpected skew or scale usually means a prior call was not reset.

order

Order matters

Translate then rotate ≠ rotate then translate. Plan the sequence, or use setTransform with a fully computed matrix.

helpers

Prefer helpers when possible

For everyday move/turn/zoom, translate/rotate/scale are clearer than raw a–f values.

leak

Isolate with save/restore

Temporary matrices should sit inside save()/restore() so later draws stay in identity (or the prior state).

Browser Support

transform and setTransform are part of Canvas 2D everywhere modern. resetTransform is widely available since about January 2020.

✓ Baseline · Widely available

transform / setTransform

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the matrix APIs. Internet Explorer 9+ supports transform/setTransform; use setTransform(1,0,0,1,0,0) where resetTransform is missing.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+
transform/setTransform
transform() Widely supported

Bottom line: Safe for production matrix transforms. Prefer setTransform(1,0,0,1,0,0) as a reset fallback when needed.

Key Takeaways

  • Multiply: transform(a,b,c,d,e,f).
  • Replace: setTransform(…) or resetTransform().
  • Meaning: a/d scale, b/c skew, e/f translate.
  • Isolate: save() / restore().

One line: set or multiply the matrix → draw → reset or restore.

Default CSS Settings

Transforms are a Canvas API concern. The <canvas> element still uses typical user-agent CSS:

default.css
canvas {
  display: inline;
}

CSS transform on the element scales the whole bitmap visually; it does not change the Canvas 2D matrix used by drawing calls.

Frequently Asked Questions

It multiplies the current transformation matrix by the matrix made from those six values. That can scale, skew, rotate, and translate in one step.
setTransform resets to the identity matrix first, then applies the given matrix. transform multiplies on top of whatever is already there.
a and d scale X/Y (when b and c are 0). b and c skew. e and f translate. A point (x, y) becomes (ax + cy + e, bx + dy + f).
For simple moves, turns, and zooms, the helpers are clearer. Use transform/setTransform for skew, custom matrices, or one-shot resets.
Call setTransform(1, 0, 0, 1, 0, 0), resetTransform(), or wrap changes in save()/restore().
Yes. Matrices multiply; translate-then-rotate is not the same as rotate-then-translate. Prefer save/restore around temporary matrices.

Next: Canvas Translate

Learn ctx.translate() to move the drawing origin.

Canvas Translate →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful