Home Canvas Translate HTML Canvas Translate 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
Syntax 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.
Reference
Key Methods Core translation tools:
Method / idea Description Try it translate(x, y)Move the origin by x, y Try itDraw at (0, 0) Place shapes after moving the origin Try itStacked translates Each call adds to the previous move Try itPivot + rotate Translate to center, then rotate Try itsave / restoreIsolate a temporary move Try itLoop placement Translate in a loop to tile shapes Try it
Try it
Live Preview Move X and Y. The blue rect is drawn at (0, 0) after translate. Gray is the unmoved reference.
Code
HTML Examples Short, copy-ready snippets. Use Try It Yourself for a live editor.
1. Basic 2. Stack 3. Pivot 4. save / restore 5. Loop Example 1 — Move, Then Draw at (0, 0) Translate the origin, then fill a rect in local coordinates.
<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> How It Works Gray rect uses the default origin (top-left).translate(120, 40) moves the origin.fillRect(0, 0, …) now starts at that new origin.Same local size — only the origin changed.Example 2 — Stacked Translates Each call adds to the previous move.
<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> How It Works First rect at the default origin.First translate places the purple rect.Second translate is relative to the already-moved origin.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.
<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> How It Works Mark the pivot with a small circle.translate(cx, cy) moves the origin there.rotate spins around that origin.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.
<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> How It Works save() snapshots the current matrix.Translate and draw the blue rect.restore() puts the origin back.Green rect uses absolute canvas coordinates again.Example 5 — Place Items in a Loop Step the origin across columns without changing local shape code.
<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> How It Works Start near the left with an initial translate.Each iteration draws the same local square at (0, 0).translate(65, 0) steps to the next column.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.
Compatibility
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.
Remember
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.
UA style
Default CSS Settings Translation is a Canvas API concern. The <canvas> element still uses typical user-agent CSS:
canvas {
display: inline;
} CSS positioning of the element is separate from the Canvas 2D matrix used by translate().
Frequently Asked Questions What does translate(x, y) do? 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.
Does translate move existing pixels? No. It changes the transform for future draws only. Already painted pixels stay put.
Why draw at (0, 0) after translate? 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.
Do translates stack? Yes. Each translate multiplies the current matrix. Two calls of translate(10, 0) move 20px right total unless you restore or reset.
How do I use translate with rotate? Translate to the pivot, then rotate, then draw relative to (0, 0). Wrap with save()/restore() so later draws stay upright.
How do I undo a translate? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion