Search code examples
javascripthtmlcanvashtml5-canvas

Higher DPI graphics with HTML5 canvas


Is there a way to set a custom DPI/PPI when creating an image using the HTML5 canvas? I know how can I draw on the canvas and export it as an image, but how can I make sure the output image is of certain DPI/PPI. I guess using SVG elemnts to draw on the canvas is a way, but wouldn't that be flattened out when I export the whole canvas as an image? Or calculating the device DPI and then scaling the image to meet my DPI requirement, but that doesn't seem like the correct solution.


Solution

  • You cannot (ugh) access the DPI of a display of the current web page in any browser:

    Detecting the system DPI/PPI from JS/CSS?

    For printing: You most likely cannot set the DPI of exported <canvas> image (PNG, JPEG) using browser standard functions. However, if you use a pure Javascript encoder image encoder you are free to create any sort of binary file you wish and manually adjust the DPI value embedded int he binary.

    https://gist.github.com/1245476