canvas 文本样式处理

该代码段展示了如何在HTML5Canvas上处理文本样式,包括使文本居中和实现自动换行。它使用了`measureText`方法来测量文本宽度,并动态调整坐标以达到居中效果。此外,还通过遍历文本并检查宽度来处理换行。
摘要由CSDN通过智能技术生成

canvas 文本样式处理

文本居中和自动换行

var _self = this
var _canvas = document.createElement('canvas');
var _context = _canvas.getContext("2d")
_canvas.width = 500
_canvas.height = 600

//调用示例
var _obj = {
    ontList: '测试文案',
    isCenter: false,
    top: 120,
    leftAndright: 10,
    lineH: 13,
    lineW: 0,
    font: '30px Courier New',
    fillStyle: 'black'
}
determineNumberRows(_obj)

//fontList:文本内容 ,
//isCenter: 不满一行是否要文本居中,
//top: 距顶部高度,
//leftAndright:左右边距,
//lineH: 行高,
//lineW,文字的间距
function determineNumberRows(_obj,_w = _obj.leftAndright + _obj.lineW / 2) {
   _context.font = _obj.font
   var _font_w = _context.measureText('例').width + _obj.lineW / 2
   var _h = _obj.top + _font_w
   var _k = false
   _context.fillStyle = _obj.fillStyle
   if(_obj.isCenter) _w = (_canvas.width - _obj.fontList.split('').length * (_font_w  + _obj.lineW / 2) + _obj.leftAndright * 2) / 2
          _obj.fontList.split('').forEach((T,I)=> {
          if(_obj.isCenter && _canvas.width > (_obj.fontList.split('').length * (_font_w  + _obj.lineW / 2) + _obj.leftAndright)) {
                   if(_k) _w += _font_w + _obj.lineW / 2
                           _k = true
           } else {
                  if(_canvas.width <= _w + _font_w + _obj.leftAndright + _obj.lineW / 2) {
                      _h += _font_w + _obj.lineH
                      _w = _obj.leftAndright + _obj.lineW / 2
                  } else {
                      if(_k) _w += _font_w + _obj.lineW / 2 
                            _k = true
                  }
          }
          _context.fillText(T,_w,_h);
      })
      _self.base64 = _canvas.toDataURL("image/png")
}
                          
要在 Canvas 中实现文本的首行缩进和换行处理,您可以使用以下方法: 1. 手动处理换行:您可以通过在适当的位置插入换行符 "\n" 来手动控制文本的换行。例如,如果要在 Canvas 上绘制一段文本,并在第一行进行缩进,您可以将文本拆分为多个字符串,并在需要换行的地方添加换行符。 ```javascript var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var text = "This is a long text that needs to be indented on the first line and wrapped to the next line if it exceeds a certain width."; var indent = 50; // 首行缩进的像素值 var maxWidth = 200; // 文本的最大宽度 var words = text.split(" "); var line = ""; var lines = []; for (var i = 0; i < words.length; i++) { var testLine = line + words[i] + " "; var metrics = ctx.measureText(testLine); var testWidth = metrics.width; if (testWidth > maxWidth) { lines.push(line); line = words[i] + " "; } else { line = testLine; } } lines.push(line); var lineHeight = 20; // 行高 var y = 100; // 文本起始位置的垂直坐标 for (var j = 0; j < lines.length; j++) { ctx.fillText(lines[j], indent, y + j * lineHeight); } ``` 2. 使用 CSS 样式处理换行:您也可以使用 CSS 的样式属性来处理 Canvas 中的文本换行。首先,您需要将 Canvas 的 CSS 样式设置为包含一个固定的宽度,并将 `white-space` 属性设置为 `"pre-wrap"` 或 `"pre"`,以保留文本中的空格和换行符。然后,您可以使用 `ctx.fillText()` 方法在 Canvas 上绘制文本。 ```html <style> #myCanvas { width: 200px; } </style> <canvas id="myCanvas"></canvas> <script> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var text = "This is a long text that needs to be indented on the first line and wrapped to the next line if it exceeds a certain width."; var indent = 50; // 首行缩进的像素值 canvas.style.whiteSpace = "pre-wrap"; ctx.font = "16px Arial"; ctx.fillText(text, indent, 100); </script> ``` 这些方法可以帮助您在 Canvas 中实现文本的首行缩进和换行处理。请根据您的需求选择适合您的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值