ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

The Canvas Blur Issue

2021-05-21 11:35:09  阅读:249  来源: 互联网

标签:Canvas ctx canvas let webkitBackingStorePixelRatio Blur Issue pixels size


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:

    1. Drawing:
      According to webkitBackingStorePixelRatio, 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.

    2. Rendering:
      According to devicePixelRatio, 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.

  1. 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.

  2. 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:

  1. canvas.width/height represents the actual size of the canvas image. It defines the pixels of canvas.
  2. 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.

 

标签:Canvas,ctx,canvas,let,webkitBackingStorePixelRatio,Blur,Issue,pixels,size
来源: https://www.cnblogs.com/simer-web/p/14793054.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有