如果要做一个分享朋友圈,而需要通过canvas绘制图片,在真机上是不显示你的用户头像的,但是开发者工具和真机上打开调试是可以看的。这就需要在微信公众平台把你头像前面的网址配置到downloadFile网址里去
比如下面的头像,就需要把https://wx.qlogo.cn添加到downloadFile网址去,然后清除缓存,重新打开就可以看到绘制的头像了
"avatarUrl":"https://wx.qlogo.cn/mmopen/vi_32/F8sadfIasdasdfadf"},还有这个头像地址需要下载到本地才能去绘制
重点注意
1,这就需要在微信公众平台把你头像前面的网址配置到downloadFile网址里去。
2,"avatarUrl":"https://wx.qlogo.cn/mmopen/vi_32/F8sadfIasdasdfadf"},还有这个头像地址需要下载到本地才能去绘制
downloadFile 该方法去下载到本地。
openfun() {
let that= this;
wx.downloadFile({
url: that.data.avatarUrl,
success: function (res) {
// console.log(res.tempFilePath); //还有下载过的头像图片才能去绘制
that.setData({
avatarUrl:res.tempFilePath,
})
//绘图方法
that.drawImage();
}, fail: function (fres) {
}
})
},
//绘制方法
drawImage:function() {
var that = this
const ctx = wx.createCanvasContext('sharePhoto',this)
// console.log(wx.getSystemInfoSync())
var Width = wx.getSystemInfoSync().windowWidth*0.74
var Height = wx.getSystemInfoSync().windowHeight * 0.68
var avatarUrl = that.data.avatarUrl
ctx.save();
ctx.beginPath();
ctx.arc(Width * 0.5, Height * 0.175, Width * 0.098, 0, 2 * Math.PI);
ctx.clip();//画了圆 再剪切
ctx.drawImage(avatarUrl, Width * 0.4, Height * 0.109, Width*0.197, Height*0.132)
ctx.restore();
ctx.save();
ctx.draw();
},