微信小程序海报
Page({
data: {
imgUrl: "项目中图片地址",
img: ''
},
onLoad: function (options) {
let that = this;
wx.downloadFile({
url: '线上图片地址',
success(res) {
var postersize = that.setCanvasSize(750);
const ctx = wx.createCanvasContext('shareCanvas')
ctx.drawImage(that.data.imgUrl, 0, 0, postersize.w, postersize.h)
var re = wx.getSystemInfoSync();
var scale = 750 / 180;
var width = re.windowWidth / scale;
var height = width
var leftscale = 750 / 480;
var left = re.windowWidth / leftscale;
var topscale = 750 / 880;
var top = re.windowWidth / topscale;
ctx.drawImage(res.tempFilePath, left, top, width, height)
ctx.draw()
setTimeout(() => {
wx.canvasToTempFilePath({
canvasId: 'shareCanvas',
success: function (res) {
var tempFilePath = res.tempFilePath;
that.setData({
img: tempFilePath
})
console.log(tempFilePath)
},
fail: function (res) {
console.log(res);
}
});
}, 1000);
}
})
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
},
setCanvasSize: function (width) {
var size = {};
try {
var res = wx.getSystemInfoSync();
var scale = 750 / width;
var width = res.windowWidth / scale;
var height = res.windowHeight / scale;;
size.w = width;
size.h = height;
} catch (e) {
console.log("获取设备信息失败" + e);
}
return size;
},
previewImg: function (e) {
var img = this.data.img;
let _this = this;
wx.saveImageToPhotosAlbum({
filePath: img,
success: function (res) {
wx.showModal({
content: '保存成功',
confirmText: '确认',
showCancel: false,
success: function (res) {
}
});
},
fail: function (res) {
wx.showModal({
content: '保存失败',
confirmText: '确认',
showCancel: false,
success: function (res) {
}
});
}
})
},
})