HTML5 Canvas ctx.fillText 不支持换行
htmlweb developmentfront end technology更新于 2025/8/18 15:40:17
fillText() 方法会在 Canvas 上绘制填充文本。如果您想换行,可以在新行处拆分文本,然后多次调用 filltext() 函数。这样做会将文本拆分成几行,并分别绘制每行。
您可以尝试运行以下代码片段 −
var c = $('#c')[0].getContext('2d');
c.font = '12px Courier';
alert(c);
var str = 'first line
second line...';
var a = 30;
var b = 30;
var lineheight = 15;
var lines = str.split('
');
for (var j = 0; j
// 用于画布
<canvas id="c" width="200" height="200"></canvas>
// CSS
canvas {
background-color: #FFCE9E;
}
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

