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
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.
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).
Compatibility
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 ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetAll modern
Full support
Internet Explorer9+
Supported
scale()Widely supported
Bottom line: Safe for production zooms, flips, and stretch effects on Canvas 2D everywhere modern browsers run.
Remember
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.
UA style
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.