diff --git a/index.js b/index.js new file mode 100644 index 0000000..3e74cc4 --- /dev/null +++ b/index.js @@ -0,0 +1,191 @@ +// index.js +Page({data: { +}, +onLoad: function (options) { + var that = this; + that.setData({ + imgUrl: options.imgUrl, + img1: options.img1 + }) + console.log(that.data.imgUrl + '' + that.data.img1) + wx.getSystemInfo({ + success: function (res) { + var clientHeight = res.windowHeight; + var clientWidth = res.windowWidth; + that.setData({ + clientWidth: clientWidth, + clientHeight: clientHeight, + }) + } + }) + wx.getImageInfo({ + src: that.data.imgUrl, + success: function (res) { + console.log(res) + that.setData({ + width1: res.width, + height1: res.height + }) + } + }) + wx.getImageInfo({ + src: that.data.img1, + success: function (res) { + console.log(res) + that.setData({ + width2: res.width, + height2: res.height + }) + } + }) +}, +combine:function(){ + if (this.data.clientWidth > this.data.width2) { + var imgwidth1 = this.data.width1 + var imgheight1 = this.data.height1 + var imgwidth2 = this.data.width2 + var imgheight2 = this.data.height2 + } + else { + var imgwidth2 = this.data.clientWidth + var imgheight2 = this.data.height2 * imgwidth2 / this.data.width2 + if (imgheight2 < this.data.height1) + { + var imgheight1 = imgheight2 + var imgwidth1 = this.data.width1 * imgheight1 / this.data.height1 + } + else{ + var imgheight1 = this.data.height1 + var imgwidth1 = this.data.width1 * imgheight1 / this.data.height1 + } + } + console.log(imgwidth2 + '' + imgheight2 + imgwidth1 + '' + imgheight1) + const ctx = wx.createCanvasContext('shareCanvas') + ctx.drawImage(this.data.img1, 0, 0, this.data.width2, this.data.height2,0,0,imgwidth2, imgheight2) + ctx.drawImage(this.data.imgUrl, 0, 0, this.data.width1, this.data.height1,0,0, imgwidth1, imgheight1) + ctx.draw() + var that = this + wx.showToast({ + title: '图片生成中...', + icon: 'loading', + duration: 1000 + }); + setTimeout(function () { + wx.canvasToTempFilePath({ + canvasId: 'shareCanvas', + success: function (res) { + var tempFilePath = res.tempFilePath; + wx.saveImageToPhotosAlbum({ + filePath: tempFilePath, + success(res) { + wx.showToast({ + title: '保存成功', + icon: 'success' + }) + } + }) + wx.hideToast() + }, + fail: function (res) { + console.log(res); + } + }); + }, 500); + +}, +/** + * 生命周期函数--监听页面初次渲染完成 + */ +onReady: function () { + +}, + +/** + * 生命周期函数--监听页面显示 + */ +onShow: function () { + +}, + +/** + * 生命周期函数--监听页面隐藏 + */ +onHide: function () { + +}, + +/** + * 生命周期函数--监听页面卸载 + */ +onUnload: function () { + +}, + +/** + * 页面相关事件处理函数--监听用户下拉动作 + */ +onPullDownRefresh: function () { + +}, + +/** + * 页面上拉触底事件的处理函数 + */ +onReachBottom: function () { + +}, + +/** + * 用户点击右上角分享 + */ +onShareAppMessage: function () { + +}, +//适配不同屏幕大小的canvas +setCanvasSize: function (width) { + var size = {}; + try { + var res = wx.getSystemInfoSync(); + var scale = 750 / width; + // var scale = 1 + var width = res.windowWidth / scale; + var height = res.windowHeight / scale;; + size.w = width; + size.h = height; + } catch (e) { + // Do something when catch error + 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) { + + } + }); + } + }) +}, +})