canvas绘制多行文字
代码如下:
let Canvas = document.createElement(‘canvas’);
let ctx = Canvas.getContext(“2d”);
let scaleBy = 2;
let fontSize = 12;
let lineheight = fontSize * scaleBy * 1.4; //行高为size1.4倍
let {x, y} = {30, 10};
let line = ‘你好#该我换行了#再见’
ctx.textAlign = ‘left’; //可以选择center right
ctx.font = fontSize * scaleBy + “px PingFangSC”;
line.split(‘#’).forEach((item, i) => {
ctx.fillText(item, x3 * scaleBy, y3 * scaleBy + (i * lineheight));
})
let nodeImg = document.createElement(“img”);
nodeImg.src = Canvas.toDataURL();
document.body.appendChild(nodeImg)
阿里云限时活动-云数据库 RDS MySQL 1核2G配置 1.88/月 速抢
本文由乐趣区整理发布,转载请注明出处,谢谢。