devicePixelRatio is the wrong input for a canvas high-res export
A poster generator makes one promise at the export button: the file comes out at the size the template asked for. The HiDPI canvas recipe, where you multiply the canvas width and height by window.devicePixelRatio and the
A poster generator makes one promise at the export button: the file comes out at the size the template asked for. The HiDPI canvas recipe, where you multiply the canvas width and height by window.devicePixelRatio and then call ctx.scale(dpr, dpr), is built for crisp display. If the export function borrows it, the size of the downloaded file gets decided by whoever happens to be looking at the screen, and the template no longer has a say.
This run kept the setup small on purpose. The layout is a 400ร100 logical canvas with one bold 28px line of mixed Chinese and Latin text, black on white, and the text sample is self-made. The deliverable is an 800ร200 image, i.e. a 2ร banner. Two export functions ran in desktop Chromium on one Apple-silicon Mac, at devicePixelRatio 1, 2 and 3, one run each. One function multiplies by devicePixelRatio. The other ignores the screen and always draws at a scale of 2.
A DPR 1 screen gets the 1x file back
The DPR-scaled function returned 400ร100 at DPR 1, 800ร200 at DPR 2 and 1200ร300 at DPR 3. That is three screens and three different files, and only one of them is the size the template wanted. The DPR 1 case is the quiet one: devicePixelRatio is 1, nothing gets multiplied, so the "high-res" branch is the plain 1ร branch. If the app then stretches that file to 800ร200 to fit the template, it lands exactly where upscaling the preview canvas lands, and in this run the two gave identical numbers on both measures. The DPR 3 file has the opposite problem. 1200ร300 is 2.25 times the pixel count of 800ร200, so the file is likely bigger than the template needs, and shrinking it back to the template size left the letter edges a little softer than drawing at 800ร200 directly. File bytes were not compared here, only pixel counts.
For a one-person product, the worrying part is who sees which branch. If the developer works on a 2ร laptop, the only path they ever click is the one that produces the right size. My assumption is that a customer on a plain office monitor would be the first to receive the 400ร100 version, and nothing on the developer's own screen would hint at it.
Drawing at the target size gave one file three times
The second function sizes its canvas from the job, not from the screen. Here is a simplified sketch of the idea; it is not the script that produced the numbers in this post:
// sketch only: the scale is part of the order, the screen is never asked
function renderPoster(order, paint) {
const sheet = document.createElement('canvas');
sheet.width = order.w * order.scale;
sheet.height = order.h * order.scale;
const pen = sheet.getContext('2d');
pen.scale(order.scale, order.scale);
paint(pen, order.w, order.h);
return sheet;
}
renderPoster({ w: 400, h: 100, scale: 2 }, drawBanner);
At DPR 1, 2 and 3 the measured version gave 800ร200 every time, and the three files were pixel-identical. That also explains why the screen stopped mattering: in this run, what fillText drew depended on the canvas pixels alone, so three different screens painting the same 800ร200 canvas got the same pixels. The text came out with greyscale antialiasing, and the largest red-minus-blue difference across all exports was 0, so no colour fringes. To put a number on sharpness, each export was compared with an 8ร supersampled reference using a homemade yardstick: mean absolute grey difference on a 0 to 255 scale. The stretched 1ร file scored 4.759 and the redrawn one 1.603, so the stretched file carried about 2.97 times the error of the redraw.
The on-screen canvas is a different story. It still needs devicePixelRatio to look sharp in the editor, so the display canvas keeps the DPR recipe and the export function stays separate. ImgIng https://imging.ai/ keeps the two apart in its online design editor too: its site says editing is shown at an adaptive resolution and the export is rebuilt at the original canvas size. This run did not look inside how it does that.
The poster generator will read its export size from the template
The plan for the generator is plain. Each template stores its output size, say 800ร200 for a banner, next to its 400ร100 layout size. The export path computes the scale as template pixels divided by layout size and never reads devicePixelRatio. After export, a one-line check compares the file's width and height with the template and refuses to save on a mismatch. For testing, DevTools device mode lets you set the device pixel ratio, so DPR 1 can be checked from a 2ร laptop instead of hoping a customer reports it.
What this run did not cover
Only desktop Chromium was measured. Safari, Firefox and Windows ClearType were not tested, and neither were imageSmoothingQuality, OffscreenCanvas or the scale option in html2canvas. Whether passing a fixed scale to html2canvas behaves like the export-at-target function is an open question here, not a result. It is also one text sample, one machine and one run per setting, so treat the numbers as this sample's numbers. The one check worth adding before anything else is cheap: export once at DPR 1 and read the file's dimensions against the template.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.