A DEV Community article by Mustafa Morbel explains why a canvas element, the HTML tag for drawing bitmap graphics with JavaScript, can look soft on Retina and other high-density screens. The cause is a mismatch between CSS pixels, the layout unit, and device pixels, the physical dots on the panel. A canvas allocated at 300 by 150 pixels is stretched across 600 device pixels on a 2x screen, and the browser blends neighbouring pixels to fill the gap.
The fix has a few parts. Set the canvas's internal size to the CSS size multiplied by window.devicePixelRatio, which is the number of device pixels per CSS pixel. Keep the displayed size fixed in CSS pixels. Then call ctx.setTransform(dpr, 0, 0, dpr, 0, 0) once, so drawing code can keep using CSS coordinates. The article notes that devicePixelRatio is not always a whole number, because page zoom or fractional OS scaling can produce values such as 1.25 or 1.5.
The author also covers two other causes of softness. A 1px line drawn on a whole-number coordinate straddles two pixels and looks pale, so offsetting it by 0.5 keeps it crisp. Scaled pixel art blurs unless imageSmoothingEnabled is set to false. Assigning to canvas.width resets the drawing context, including that setting, so it must be reapplied after any resize. The ratio can also change while a page is open, for example when a window moves between monitors. The article suggests watching for this with window.matchMedia and a resolution query.
Sharpness has a cost. Pixel count rises with the square of the ratio, so 2x means four times the pixels and 3x means nine times. In the author's headless benchmark, which fills a memory buffer rather than using a GPU, the measured cost was 0.90x, 3.78x and 8.40x at ratios of 1, 2 and 3. For demanding 60 FPS scenes, the article recommends capping the ratio with Math.min(dpr, 2).
Why it matters
If your site uses canvas for charts, editors, product configurators or games, it may look softer on Retina laptops and many phones than on a standard monitor. Thin lines and small text show the problem first. Large shapes and solid fills hide it, so the issue can go unnoticed for a long time.
The same reasoning applies to any heavy canvas work. Sharper output costs more processing, which can hurt smoothness and battery life on busy pages. The article's advice is to use the full ratio for mostly static content and cap it for animation-heavy scenes.
Source: DEV Community. This brief summarises their reporting; read the original for the full detail.