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

<legend id='p0B5u'><style id='p0B5u'><dir id='p0B5u'><q id='p0B5u'></q></dir></style></legend>

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

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

      在带有背景的画布上书写文本

      Write text on canvas with background(在带有背景的画布上书写文本)

          1. <small id='m2l9n'></small><noframes id='m2l9n'>

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

            • <bdo id='m2l9n'></bdo><ul id='m2l9n'></ul>
              <legend id='m2l9n'><style id='m2l9n'><dir id='m2l9n'><q id='m2l9n'></q></dir></style></legend>
                  <tbody id='m2l9n'></tbody>
                <tfoot id='m2l9n'></tfoot>
              • 本文介绍了在带有背景的画布上书写文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                是否可以在画布上写图像并用背景写文本?比如这样:

                Is it possible to write image on canvas and write text with background? For example like this:

                推荐答案

                文本在画布中的工作原理

                很遗憾,您不能使用 text 方法生成带背景的文本 - 只能填充或勾勒文本本身.

                How text works in canvas

                Unfortunately no, you can't produce text with background with the text methods - only fill or outline the text itself.

                这是因为字体(字体)中的字形会根据需要转换为单独的形状或路径,其背景将是字形本身的内部部分(使用填充时看到的部分).除了使用其几何位置之外,字形使用的黑盒(字形适合的矩形)没有层,因此我们需要自己提供一种黑盒和轴承.

                This is because the glyphs from the typeface (font) are converted to individual shapes or paths if you want, where the background of it would be the inner part of the glyph itself (the part you see when using fill). There is no layer for the black-box (the rectangle which the glyph fits within) the glyph is using besides from using its geometric position, so we need to provide a sort-of black-box and bearings ourselves.

                在旧的计算机系统上,大多数字体在二进制字体中设置或清除像素.除了清除背景之外,还可以选择提供背景.默认情况下,基于矢量的字体并非如此(浏览器可以直接访问字形几何,因此可以通过这种方式提供背景).

                On the old computer systems most fonts where binary font which where setting or clearing a pixels. Instead of just clearing the background one could opt to provide a background instead. This is not the case with vector based typefaces by default (a browser has direct access to the glyphs geometry and can therefor provide a background this way).

                为了创建背景,您需要先使用其他方式(例如形状或图像)绘制它.

                In order to create a background you would need to draw it first using other means such as shapes or an image.

                例子:

                ctx.fillRect(x, y, width, height);
                

                ctx.drawImage(image, x, y  [, width, height]);
                

                然后在上面绘制文字:

                ctx.fillText('My text', x, y);
                

                您可以使用 measureText 来找出文本的宽度(将来还有高度:上升 + 下降)并将其用作基础:

                You can use measureText to find out the width of the text (in the future also the height: ascend + descend) and use that as a basis:

                var width = ctx.measureText('My text').width; /// width in pixels
                

                您可以将所有这些包装在一个函数中.这里的功能是基本的,但您可以使用颜色和背景参数以及填充等对其进行扩展.

                You can wrap all this in a function. The function here is basic but you can expand it with color and background parameters as well as padding etc.

                /// expand with color, background etc.
                function drawTextBG(ctx, txt, font, x, y) {
                
                    /// lets save current state as we make a lot of changes        
                    ctx.save();
                
                    /// set font
                    ctx.font = font;
                
                    /// draw text from top - makes life easier at the moment
                    ctx.textBaseline = 'top';
                
                    /// color for background
                    ctx.fillStyle = '#f50';
                    
                    /// get width of text
                    var width = ctx.measureText(txt).width;
                
                    /// draw background rect assuming height of font
                    ctx.fillRect(x, y, width, parseInt(font, 10));
                    
                    /// text color
                    ctx.fillStyle = '#000';
                
                    /// draw text on top
                    ctx.fillText(txt, x, y);
                    
                    /// restore original state
                    ctx.restore();
                }
                

                在线演示

                请注意,这种测量"方式高度不准确.您可以使用临时 div/span 元素测量字体的高度,并在为其设置字体和文本时从中获取计算的样式.

                Just note that this way of "measuring" height is not accurate. You can measure height of a font by using a temporary div/span element and get the calculated style from that when font and text is set for it.

                这篇关于在带有背景的画布上书写文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How does object-fit work with canvas element?(对象适合如何与画布元素一起使用?)
                How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)

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

                      • <bdo id='65hSG'></bdo><ul id='65hSG'></ul>
                      • <small id='65hSG'></small><noframes id='65hSG'>