HTML Canvas Translate

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

Definition

On an <canvas>, ctx.translate(x, y) moves the drawing origin. Positive x goes right; positive y goes down. Later coordinates are relative to that new origin.

Translation does not move pixels already painted — only future draws. It is the usual first step before rotate or scale around a point. Isolate with save/restore.

Baseline · Widely available.

translate is a stable Canvas 2D feature across modern browsers since about July 2015.

Transforms stack.

Each translate multiplies the current matrix. Forgotten calls shift everything that follows. Reset with restore(), setTransform(1,0,0,1,0,0), or an opposite translate.

Syntax

translate.js
ctx.translate(x, y);

// Place a shape by moving the origin first
ctx.translate(120, 40);
ctx.fillRect(0, 0, 80, 60);

// Pivot for rotate / scale
ctx.translate(cx, cy);
ctx.rotate(angle);
ctx.fillRect(-w / 2, -h / 2, w, h);

After translate(cx, cy), local (0, 0) is that canvas point.

Key Methods

Core translation tools:

Method / ideaDescriptionTry it
translate(x, y)Move the origin by x, yTry it
Draw at (0, 0)Place shapes after moving the originTry it
Stacked translatesEach call adds to the previous moveTry it
Pivot + rotateTranslate to center, then rotateTry it
save / restoreIsolate a temporary moveTry it
Loop placementTranslate in a loop to tile shapesTry it

Global Attributes

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

Live Preview

Move X and Y. The blue rect is drawn at (0, 0) after translate. Gray is the unmoved reference.

110
40

Each frame resets the matrix, then applies one translate.

Canvas

translate(110, 40)

HTML Examples

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

Example 1 — Move, Then Draw at (0, 0)

Translate the origin, then fill a rect in local coordinates.

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

  ctx.fillStyle = "#94a3b8";
  ctx.fillRect(0, 0, 80, 60);

  ctx.translate(120, 40);
  ctx.fillStyle = "#2563eb";
  ctx.fillRect(0, 0, 80, 60);
</script>
Try It Yourself

How It Works

  1. Gray rect uses the default origin (top-left).
  2. translate(120, 40) moves the origin.
  3. fillRect(0, 0, …) now starts at that new origin.
  4. Same local size — only the origin changed.

Example 2 — Stacked Translates

Each call adds to the previous move.

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

  ctx.fillStyle = "#cbd5e1";
  ctx.fillRect(0, 0, 50, 40);

  ctx.translate(70, 20);
  ctx.fillStyle = "#7c3aed";
  ctx.fillRect(0, 0, 50, 40);

  ctx.translate(70, 30);
  ctx.fillStyle = "#16a34a";
  ctx.fillRect(0, 0, 50, 40);
</script>
Try It Yourself

How It Works

  1. First rect at the default origin.
  2. First translate places the purple rect.
  3. Second translate is relative to the already-moved origin.
  4. Green ends farther than a single translate of the sum would if you reset between draws without care — here the moves add.

Example 3 — Translate, Then Rotate

Move to a pivot, rotate, draw centered on that point.

HTML
<canvas id="demo" width="300" height="160" aria-label="Translate then rotate around pivot"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const cx = 150, cy = 80;

  ctx.beginPath();
  ctx.arc(cx, cy, 4, 0, Math.PI * 2);
  ctx.fillStyle = "#0f172a";
  ctx.fill();

  ctx.save();
  ctx.translate(cx, cy);
  ctx.rotate((35 * Math.PI) / 180);
  ctx.fillStyle = "#ea580c";
  ctx.fillRect(-60, -15, 120, 30);
  ctx.restore();
</script>
Try It Yourself

How It Works

  1. Mark the pivot with a small circle.
  2. translate(cx, cy) moves the origin there.
  3. rotate spins around that origin.
  4. Draw from -w/2, -h/2 so the rect stays centered on the pivot.

Example 4 — Isolate with save / restore

Translate one shape without shifting the next draw.

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

  ctx.save();
  ctx.translate(40, 50);
  ctx.fillStyle = "#2563eb";
  ctx.fillRect(0, 0, 90, 60);
  ctx.restore();

  ctx.fillStyle = "#16a34a";
  ctx.fillRect(160, 50, 90, 60);
</script>
Try It Yourself

How It Works

  1. save() snapshots the current matrix.
  2. Translate and draw the blue rect.
  3. restore() puts the origin back.
  4. Green rect uses absolute canvas coordinates again.

Example 5 — Place Items in a Loop

Step the origin across columns without changing local shape code.

HTML
<canvas id="demo" width="300" height="160" aria-label="Translate loop of squares"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const colors = ["#2563eb", "#7c3aed", "#16a34a", "#ea580c"];

  ctx.translate(25, 50);
  for (let i = 0; i < colors.length; i++) {
    ctx.fillStyle = colors[i];
    ctx.fillRect(0, 0, 40, 40);
    ctx.translate(65, 0);
  }
</script>
Try It Yourself

How It Works

  1. Start near the left with an initial translate.
  2. Each iteration draws the same local square at (0, 0).
  3. translate(65, 0) steps to the next column.
  4. Great for rows of icons or repeated UI chips.

Edge Cases & Pitfalls

Most translate bugs are stacked moves that were never restored, or confusing translate with moving existing pixels.

future

Only future draws move

translate changes the matrix for later calls. It does not slide bitmaps already on the canvas.

stack

Calls accumulate

Repeated translates add up. Isolate with save/restore, or reset the matrix between independent scenes.

order

Order with rotate/scale

Translate-then-rotate is the usual “spin in place” pattern. Rotate-then-translate orbits around the old origin.

y

Y increases downward

Positive y is down on canvas, matching the default 2D coordinate system.

Browser Support

ctx.translate() is part of the Canvas 2D transform API in every modern browser.

✓ Baseline · Widely available

ctx.translate

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support translate. Internet Explorer 9+ also supports it.

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+
Supported
translate() Widely supported

Bottom line: Safe for production origin moves, pivots, and layout loops on Canvas 2D everywhere modern browsers run.

Key Takeaways

  • Call: ctx.translate(x, y) moves the origin.
  • Then draw in local coords (often from 0, 0).
  • Pivot: translate before rotate/scale.
  • Isolate: save() / restore().

One line: move origin → draw local → restore.

Default CSS Settings

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

default.css
canvas {
  display: inline;
}

CSS positioning of the element is separate from the Canvas 2D matrix used by translate().

Frequently Asked Questions

It moves the canvas origin x units right and y units down (negative values go left/up). Later drawing coordinates are relative to that new origin.
No. It changes the transform for future draws only. Already painted pixels stay put.
After translate(cx, cy), (0, 0) is that point on the canvas. It is a common pattern for placing shapes by moving the origin first.
Yes. Each translate multiplies the current matrix. Two calls of translate(10, 0) move 20px right total unless you restore or reset.
Translate to the pivot, then rotate, then draw relative to (0, 0). Wrap with save()/restore() so later draws stay upright.
Use restore() after save(), call setTransform(1,0,0,1,0,0) / resetTransform(), or translate by the opposite amounts.

Next: Canvas Rotate

Learn ctx.rotate() in radians, usually after a translate to the pivot.

Canvas Rotate →

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