真服了,现在接口不支持传canvasId了。得传canvas进去
wx.canvasToTempFilePath调用后没有任何反应?我直接把代码贴出来,[图片][图片] <view id="myView"> <view> 页面某些内容 </view> <view> 页面某些内容66666 </view> </view> <canvas id="myCanvas" width="300" height="300"></canvas> <button bindtap="generateCanvasImage">生成图片</button> <view> <image src="{{imageSrc}}" mode="aspectFill"></image> </view> Page({ /** * 点击按钮生成canvas图片 */ data:{ imageSrc:'' }, generateCanvasImage: function () { const query = wx.createSelectorQuery(); query.select('#myView').boundingClientRect(); query.exec((res) => { console.log('内容',res) const rect = res[0]; const width = rect.width; const height = rect.height; const left = rect.left; const top = rect.top; const ctx = wx.createCanvasContext('myCanvas', this); ctx.setFillStyle('#fff'); ctx.fillRect(0, 0, width, height); ctx.draw(false, () => { setTimeout(() => { wx.canvasToTempFilePath({ canvasId: 'myCanvas', x: left, y: top, width: width, height: height, success: (res) => { console.log('进入了生成了图片', res) this.setData({ imageSrc: res.tempFilePath, }); }, fail: (res) => { wx.showToast({ title: '生成图片失败', icon: 'none', }); }, }, this); }, 500); // 延迟500毫秒 }); }); }, /** * 点击按钮保存图片到相册 */ saveImageToAlbum: function () { wx.saveImageToPhotosAlbum({ filePath: this.data.imageSrc, success: (res) => { wx.showToast({ title: '保存成功', }); }, fail: (res) => { wx.showToast({ title: '保存失败', icon: 'none', }); }, }); }, });
2023-11-17