let canvas= document.createElement('canvas'); //创建canvas 对象
canvas.width = 500;
canvas.height = 500;
let pen = canvas.getContext('2d'); //创建context 对象
let img = new Image(); //创建img对象做为背景
img.src = 'img/2.jpg';
img.onload = ()=>{ //这个方法很重要,在图片加载完再开始绘制,不然背景会是空白
pen.drawImage(img,0,0,500,400);
pen.font = '60px Georgia';
pen.fillStyle = '#ffcd00';
pen.textBaseline = 'top'
pen.fillText('七七',200,200);
let dataUrl = canvas.toDataURL('image/jpge'); //转换换成base64位格式路径
}
ps 你可以在页面上创建一个div <div id="main"></div>
然后 document.getElementById('main').appendChild(canvas);添加到页面上查看效果