//文字转图片
function textToImage(text){
let tLength = text.length;//获取文本个数
var canvas = document.createElement("canvas");//创建画布
canvas.width = tLength*15;//设置画布宽度,15为字体大小
canvas.height = 22;//设置画布高度
var context = canvas.getContext("2d"); //获取绘图环境
context.textAlign = "center";//设置居中
context.fillStyle = "#fff";//设置白色画笔
context.fillRect(0, 0, canvas.width, canvas.height);//绘制白色背景,rect为方形
context.font = "blod 15px 宋体";//加粗,字号,字体
context.fillStyle = "#000";//设置黑色画笔
context.fillText(text, canvas.width/2, canvas.height/2);//添加文字
return canvas.toDataURL("image/jpeg")//返回Base64
}
————————————————
版权声明:本文为CSDN博主「学习要趁早z」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42677452/article/details/116052786