恶补canvas。。。
const ctx = wx.createCanvasContext('myCanvas');
/**/获取设备信息
wx.getSystemInfo({
success: function (res) {
let clientHeight = res.windowHeight;
let clientWidth = res.windowWidth;
//获取屏幕宽度,获取自适应单位
let rpx = res.windowWidth / 375;
that.setData({
clientHeight: clientHeight,
clientWidth: clientWidth,
rpx:rpx
});
}
});
// 生成带参数二维码
this.drawBgPic(ctx,that,function () {
wx.request({
url: 'https://www.xxxxxx.com/xxx/wx_qrcode',
data: {
page: 'pages/xxx/xxx'
},
responseType: 'arraybuffer',
success: function (res) {
//网络图片要下载到本地以后才能绘制
var qrImg = wx.arrayBufferToBase64(res.data);
that.setData({
code: qrImg
})
const fsm = wx.getFileSystemManager(), //文件管理器
FILE_BASE_NAME = 'tmp_base64src', //文件名
format = 'png', //文件后缀
base64Str = that.data.code
var buffer = wx.base64ToArrayBuffer(base64Str), //base 转二进制
filePath = `${wx.env.USER_DATA_PATH}/www.${format}`; //文件名
fsm.writeFile({ //写文件
filePath,
data: buffer,
encoding: 'binary',
success(res) {
wx.getImageInfo({ //读取图片
src: filePath,
success: function (res) {
that.circleImg(ctx, res.path, 200 * that.data.rpx, 250 * that.data.rpx, 55 * that.data.rpx)
that.drawName(ctx,that)
},
})
}
})
},
complete:function(){
wx.hideLoading()
}
})
});
//圆形二维码
circleImg: function (ctx, img, x, y, r) {
ctx.save();
var d = 2 * r;
var cx = x + r;
var cy = y + r;
ctx.beginPath();
ctx.arc(cx, cy, r, 0, 2 * Math.PI);
ctx.clip();
ctx.drawImage(img, x, y, d, d);
},
/**
* 生成分享图
*/
/**
* 保存到相册
*/
save: function () {
var that = this;
//获取相册授权
wx.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
that.savaImageToPhoto();
}
})
} else {
that.savaImageToPhoto();
}
}
})
},
drawName: function(ctx,that,callback){
ctx.save();
ctx.setFontSize(20)
ctx.fillText(that.data.nickname, 10, 10)
ctx.draw();
ctx.save();
if(callback){ callback() }
},
drawBgPic: function (ctx,that,callback) {
wx.showLoading({
title: '努力生成中',
mask:true
})
var path = 'https://www.xxxxx.com/img/xxx.png';
wx.getImageInfo({
src: path, //服务器返回的图片地址
success: function (res) {
//res.path是网络图片的本地地址
let Path = res.path;
ctx.drawImage(Path, 0, 0, 325 * that.data.rpx , 450 * that.data.rpx)
ctx.setTextAlign('left')
ctx.setFillStyle('#000')
ctx.setFontSize(18*that.data.rpx)
ctx.fillText(that.data.nickname, (325 / 7) * that.data.rpx, 275*that.data.rpx)
callback()
},
fail: function (res) {
console.log(res)
//失败回调
}
});
},
//保存
saveewm: function () {
let that = this;
wx.showLoading({
title: '努力生成中...'
})
wx.canvasToTempFilePath({
x: 0,
y: 0,
width: that.data.winWidth,
height: that.data.winHeight - 70,
destWidth: that.data.winWidth,
destHeight: that.data.winHeight - 70,
canvasId: 'myCanvas',
success: function (res) {
wx.hideLoading()
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
wx.showModal({
content: '图片已保存到相册了',
showCancel: false,
confirmText: '我知道啦',
confirmColor: '#232323',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定');
that.setData({
hidden: true
})
}
}
})
}
})
},
fail: function (res) {
console.log(res)
}
})
},