Home Canvas Rotate HTML Canvas Rotate 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
Syntax 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.
Reference
Key Methods Core rotation tools:
Method / idea Description Try it rotate(angle)Rotate the transform clockwise by angle radians Try itDegrees helper (deg * Math.PI) / 180 Try itAround center translate → rotate → draw at offset Try itsave / restoreIsolate a rotation so later draws stay upright Try itNeedle / hand Translate to pivot, rotate, draw a thin rect Try it
Try it
Live Preview Drag the angle. The blue rect rotates around its center via translate → rotate.
Code
HTML Examples Short, copy-ready snippets. Use Try It Yourself for a live editor.
1. Around origin 2. Degrees 3. Around center 4. save / restore 5. Needle Example 1 — Rotate Around the Origin Without translate, the pivot is the canvas top-left corner.
<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> How It Works Draw a gray rect with the default (unrotated) transform.Call rotate with 35° converted to radians.Draw the blue rect with the same coordinates — it swings around (0, 0).Origin stays top-left unless you translate.Example 2 — Degrees Helper Keep a small helper so angles stay readable.
<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> How It Works Define toRad so you can think in degrees.Translate to the pivot first.Rotate 60° via the helper.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).
<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> How It Works Gray rect shows the unrotated position.translate(cx, cy) moves the origin to the center.rotate(45°) spins around that new origin.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.
<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> How It Works save() snapshots the current transform.Translate + rotate only for the orange rect.restore() puts the matrix back.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.
<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> How It Works Draw a hub at the pivot.Translate to the hub , then rotate.Draw the needle as a thin rect from y = -length to 0.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).
Compatibility
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.
Remember
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.
UA style
Default CSS Settings Rotation is a Canvas API concern. The <canvas> element still uses typical user-agent 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 Does rotate() use degrees or radians? Radians. Convert with degrees * Math.PI / 180. Example: 45° → (45 * Math.PI) / 180.
What is the center of rotation? Always the current canvas origin (0,0 of the transform), usually the top-left unless you translate first.
How do I rotate a shape around its center? 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.
Why did everything after rotate look skewed? 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).
Is rotation clockwise or counterclockwise? Clockwise for positive angles (in the default canvas coordinate system where Y increases downward).
How is rotate different from CSS rotate? 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 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