Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 4 min read

Canvas text blurry on HiDPI? fillText gave identical pixels at DPR 1, 2 and 3

One explanation for soft canvas text goes like this: on a Retina-class screen, fillText antialiases worse, so the letters come out fuzzy and you need some smoothing setting to fix it. In one controlled run it did not hol

One explanation for soft canvas text goes like this: on a Retina-class screen, fillText antialiases worse, so the letters come out fuzzy and you need some smoothing setting to fix it. In one controlled run it did not hold. With the canvas bitmap size fixed, the same fillText call produced pixel-identical output at devicePixelRatio 1, 2 and 3, and the text was greyscale antialiased with no colour fringes at all. What did get worse on the high-DPI screens was a canvas with too few pixels being stretched by CSS, which is a sizing problem and not a rasteriser problem.

Scope first, because all of this rests on a small setup. One Apple-silicon Mac, desktop Chromium, DPR set to 1, 2 and 3, each case run once. The text sample is self-made: γ€Œε‘¨ζœ«εΈ‚ι›† Canvas 11月22ζ—₯」 in bold 28px, black on white, on a canvas whose logical size is 400Γ—100, with an 800Γ—200 image as the 2x target. Safari, Firefox and Windows ClearType were not tested; every number below means this machine, this browser, this one sample.

The two numbers behind every claim

Neither metric is a standard one; both were defined for this comparison. Edge transition width takes each pixel row, finds the runs of pixels whose grey value sits between 32 and 223 (neither clearly paper nor clearly ink), and averages their length. A crisp edge has short runs, a smeared edge has long ones. Error is the mean absolute grey difference, on a 0–255 scale, against a reference drawn at 8x (3200Γ—800) and area-averaged down to 800Γ—200. The reference itself has an edge width of 1.768 px, so "sharper than the reference" is not automatically better.

Same bitmap size gave the same pixels on every screen

Two of the export paths keep the canvas bitmap size independent of the screen. One draws on a 400Γ—100 canvas, the same as its CSS size, and exports that. The other ignores the screen and draws straight at 800Γ—200. For both, the files from DPR 2 and DPR 3 matched the DPR 1 file pixel for pixel. Not "close", identical. The 800Γ—200 draw measured an edge width of 1.641 px and an error of 1.603 on all three screens.

That kills the HiDPI-antialiasing theory for this setup. If the rasteriser treated high-DPI screens differently, the pixels would differ between DPR 1 and DPR 3. They did not. The text output depended on how many pixels the canvas had, and nothing else that changed between runs.

The colour check was simpler. Subpixel antialiasing, the ClearType-style kind, shows up as red and blue channels drifting apart at the stroke edges. Across every export in this run the maximum |Rβˆ’B| was 0, so each pixel was a pure grey. Whether other systems switch canvas text to subpixel rendering is outside what this run covers.

The stretched canvas is what got worse

Now the screen does matter. A canvas with a 400Γ—100 bitmap, displayed at 800Γ—200 by CSS, was captured on screen and measured in device pixels. Its edge went 5.99 β†’ 11.262 β†’ 16.756 across DPR 1, 2 and 3, so the DPR 2 reading is 1.88 times the DPR 1 one and the DPR 3 reading 2.80 times, with error rising from 4.758 to 5.251 and 5.413. The same coarse bitmap is simply spread over more physical pixels. Give the canvas CSS size Γ— DPR pixels instead and those three edge readings drop to 1.641, 1.873 and 1.639 (error 1.603, 1.078, 1.01).

My guess at where the theory comes from (inference, not something this run proves): the blur only becomes obvious on the sharp screen, so the sharp screen gets the blame, and the text renderer is the nearest suspect.

The quick check that follows from this is one comparison, canvas.width against canvas.clientWidth * window.devicePixelRatio. If the first is smaller, fix that gap before touching anything font-related.

One more thing showed up on the export side, and it cuts against the devicePixelRatio fix itself. Sizing the canvas by devicePixelRatio and exporting it works on a DPR 2 screen (800Γ—200, edge 1.641, error 1.603), but on a DPR 1 screen the same code exports a 400Γ—100 file. Upscaled to the 800Γ—200 target that file measured 5.98 px and 4.759, identical to not handling DPR at all. DPR 3 went the other way: a 1200Γ—300 file that, once shrunk to 800Γ—200, landed at error 1.46 and edge 2.517 px, softened by the bilinear downscale. So for a file meant to be a fixed size, the export size works better as a number you pick, not one you read off the user's screen.

One image editor, ImgIng https://imging.ai/ , draws that same line; its site says the preview runs at adaptive resolution to stay responsive, while the export is put together again at the original canvas size. Its internals were not part of this run.

Claim check

Claim Status in this run
fillText antialiases worse on HiDPI Did not hold (Chromium, Apple-silicon Mac)
Same bitmap size gives same pixels at any DPR Measured: pixel-identical at DPR 1, 2, 3
Canvas text has colour fringes Not here: max |Rβˆ’B| = 0
Stretching an under-sized canvas blurs more at higher DPR Measured: 5.99 / 11.262 / 16.756 px
Safari, Firefox, Windows ClearType Not tested
imageSmoothingQuality, OffscreenCanvas, html2canvas scale Not tested

The last two rows stay open, and Windows with ClearType is where subpixel text could plausibly appear (inference, not tested). Until that is measured, the reading stays narrow: on this Chromium setup, blurry canvas text was a pixel-count problem, and the check that located it was canvas.width against the displayed size Γ— DPR.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.