I have an issue with SVG rendered into canvas. On retina displays canvas rendered as base64 url and set as SRC into is blurred.
I've tried various methods that was described in list below with no luck:
- https://tristandunn.com/2014/01/24/rendering-svg-on-canvas/
- How do I fix blurry text in my HTML5 canvas?
- Canvas drawing and Retina display: doable?
- https://www.html5rocks.com/en/tutorials/canvas/hidpi/
Now i don't know what should i do to make it better. Please look into my result: jsfiddle.net/a8bj5fgj/2/
Edit:
Updated fiddle with fix: jsfiddle.net/a8bj5fgj/7/
Retina display
Retina and very high resolution displays have pixel sizes that are smaller than the average human eye can resolve. Rendering a single line then ends up looking a like a lighter line. To fix the issues involved pages that detect the high res display will change the default CSS pixel size to 2.
The DOM knows this and adjusts its rendering to compensate. But the Canvas is not aware and its rendering is just scaled up. The default display rendering type for the canvas is bilinear interpolation. This smooths the transition from one pixel to the next which is great for photos, but not so great for lines, text, SVG and the like.
Some solutions
First is to turn of bilinear filtering on the canvas. This can be done with the CSS rule
image-rendering: pixelated;
Though this will not create the quality of the SVG rendered on the DOM it will reduce the appearance of blurriness some users experience.When rendering SVG to the canvas you should turn off image smoothing as that can reduce the quality of the svg image. SVG is rendered internally and does not need additional smoothing when the internal copy is rendered onto the canvas.
To do this
ctx.imageSmoothingEnabled = false;
Detect the CSS pixel size. The window variable
devicePixelRatio
returns the size of the CSS pixel compared to the actual screen physical pixel size. Retina and High res devices will typically have a value of 2. You can then use that to set the canvas resolution to match the physical pixel resolution.But there is a problem because
devicePixelRatio
is not supported on all browsers anddevicePixelRatio
is affected by the page zoom setting.So at the most basic using
devicePixelRatio
and the assumption that few people zoom past 200%.Code Assuming that the
canvas.style.width
andcanvas.style.height
are already correctly set.Now that you have increased the resolution you must also increase the rendering size. This can be done via the canvas transform, and better yet create it as a function.
A better Guess
The two methods above are either a stop gap solution or a simple guess. You can improve your odds by examining some of the system.
Retina displays currently come in a fixed set of resolutions for a fixed set of devices (phones, pads, notebooks).
You can query the
window.screen.width
andwindow.screen.height
to determine the absolute physical pixel resolution and match that against known retina displays resolutions. You can also query the userAgent to determine the device type and brand.Putting it all together you can improve the guess. The next function makes a guess if the display is retina. You can use something similar to determin if the device is retina and then increase the canvas resolution accordingly.
Information for the following code found at wiki Retina Display Models This information can be machine queried using Wiki's SPARQL interface if you want to keep it up to date.
Demo Guess if Retina.
From your snippet
This may do what you want. As I don't own any apple products I can not test it apart from forcing true on isRetina.
Please Note.
Most people who have 6/6 vision (20/20 for imperial countries) will be hard pressed to see the difference between the slightly blurry display of the canvas and the crisp DOM. You should ask yourself, did you need to have a closer look to make sure? can you see the blur at normal viewing distance?
Also some people who have zoomed the display to 200% do so for good reason (vision impaired) and will not appreciate you circumventing their settings.