我直接把代码贴出来,
<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',
});
},
});
},
});
真服了,现在接口不支持传canvasId了。得传canvas进去
canvas标签写错了,<canvas canvas-id="myCanvas" />这是老版的,<canvas id="myCanvas" type="2d" />这是新版的
你照着官方demo对一下吧
我点击按钮后,生成图片的那段代码里的console没有然后反应