All guides

Canvas · Pixel density · Coordinates

Why canvas looks blurry and how to handle pixel density

Author: Grundy · Checked: November 16, 2026

A player can look soft even when its drawing logic is correct. We will trace how the site sizes its canvas, keeps drawing coordinates stable and redraws after layout changes. Dotted-underlined terms have definitions on hover, click or keyboard focus. The standalone example includes all its setup; site excerpts explain their surrounding values.

1. A canvas has two sizes

A is a page-layout unit, while the stores the picture. CSS might display a canvas at 320 × 180 while canvas.width and canvas.height remain 300 and 150, the defaults when no attributes are supplied. The browser then scales an existing picture. For players with text and fine lines, matching the bitmap to its displayed size matters.

2. Account for density on both axes

At 2, a 320 × 180 CSS-pixel rectangle uses a 640 × 360 bitmap. Drawing can still use 320 × 180 coordinates when bitmap size and drawing scale are configured together. Read window.devicePixelRatio rather than inferring it from the device name. The site’s players round bitmap dimensions with Math.round, then use canvas.width / width and canvas.height / height as the exact horizontal and vertical scales, with width and height measured in CSS pixels.

3. A standalone setup example

The code creates an element and obtains its . It can run in a browser page after TypeScript is compiled to JavaScript. It sets the displayed size explicitly, changes the bitmap only when needed and then installs the drawing scale. The font is assigned afterwards: assigning canvas.width or canvas.height clears the picture and resets context state, even when assigning the current value. Redraw the picture after a size change.

function resizeCanvas(
    canvas: HTMLCanvasElement,
    width: number,
    height: number,
    ratio: number
) {
    if (!(width > 0 && height > 0) || !Number.isFinite(width + height))
        throw new RangeError("Expected positive finite CSS dimensions");
    const ctx = canvas.getContext("2d");
    if (!ctx) return null;
    const scale = Number.isFinite(ratio) && ratio > 0 ? ratio : 1;
    canvas.style.width = width + "px";
    canvas.style.height = height + "px";
    const bitmapWidth = Math.max(1, Math.round(width * scale));
    const bitmapHeight = Math.max(1, Math.round(height * scale));
    if (canvas.width !== bitmapWidth) canvas.width = bitmapWidth;
    if (canvas.height !== bitmapHeight) canvas.height = bitmapHeight;
    ctx.setTransform(bitmapWidth / width, 0, 0, bitmapHeight / height, 0, 0);
    return ctx;
}

const canvas = document.createElement("canvas");
canvas.style.background = "#fff";
document.body.append(canvas);
const ctx = resizeCanvas(canvas, 320, 180, window.devicePixelRatio);
if (ctx) {
    ctx.font = "18px sans-serif";
    ctx.fillStyle = "#20222d";
    ctx.strokeStyle = "#20222d";
    ctx.fillText("Canvas 123", 16, 32);
    ctx.strokeRect(16, 50, 120, 80);
}

4. How the site’s players do it

createCanvasSurface takes an HTML canvas and returns context and resize. In the players, page layout controls the displayed width and the adapter receives measured dimensions. In this excerpt, width and height are CSS dimensions, ratio is the chosen density and canvas is the actual element. identity() calls native.setTransform(scaleX, 0, 0, scaleY, 0, 0) on the original context. A wrapper also exposes logical dimensions through context.canvas.width and context.canvas.height, keeping layout from growing with the bitmap.

const bitmapWidth = Math.max(1, Math.round(width * ratio));
const bitmapHeight = Math.max(1, Math.round(height * ratio));
if (canvas.width !== bitmapWidth) canvas.width = bitmapWidth;
if (canvas.height !== bitmapHeight) canvas.height = bitmapHeight;
scaleX = bitmapWidth / width;
scaleY = bitmapHeight / height;
identity();

5. Compare buffers and enlarged crops

The left scene uses factor 1; the right uses the selected factor. Both use the site’s actual createCanvasSurface adapter. Auto reads the browser’s density; other choices are teaching overrides and do not change the display. CSS dimensions stay equal. Enlarged crops reveal source samples even on a density-1 screen; the normal-size views may look less different on your display. This compares bitmap preparation, not monitor sharpness.

Measuring canvas sizes…

Buffer at 1×

Enlarged text crop

Buffer at selected density

Enlarged text crop

Changing the selection does not change the screen’s DPR. Crop filtering is off; both enlarged views use the browser’s actual density. Resize the window to compare bitmap dimensions while keeping drawing coordinates stable.

6. Keep the scale while drawing

On a native context, setTransform replaces the current , while repeated scale(2, 2) calls accumulate: two calls yield a fourfold scale. The standalone function installs the coordinate mapping afresh. The site’s adapter handles another case: drawing code may call setTransform(1, 0, 0, 1, 0, 0) or resetTransform before a frame. The wrapper keeps the density scale and exposes logical coordinates through getTransform. This is adapter behavior; a native context’s resetTransform removes scaling, rotation and translation.

7. Redraw while paused too

The players’ useCanvasViewport watches element sizes with . When the container changes, it measures, calls surface.resize, then redraws the selected frame. Here surface is the adapter, width and height are new CSS dimensions, and redraw.current stores the current frame’s drawing function. Pausing model playback does not remove the need to redraw after resizing. Expanded mode also accounts for available space and controls.

surface.resize(width, height, window.devicePixelRatio);
redraw.current();

8. Observe density changes

Moving a window between displays or changing page zoom can change devicePixelRatio. The players subscribe to the current density with matchMedia and recreate that query after a change. In this excerpt, resolution stores the query and update() measures, resizes and redraws. The listener is removed when the player is removed. Pinch zoom behaves differently and does not change DPR. The example’s override, page zoom and window size are separate controls.

let resolution: MediaQueryList;
const watchResolution = () => {
    resolution?.removeEventListener("change", changedResolution);
    resolution = matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);
    resolution.addEventListener("change", changedResolution);
};
function changedResolution() {
    update();
    watchResolution();
}
watchResolution();

9. What else affects appearance

Correct bitmap size still permits on letters and diagonals. Adding 0.5 to a coordinate is not a universal remedy for fractional scales or varying line widths. imageSmoothingEnabled controls images scaled with drawImage, not text rendering or CSS sizing. The enlarged crops disable to expose the samples. Doubling the factor quadruples the bitmap’s pixel count; assess drawing costs rather than choosing a large factor from one small crop.

10. Separate display and video dimensions

recordPlayerVideo creates a separate canvas with video.width and video.height. The video object supplies recording dimensions and a drawing function; the excerpt shows bitmap setup only. A 1280 × 720 video uses those dimensions regardless of the display density or player width. The display adapter is not added to that recording canvas, so a higher screen DPR alone does not improve the exported file.

const canvas = document.createElement("canvas");
canvas.width = video.width;
canvas.height = video.height;
const context = canvas.getContext("2d");

11. Check dimensions, coordinates and appearance

Compare the element’s CSS dimensions with its width and height, then try densities 1, 1.25, 1.5, 2 and 3. At a fixed CSS size, labels and bars should keep their positions. Resize while paused and check expanded mode, density changes and themes. The site’s adapter checks compare sizes and drawing commands, including intermediate sorting frames. They check geometry; font appearance and edge blending still need browser inspection. This article does not claim measured FPS or memory improvements.