1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| var app = getApp(); Page({ data: { decorationImage: '../img/1.jpg', canvasW: 300, canvasH: 300, canvasWidth: 250, canvasHeight: 250, smallImage: 74, marginWidth: 25, headerImage: '' }, onReady: function () { this.drawCanvas() }, setHeader() { let that = this wx.chooseImage({ count: 1, sizeType: 'original', sourceType: 'album', success: function (res) { var tempFilePaths = res.tempFilePaths console.log(tempFilePaths) that.setData({ headerImage: tempFilePaths[0] }); that.drawCanvas() } }) }, roundRectColor: function (context, x, y, w, h, r) { context.save(); context.fillStyle = "#fff"; context.strokeStyle = '#fff'; context.lineJoin = 'round'; context.lineWidth = r; context.strokeRect(x + r / 2, y + r / 2, w - r, h - r); context.fillRect(x + r, y + r, w - r * 2, h - r * 2); context.stroke(); context.closePath(); }, drawCanvas: function () { var that = this; let ctx = wx.createCanvasContext('share_canvas'); this.roundRectColor(ctx, that.data.marginWidth, that.data.marginWidth, this.data.canvasWidth, this.data.canvasHeight, 30) if (that.data.headerImage) { ctx.drawImage(that.data.headerImage, 35, 35, this.data.canvasWidth - 20, this.data.canvasHeight - 20) } let x = this.data.canvasWidth - (that.data.smallImage - 20) let y = this.data.canvasWidth - (that.data.smallImage - 20) ctx.drawImage(this.data.decorationImage, x, y, that.data.smallImage, that.data.smallImage) ctx.draw(); }, saveImage: function () { wx.showLoading({ title: '正在保存图片..', }); let that = this; wx.canvasToTempFilePath({ x: that.data.marginWidth, y: that.data.marginWidth, fileType: 'jpg', width: that.data.canvasW - (that.data.marginWidth * 2), height: that.data.canvasH - (that.data.marginWidth * 2), destWidth: 960, destHeight: 960, canvasId: 'share_canvas', success: function (res) { wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success(res) { console.log(res); wx.showToast({ title: '保存到相册成功', duration: 1500, }) }, fail(res) { console.log(res) wx.showToast({ title: '保存到相册失败', icon: 'fail' }) }, complete(res) { console.log(res) } }) } }) }
|