Rendering PipelineBaseline · Widely

Device Pixel Ratio & Subpixel Rendering

Mental Model

A CSS px is an angular unit, not a hardware dot. window.devicePixelRatio (DPR) maps CSS pixels to physical screen pixels; fractional coordinates or unscaled <canvas> buffers force bilinear filtering, blurring edges and text.

Interactive Simulator

CSS Pixel to Physical Pixel Mapping

Adjust devicePixelRatio and fractional CSS coordinates to see physical pixel alignment.

Parameter Profile
dpr2
cssOffset0.5
snapToDeviceOff
dpr
2
cssOffset
0.5
snapToDevice
Off
Parameters evaluated in real time.

Under the Hood

CSS coordinates are a floating-point blueprint, whereas your monitor is a rigid grid of physical LEDs. When a line lands halfway between two LEDs, the rasterizer smears it across both.

  1. Subpixel Layout: Render Tree & Layout stores box edges in fixed-point fractions (e.g., 1/64px).
  2. Border Snapping: During Paint & Rasterization, engines snap borders and backgrounds to whole device pixels so 1px lines stay crisp.
  3. Canvas & Transforms: <canvas> backing stores and transform: translate() bypass layout snapping unless explicitly scaled by devicePixelRatio.
JS
const dpr = window.devicePixelRatio || 1;
const rect = canvas.getBoundingClientRect();
canvas.width = Math.round(rect.width * dpr);
canvas.height = Math.round(rect.height * dpr);
ctx.scale(dpr, dpr);

Problem Genealogy

  • 2010 (HiDPI / Retina): Apple doubled physical pixel density (dpr = 2) while keeping CSS px invariant, exposing unscaled bitmaps and <canvas> buffers.
  • 2016+: Fractional DPR (1.25, 1.5, 2.625) became common on Windows and Android, making naive Math.floor(dpr) rounding produce uneven borders.

Production Pitfalls

Production Pitfall
  • Blurry <canvas> on HiDPI: Setting only CSS width and height stretches a 1x bitmap across 2x or 3x physical pixels.
  • translate(-50%, -50%) on odd-sized boxes: Centering an odd-height modal with -50% places text on a half-pixel boundary, softening glyph edges.
When Not to Use
  • When not to use 0.5px borders: Avoid border: 0.5px solid for hairlines on low-DPR screens where it rounds to 0px or 1px; use 1px or transform: scaleY(0.5) gated by min-resolution: 2dppx.

Connections & Logical Next Steps