DEV Community

yue xing
yue xing

Posted on

Canvas width attribute vs CSS width: how much blur a stretch adds at DPR 1, 2 and 3

A canvas has two sizes, and only one of them holds pixels. The width and height attributes set the backing store, the bitmap that fillText actually draws into. The CSS width and height only decide how large that bitmap is shown on the page. Give a canvas the attributes 400×100 and the CSS 800×200, and the browser takes 40,000 drawn pixels and spreads them over a box with four times the area. On a 1× screen that is a 2× upscale per side. On a 3× screen it is 6× per side. Knowing the two sizes are separate is the easy part; how much blur the stretch costs, and how that changes from one screen to the next, is what this run put a number on.

The test page and the edge metric

The text sample is homemade: the line 周末市集 Canvas 11月22日 (a weekend market notice mixing Chinese, Latin letters and digits), bold 28px, black on white, in the system PingFang SC font, drawn into a canvas with a logical size of 400×100. One version of the page keeps the attributes at 400×100 and sets the CSS to 800×200. The other sets the attributes to CSS size × devicePixelRatio, so 800×200, 1600×400 and 2400×600, and scales the context to match. Both pages were rendered at DPR 1, 2 and 3 in desktop Chromium on one Apple-silicon Mac, and the canvas element was captured at device resolution. Each configuration ran once.

Blur needs a number, and the one used here is homemade too. Edge transition width is the average length, row by row, of runs of pixels whose gray value sits between 32 and 223, so a crisp edge gives a short run and a smeared edge gives a long one. Error is the mean absolute gray difference, on a 0 to 255 scale, against a reference drawn at 8× and averaged down. For scale, that reference measured 1.768 px of edge at 800×200.

Stretched edges got wider at every DPR step

DPR Attributes 400×100, CSS 800×200 Backing store = CSS size × DPR
1 captured 800×200, edge 5.99 px, error 4.758 800×200, edge 1.641 px, error 1.603
2 captured 1600×400, edge 11.262 px, error 5.251 1600×400, edge 1.873 px, error 1.078
3 captured 2400×600, edge 16.756 px, error 5.413 2400×600, edge 1.639 px, error 1.01

Edges are counted in device pixels. The right column barely moves: 1.6 to 1.9 at every ratio, sitting around the 1.768 reference. The left column climbs with every step, and the stretched edge works out to 3.65 times the matched one at DPR 1, 6.01 times at DPR 2 and 10.22 times at DPR 3. The DPR 1 row deserves a second look, because nothing about the screen is high-density there. All the blur in that row comes from the attribute being half the CSS size. So there are two multipliers stacked on top of each other, CSS size over attribute size and then the device pixel ratio, and the backing store has to cover both.

The rasterizer was not the problem

The other suspect is fillText antialiasing itself getting worse on high-DPI screens. This run doesn't support that. On the export side of the same test, a canvas with a fixed pixel size gave pixel-identical files at DPR 1, 2 and 3, so the drawn text did not depend on the screen here. What a denser screen changes is how far the browser has to stretch a bitmap that was already too small, which is why the left column looks like a rendering problem when it is a sizing one.

The display fix is the familiar one. Below is a sketch of the idea, not the measured page itself:

const card = document.querySelector('#notice');     // the visible canvas
const box = card.getBoundingClientRect();           // CSS size, 800×200 here
const k = window.devicePixelRatio || 1;
card.width = Math.round(box.width * k);              // bitmap = CSS size × DPR
card.height = Math.round(box.height * k);
card.getContext('2d').scale(k, k);                   // keep drawing in CSS units
Enter fullscreen mode Exit fullscreen mode

That handles what the page shows. A downloaded file is a different question, since the same multiplication at DPR 1 hands back a 1× bitmap, and an export probably wants its size from the target instead of from whoever's screen happens to run it. One browser editor that writes this split down is ImgIng https://imging.ai/ , whose site says the preview renders at an adaptive resolution and the export is recomposed at the original canvas size. How it does that internally, I don't know.

What this run did not cover

One thing I still can't explain is the matched DPR 2 row. Its edge is 1.873 px while DPR 1 and DPR 3 both land near 1.64, and its error, 1.078, is between the other two, so it isn't simply worse. The cause wasn't looked into. Beyond that, only Chromium was measured. Safari, Firefox and Windows ClearType were not tested, and neither were imageSmoothingQuality, OffscreenCanvas or the scale option in html2canvas, so anything said about them would be a guess. It is also one string, one machine, one run per setting, with a metric defined just for this test.

If canvas text on a page looks soft, the quickest check is in the console: compare canvas.width with getBoundingClientRect().width * devicePixelRatio. When the two numbers differ, the bitmap is being stretched, and in this run that stretch alone made edges between 3.65 and 10.22 times wider than the matched version.

Top comments (0)