async share () {
var that = this;
wx.showLoading({
title: '图片生成中...',
})
let photo = '' //图片地址
let result = await that .getCanvas('companyName', 'name', 'position', photo) //生成图片
wx.hideLoading()
},
getCanvas(companyName, personName, position, photo) {
return new Promise((resolve, reject) => {
wx.getImageInfo({ //获取图片信息
src: photo,
success: (res) => {
let pic = res.path
const ctx = wx.createCanvasContext('shareCanvas')
// 背景图
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, 320, 120);
// ctx.fillStyle = "#469cf8";
// ctx.fillRect(0, 20, 4, 20);
// 标题
ctx.font = "bold 10px arial";
ctx.fillStyle = "#469cf8";
ctx.fillText(companyName || '', 0, 10);
ctx.font = "bold 14px arial";
ctx.fillStyle = "#222";
ctx.fillText(personName || '', 54, 40);
ctx.font = "bold 10px arial";
ctx.fillStyle = "#A8A8A8";
ctx.fillText(position || '', 54, 60);
ctx.drawImage(pic, 0, 24, 50, 50)
ctx.draw(false, function (){ // 绘制画布回调函数
//把当前画布指定区域的内容导出生成指定大小的图片。在 draw() 回调里调用该方法才能保证图片导出成功。
wx.canvasToTempFilePath({
x: 0,
y: 0,
canvasId: 'shareCanvas',
success: (res) => {
wx.saveFile({ //保存文件到本地
tempFilePath: res.tempFilePath,
success (res) {
resolve(res.savedFilePath)
},
fail (err) {
reject(err)
}
})
}
});
})
}
})
})
小程序画布Canvas生成海报,分享、转发
最新推荐文章于 2023-03-14 18:21:28 发布