HTML Canvas Scale

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

Definition

On an <canvas>, ctx.scale(x, y) multiplies the current transform so one unit becomes x pixels wide and y pixels tall. By default, one unit is one pixel.

Scaling is relative to the current origin. To enlarge a shape in place, translate() to its center first, then scale, then draw. Negative factors flip the drawing. Isolate with save() / restore().

Baseline · Widely available.

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

Positions and lineWidth scale too.

After scale(2, 2), a point at (10, 10) maps farther from the origin, and strokes look thicker. Plan pivots with translate, and restore when done.

Syntax

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

// Enlarge around a point
ctx.translate(cx, cy);
ctx.scale(sx, sy);
ctx.fillRect(-w / 2, -h / 2, w, h);

// Flip horizontally
ctx.scale(-1, 1);

x = 1, y = 1 means no change. Values stack until you restore or reset the matrix.

Key Methods

Core scaling tools:

Method / ideaDescriptionTry it
scale(x, y)Scale horizontal and vertical unitsTry it
Around centertranslate → scale → draw at offsetTry it
Non-uniformDifferent x and y stretch the shapeTry it
Negative flipscale(-1, 1) or scale(1, -1)Try it
save / restoreIsolate scale so later draws stay normal sizeTry it

Global Attributes

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

Live Preview

Drag the scale. The blue rect grows or shrinks around its center.

1.50×

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

Canvas

scale(1.5, 1.5)

HTML Examples

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

Example 1 — Basic scale

Scale units from the origin, then draw a small rect that appears larger.

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

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

  ctx.scale(3, 2);
  ctx.fillStyle = "#2563eb";
  ctx.fillRect(20, 20, 40, 30);
</script>
Try It Yourself

How It Works

  1. Gray rect is drawn at normal size.
  2. scale(3, 2) makes X units 3× and Y units 2×.
  3. Same coordinates (20, 20, 40, 30) become a larger, stretched blue rect.
  4. Position moves because the corner is also scaled from the origin.

Example 2 — Scale Around a Center

Translate to the pivot first so the shape grows in place.

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

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

  ctx.translate(cx, cy);
  ctx.scale(1.6, 1.6);
  ctx.fillStyle = "#7c3aed";
  ctx.fillRect(-w / 2, -h / 2, w, h);
</script>
Try It Yourself

How It Works

  1. Gray rect shows the unscaled size.
  2. translate(cx, cy) moves the origin to the center.
  3. scale(1.6, 1.6) enlarges around that pivot.
  4. Draw from -w/2, -h/2 so the rect stays centered.

Example 3 — Non-Uniform Stretch

Different X and Y factors squash or stretch the shape.

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

  ctx.translate(150, 80);
  ctx.scale(2.2, 0.7);
  ctx.beginPath();
  ctx.arc(0, 0, 40, 0, Math.PI * 2);
  ctx.fillStyle = "#16a34a";
  ctx.fill();
</script>
Try It Yourself

How It Works

  1. Translate to center so the ellipse stays on-canvas.
  2. scale(2.2, 0.7) widens X and shrinks Y.
  3. Draw a circle at (0, 0) — it becomes an ellipse.
  4. Useful for ovals without switching to ellipse().

Example 4 — Horizontal Flip

Negative X flips the drawing across the vertical axis.

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

  ctx.font = "bold 28px sans-serif";
  ctx.fillStyle = "#94a3b8";
  ctx.fillText("Hello", 40, 90);

  ctx.scale(-1, 1);
  ctx.fillStyle = "#ea580c";
  ctx.fillText("Hello", -260, 90);
</script>
Try It Yourself

How It Works

  1. Draw normal text on the left for comparison.
  2. scale(-1, 1) mirrors the X axis.
  3. Use a negative X in fillText so the mirrored text lands on-canvas.
  4. scale(1, -1) flips vertically the same way.

Example 5 — Isolate with save / restore

Scale one shape without changing later draws or stroke thickness forever.

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

  ctx.save();
  ctx.translate(80, 80);
  ctx.scale(1.8, 1.8);
  ctx.fillStyle = "#2563eb";
  ctx.fillRect(-25, -20, 50, 40);
  ctx.restore();

  ctx.strokeStyle = "#0f172a";
  ctx.lineWidth = 4;
  ctx.strokeRect(160, 50, 100, 60);
</script>
Try It Yourself

How It Works

  1. save() snapshots the current transform.
  2. Translate + scale only for the blue fill.
  3. restore() resets units to 1×1.
  4. Stroke stays 4px — it never inherited the scale.

Edge Cases & Pitfalls

Most scale bugs are unexpected position shifts, thick strokes, or forgotten restores.

origin

Origin is the scale pivot

Without translate, shapes grow away from the top-left. Move the origin first to scale in place.

stroke

lineWidth scales

A 2px stroke becomes ~4px after scale(2, 2). Isolate with save/restore, or divide lineWidth by the factor.

zero

Avoid scale(0, …)

A zero factor collapses geometry. Negative factors flip; keep intentional and adjust coordinates.

leak

Transforms leak

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

Browser Support

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

✓ Baseline · Widely available

ctx.scale

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

Bottom line: Safe for production zooms, flips, and stretch effects on Canvas 2D everywhere modern browsers run.

Key Takeaways

  • Call: ctx.scale(x, y) to change unit size.
  • Pivot: translate(cx, cy) then scale.
  • Flip: negative x or y.
  • Isolate: save() / restore().

One line: move pivot → scale → draw → restore.

Default CSS Settings

Scaling 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 edges — different from ctx.scale().

Frequently Asked Questions

It multiplies the current transform so one canvas unit becomes sx pixels wide and sy pixels tall. Shapes, paths, text, and lineWidth drawn afterward are scaled.
translate(cx, cy), then scale(sx, sy), then draw with coordinates relative to that center (often fillRect(-w/2, -h/2, w, h)). Use save()/restore() to isolate.
Coordinates are scaled too. A point at (10, 10) with scale(2, 2) lands at (20, 20) from the origin. Translate first if you need a fixed pivot.
Use a negative factor: scale(-1, 1) flips left/right; scale(1, -1) flips up/down. Adjust drawing coordinates so content stays on-canvas.
Yes. Strokes thicken or thin with the transform. Isolate with save()/restore(), or compensate by dividing lineWidth by the scale factor.
drawImage(…, dw, dh) sizes one image draw. ctx.scale changes the whole transform for every later drawing call until you restore or reset.

Next: Canvas Shadow

Learn shadowColor, shadowBlur, and shadow offsets.

Canvas Shadow →

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