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

2023-06-20前端开发问题
12

本文介绍了在带有背景的画布上书写文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

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

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455