• <legend id='e4EWr'><style id='e4EWr'><dir id='e4EWr'><q id='e4EWr'></q></dir></style></legend>

    <tfoot id='e4EWr'></tfoot>

    <i id='e4EWr'><tr id='e4EWr'><dt id='e4EWr'><q id='e4EWr'><span id='e4EWr'><b id='e4EWr'><form id='e4EWr'><ins id='e4EWr'></ins><ul id='e4EWr'></ul><sub id='e4EWr'></sub></form><legend id='e4EWr'></legend><bdo id='e4EWr'><pre id='e4EWr'><center id='e4EWr'></center></pre></bdo></b><th id='e4EWr'></th></span></q></dt></tr></i><div id='e4EWr'><tfoot id='e4EWr'></tfoot><dl id='e4EWr'><fieldset id='e4EWr'></fieldset></dl></div>

    <small id='e4EWr'></small><noframes id='e4EWr'>

      • <bdo id='e4EWr'></bdo><ul id='e4EWr'></ul>

      1. 如何修复 HTML5 画布中的模糊文本?

        时间:2023-06-22
        <tfoot id='xCa4b'></tfoot>
      2. <small id='xCa4b'></small><noframes id='xCa4b'>

            <bdo id='xCa4b'></bdo><ul id='xCa4b'></ul>
            <i id='xCa4b'><tr id='xCa4b'><dt id='xCa4b'><q id='xCa4b'><span id='xCa4b'><b id='xCa4b'><form id='xCa4b'><ins id='xCa4b'></ins><ul id='xCa4b'></ul><sub id='xCa4b'></sub></form><legend id='xCa4b'></legend><bdo id='xCa4b'><pre id='xCa4b'><center id='xCa4b'></center></pre></bdo></b><th id='xCa4b'></th></span></q></dt></tr></i><div id='xCa4b'><tfoot id='xCa4b'></tfoot><dl id='xCa4b'><fieldset id='xCa4b'></fieldset></dl></div>
            <legend id='xCa4b'><style id='xCa4b'><dir id='xCa4b'><q id='xCa4b'></q></dir></style></legend>
              <tbody id='xCa4b'></tbody>

                  本文介绍了如何修复 HTML5 画布中的模糊文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 n00b 的一员,使用 HTML5 并且正在使用 canvas 来渲染形状、颜色和文本.在我的应用程序中,我有一个 view adapter 动态创建画布,并用内容填充它.这非常有效,除了我的文本被渲染得非常模糊/模糊/拉伸.我看过很多其他帖子,为什么在 CSS 中定义 widthheight 会导致这个问题,但我在 <代码>javascript.

                  相关代码(查看 来自 HTML5Rocks 更详细地解释了如何创建高清元素.

                  tl;博士?这是我在自己的项目中使用的一个示例(基于上面的 tut)以正确的分辨率吐出画布:

                  var PIXEL_RATIO = (function () {var ctx = document.createElement("canvas").getContext("2d"),dpr = window.devicePixelRatio ||1、bsr = ctx.webkitBackingStorePixelRatio ||ctx.mozBackingStorePixelRatio ||ctx.msBackingStorePixelRatio ||ctx.oBackingStorePixelRatio ||ctx.backingStorePixelRatio ||1个;返回 dpr/bsr;})();createHiDPICanvas = function(w, h, ratio) {if (!ratio) { ratio = PIXEL_RATIO;}var can = document.createElement("canvas");can.width = w * 比率;can.height = h * 比率;can.style.width = w + "px";can.style.height = h + "px";can.getContext("2d").setTransform(ratio, 0, 0, ratio, 0, 0);回可以;}//使用设备分辨率创建画布.var myCanvas = createHiDPICanvas(500, 250);//创建具有自定义分辨率的画布.var myCustomCanvas = createHiDPICanvas(500, 200, 4);

                  希望这会有所帮助!

                  I am a total n00b with HTML5 and am working with the canvas to render shapes, colors, and text. In my app, I have a view adapter that creates a canvas dynamically, and fills it with content. This works really nicely, except that my text is rendered very fuzzy/blurry/stretched. I have seen a lot of other posts on why defining the width and height in CSS will cause this issue, but I define it all in javascript.

                  The relevant code (view Fiddle):

                  var width  = 500;//FIXME:size.w;
                  var height = 500;//FIXME:size.h;
                      
                  var canvas = document.createElement("canvas");
                  //canvas.className="singleUserCanvas";
                  canvas.width=width;
                  canvas.height=height;
                  canvas.border = "3px solid #999999";
                  canvas.bgcolor = "#999999";
                  canvas.margin = "(0, 2%, 0, 2%)";
                      
                  var context = canvas.getContext("2d");
                  
                  //////////////////
                  ////  SHAPES  ////
                  //////////////////
                      
                  var left = 0;
                  
                  //draw zone 1 rect
                  context.fillStyle = "#8bacbe";
                  context.fillRect(0, (canvas.height*5/6)+1, canvas.width*1.5/8.5, canvas.height*1/6);
                  
                  left = left + canvas.width*1.5/8.5;
                  
                  //draw zone 2 rect
                  context.fillStyle = "#ffe381";
                  context.fillRect(left+1, (canvas.height*5/6)+1, canvas.width*2.75/8.5, canvas.height*1/6);
                  
                  left = left + canvas.width*2.75/8.5 + 1;
                  
                  //draw zone 3 rect
                  context.fillStyle = "#fbbd36";
                  context.fillRect(left+1, (canvas.height*5/6)+1, canvas.width*1.25/8.5, canvas.height*1/6);
                  
                  left = left + canvas.width*1.25/8.5;
                  
                  //draw target zone rect
                  context.fillStyle = "#004880";
                  context.fillRect(left+1, (canvas.height*5/6)+1, canvas.width*0.25/8.5, canvas.height*1/6);
                  
                  left = left + canvas.width*0.25/8.5;
                      
                  //draw zone 4 rect
                  context.fillStyle = "#f8961d";
                  context.fillRect(left+1, (canvas.height*5/6)+1, canvas.width*1.25/8.5, canvas.height*1/6);
                  
                  left = left + canvas.width*1.25/8.5 + 1;
                  
                  //draw zone 5 rect
                  context.fillStyle = "#8a1002";
                  context.fillRect(left+1, (canvas.height*5/6)+1, canvas.width-left, canvas.height*1/6);
                  
                  ////////////////
                  ////  TEXT  ////
                  ////////////////
                  
                  //user name
                  context.fillStyle = "black";
                  context.font = "bold 18px sans-serif";
                  context.textAlign = 'right';
                  context.fillText("User Name", canvas.width, canvas.height*.05);
                  
                  //AT:
                  context.font = "bold 12px sans-serif";
                  context.fillText("AT: 140", canvas.width, canvas.height*.1);
                  
                  //AB:
                  context.fillText("AB: 94", canvas.width, canvas.height*.15);
                         
                  //this part is done after the callback from the view adapter, but is relevant here to add the view back into the layout.
                  var parent = document.getElementById("layout-content");
                  parent.appendChild(canvas);

                  <div id="layout-content"></div>

                  The results I am seeing (in Safari) are much more skewed than shown in the Fiddle:

                  Mine

                  Fiddle

                  What am I doing incorrectly? Do I need a separate canvas for each text element? Is it the font? Am I required to first define the canvas in the HTML5 layout? Is there a typo? I am lost.

                  解决方案

                  The canvas element runs independent from the device or monitor's pixel ratio.

                  On the iPad 3+, this ratio is 2. This essentially means that your 1000px width canvas would now need to fill 2000px to match it's stated width on the iPad display. Fortunately for us, this is done automatically by the browser. On the other hand, this is also the reason why you see less definition on images and canvas elements that were made to directly fit their visible area. Because your canvas only knows how to fill 1000px but is asked to draw to 2000px, the browser must now intelligently fill in the blanks between pixels to display the element at its proper size.

                  I would highly recommend you read this article from HTML5Rocks which explains in more detail how to create high definition elements.

                  tl;dr? Here is an example (based on the above tut) that I use in my own projects to spit out a canvas with the proper resolution:

                  var PIXEL_RATIO = (function () {
                      var ctx = document.createElement("canvas").getContext("2d"),
                          dpr = window.devicePixelRatio || 1,
                          bsr = ctx.webkitBackingStorePixelRatio ||
                                ctx.mozBackingStorePixelRatio ||
                                ctx.msBackingStorePixelRatio ||
                                ctx.oBackingStorePixelRatio ||
                                ctx.backingStorePixelRatio || 1;
                  
                      return dpr / bsr;
                  })();
                  
                  
                  createHiDPICanvas = function(w, h, ratio) {
                      if (!ratio) { ratio = PIXEL_RATIO; }
                      var can = document.createElement("canvas");
                      can.width = w * ratio;
                      can.height = h * ratio;
                      can.style.width = w + "px";
                      can.style.height = h + "px";
                      can.getContext("2d").setTransform(ratio, 0, 0, ratio, 0, 0);
                      return can;
                  }
                  
                  //Create canvas with the device resolution.
                  var myCanvas = createHiDPICanvas(500, 250);
                  
                  //Create canvas with a custom resolution.
                  var myCustomCanvas = createHiDPICanvas(500, 200, 4);
                  

                  Hope this helps!

                  这篇关于如何修复 HTML5 画布中的模糊文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:画布底部有空白区域并且滚动太远 下一篇:HTML5 Canvas toDataURL 返回空白

                  相关文章

                1. <tfoot id='KPcWn'></tfoot>

                    <small id='KPcWn'></small><noframes id='KPcWn'>

                        <bdo id='KPcWn'></bdo><ul id='KPcWn'></ul>

                      <legend id='KPcWn'><style id='KPcWn'><dir id='KPcWn'><q id='KPcWn'></q></dir></style></legend>
                    1. <i id='KPcWn'><tr id='KPcWn'><dt id='KPcWn'><q id='KPcWn'><span id='KPcWn'><b id='KPcWn'><form id='KPcWn'><ins id='KPcWn'></ins><ul id='KPcWn'></ul><sub id='KPcWn'></sub></form><legend id='KPcWn'></legend><bdo id='KPcWn'><pre id='KPcWn'><center id='KPcWn'></center></pre></bdo></b><th id='KPcWn'></th></span></q></dt></tr></i><div id='KPcWn'><tfoot id='KPcWn'></tfoot><dl id='KPcWn'><fieldset id='KPcWn'></fieldset></dl></div>