Problem
If you view a canvas page in high-DPI screens(retina), maybe you will find that the canvas pictures become blurry. what results in that? It's a challenge for web developers.
Analysis
The browser shows Canvas in two steps:
-
Drawing:
According towebkitBackingStorePixelRatio
, the browser draw canvas in buffer.
If the canvas is 200px wide and high and the webkitBackingStorePixelRatio is 2, then the Canvas will be drawed with 400px. -
Rendering:
According todevicePixelRatio
, the browser scales and render the canvas to screen.
Here two concepts are involved: webkitBackingStorePixelRatio
and devicePixelRatio
.
webkitBackingStorePixelRatio:
The “webkitBackingStorePixelRatio” is a property defined on canvas contexts, which is used to draw canvas to buffer. It determines the pixel number of the canvas image in drawing.
devicePixelRatio:
The “devicePixelRatio” of Window interface returns the ratio of the resolution in device pixels to the resolution in CSS pixels for the current display device. In simpler terms, this tells the browser how many of the screen's device pixels should be used to draw a single CSS pixel.
Let's take Macbook Pro as an example:
webkitBackingStorePixelRatio = 1
devicePixelRatio = 2
Now define a canvas with the size of 200px*200px.
-
webkitBackingStorePixelRatio = 1
, so the canvas is drawed in buffer as 200px*200px. Here you need know the canvas is not a vector-based picture, but a bitmap. When a bitmap image is scaled up you begin to see the individual pixels that make up the image and this often results in making the image blurry. -
devicePixelRatio = 2
, so 200px * 200px css pixels means 400px * 400px device pixels, so the canvas, a bitmap image from drawing buffer, will be upscaled to two times of the old size, and then it will become blurry.
Solution
How can we fix the problem?
We can increase the pixels of the canvas in drawing process to match the pixel density for rendering.
HTML
<canvas id="canvas"></canvas>
JavaScript
1 let getPixelRatio = function (context) { 2 let backingStore = context.backingStorePixelRatio || 3 context.webkitBackingStorePixelRatio || 4 context.mozBackingStorePixelRatio || 5 context.msBackingStorePixelRatio || 6 context.oBackingStorePixelRatio || 7 context.backingStorePixelRatio || 1; 8 return (window.devicePixelRatio || 1) / backingStore; 9 }; 10 11 let draw = function() { 12 let canvas = document.getElementById('canvas'); 13 let ctx = canvas.getContext('2d'); 14 15 // Set display size (css pixels). 16 let size = 300; 17 canvas.style.width = size + "px"; 18 canvas.style.height = size + "px"; 19 20 // Set actual size in memory (scaled to account for extra pixel density). 21 let scale = getPixelRatio(ctx); // Change to 1 on retina screens to see blurry canvas. 22 canvas.width = Math.floor(size * scale); 23 canvas.height = Math.floor(size * scale); 24 25 // Normalize coordinate system to use css pixels. 26 ctx.scale(scale, scale); 27 28 ctx.fillStyle = "#bada55"; 29 ctx.fillRect(10, 10, 300, 300); 30 ctx.fillStyle = "#ffffff"; 31 ctx.font = '20px Arial'; 32 ctx.textAlign = 'center'; 33 ctx.textBaseline = 'middle'; 34 35 let x = size / 2; 36 let y = size / 2; 37 38 let textString = "I love Canvas"; 39 ctx.fillText(textString, x, y); 40 } 41 42 draw();
NOTE:
-
canvas.width/height
represents the actual size of the canvas image. It defines the pixels of canvas. -
canvas.style.width/height
represents the rendering size of canvas. It defines the size of the area which the canvas will be scaled to fit.