HTML Canvas Rotate

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

Definition

On an <canvas>, ctx.rotate(angle) multiplies the current transform by a clockwise rotation. The angle is in radians. The pivot is always the current origin — usually the top-left unless you translate() first.

To spin a shape around its own center: move the origin to that center, rotate, then draw relative to (0, 0). Isolate transforms with save() / restore().

Baseline · Widely available.

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

Radians, not degrees.

Passing 45 rotates ~45 radians (many turns), not 45°. Use (deg * Math.PI) / 180.

Syntax

rotate.js
ctx.rotate(angle); // radians, clockwise

// Degrees → radians
ctx.rotate((45 * Math.PI) / 180);

// Rotate around a point (cx, cy)
ctx.translate(cx, cy);
ctx.rotate(angle);
ctx.fillRect(-w / 2, -h / 2, w, h);

Rotation stacks with later draws until you restore or reset the matrix.

Key Methods

Core rotation tools:

Method / ideaDescriptionTry it
rotate(angle)Rotate the transform clockwise by angle radiansTry it
Degrees helper(deg * Math.PI) / 180Try it
Around centertranslate → rotate → draw at offsetTry it
save / restoreIsolate a rotation so later draws stay uprightTry it
Needle / handTranslate to pivot, rotate, draw a thin rectTry it

Global Attributes

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

Live Preview

Drag the angle. The blue rect rotates around its center via translate → rotate.

45°

Pivot marker at the rectangle center. Gray = unrotated reference.

Canvas

rotate((45 * Math.PI) / 180)

HTML Examples

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

Example 1 — Rotate Around the Origin

Without translate, the pivot is the canvas top-left corner.

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

  ctx.fillStyle = "#94a3b8";
  ctx.fillRect(100, 20, 100, 30);

  ctx.rotate((35 * Math.PI) / 180);
  ctx.fillStyle = "#2563eb";
  ctx.fillRect(100, 20, 100, 30);
</script>
Try It Yourself

How It Works

  1. Draw a gray rect with the default (unrotated) transform.
  2. Call rotate with 35° converted to radians.
  3. Draw the blue rect with the same coordinates — it swings around (0, 0).
  4. Origin stays top-left unless you translate.

Example 2 — Degrees Helper

Keep a small helper so angles stay readable.

HTML
<canvas id="demo" width="300" height="160" aria-label="Rotate using degrees helper"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const toRad = (deg) => (deg * Math.PI) / 180;

  ctx.translate(150, 80);
  ctx.rotate(toRad(60));
  ctx.fillStyle = "#16a34a";
  ctx.fillRect(-60, -15, 120, 30);
</script>
Try It Yourself

How It Works

  1. Define toRad so you can think in degrees.
  2. Translate to the pivot first.
  3. Rotate 60° via the helper.
  4. Draw centered with negative half-width/height.

Example 3 — Rotate Around a Shape’s Center

The usual pattern: translate to center, rotate, draw from (-w/2, -h/2).

HTML
<canvas id="demo" width="300" height="160" aria-label="Rotate rectangle around its center"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const cx = 150, cy = 80, w = 140, h = 40;

  ctx.fillStyle = "#cbd5e1";
  ctx.fillRect(cx - w / 2, cy - h / 2, w, h);

  ctx.translate(cx, cy);
  ctx.rotate((45 * Math.PI) / 180);
  ctx.fillStyle = "#7c3aed";
  ctx.fillRect(-w / 2, -h / 2, w, h);
</script>
Try It Yourself

How It Works

  1. Gray rect shows the unrotated position.
  2. translate(cx, cy) moves the origin to the center.
  3. rotate(45°) spins around that new origin.
  4. Draw with -w/2, -h/2 so the rect stays centered on the pivot.

Example 4 — Isolate with save / restore

Rotate one shape without affecting the next draw.

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

  ctx.save();
  ctx.translate(90, 80);
  ctx.rotate((-30 * Math.PI) / 180);
  ctx.fillStyle = "#ea580c";
  ctx.fillRect(-40, -15, 80, 30);
  ctx.restore();

  ctx.fillStyle = "#2563eb";
  ctx.fillRect(170, 65, 80, 30);
</script>
Try It Yourself

How It Works

  1. save() snapshots the current transform.
  2. Translate + rotate only for the orange rect.
  3. restore() puts the matrix back.
  4. Blue rect stays upright — it never inherited the rotation.

Example 5 — Clock Hand / Needle

Pivot at one end: translate to the hub, rotate, draw a thin bar upward.

HTML
<canvas id="demo" width="300" height="160" aria-label="Rotating needle around a hub"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const cx = 150, cy = 90;

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

  ctx.save();
  ctx.translate(cx, cy);
  ctx.rotate((-50 * Math.PI) / 180);
  ctx.fillStyle = "#dc2626";
  ctx.fillRect(-3, -70, 6, 70);
  ctx.restore();
</script>
Try It Yourself

How It Works

  1. Draw a hub at the pivot.
  2. Translate to the hub, then rotate.
  3. Draw the needle as a thin rect from y = -length to 0.
  4. Change the angle (or animate it) to sweep like a clock hand.

Edge Cases & Pitfalls

Most rotation bugs are degrees-vs-radians, wrong pivot, or forgetting to restore the transform.

units

Radians only

rotate(90) is not 90°. Always convert: (deg * Math.PI) / 180.

pivot

Origin is the pivot

Without translate, shapes orbit the top-left. Move the origin before rotating around a point.

order

Order matters

Translate then rotate ≠ rotate then translate. For “spin in place,” translate to the center first.

leak

Transforms leak

Later draws inherit the rotation. Use save()/restore() or setTransform(1,0,0,1,0,0).

Browser Support

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

✓ Baseline · Widely available

ctx.rotate

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support rotate. 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
rotate() Widely supported

Bottom line: Safe for production rotations, needles, and card flips on Canvas 2D everywhere modern browsers run.

Key Takeaways

  • Call: ctx.rotate(radians) (clockwise).
  • Convert: (deg * Math.PI) / 180.
  • Pivot: translate(cx, cy) then rotate.
  • Isolate: save() / restore().

One line: move pivot → rotate (radians) → draw → restore.

Default CSS Settings

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

default.css
canvas {
  display: inline;
}

Set bitmap size with HTML width/height. CSS-only sizing scales the display and can blur rotated edges.

Frequently Asked Questions

Radians. Convert with degrees * Math.PI / 180. Example: 45° → (45 * Math.PI) / 180.
Always the current canvas origin (0,0 of the transform), usually the top-left unless you translate first.
translate(cx, cy), then rotate(angle), then draw with coordinates relative to that center (often fillRect(-w/2, -h/2, w, h)). Use save()/restore() to isolate.
rotate() changes the transform for all later draws until you reset. Isolate with save()/restore(), or reset with setTransform(1, 0, 0, 1, 0, 0).
Clockwise for positive angles (in the default canvas coordinate system where Y increases downward).
ctx.rotate changes the Canvas 2D transform matrix for drawing calls. CSS rotate transforms the element or a DOM box — it does not rotate individual canvas paths.

Next: Canvas Scale

Learn ctx.scale() to enlarge, shrink, or flip drawings.

Canvas Scale →

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