一开始想着将画布整体放大,然后使用scale或者zoom将canvas缩小的方式,在微信开发工具上使用没有问题,一阵欣喜。
高兴太早了,在真机上不能使用。canvas直接不显示了。
解决
在我们使用wx.canvasToTempFilePath的时候,设置参数destWidth和destHeight(输出的宽度和高度)为width和height的2倍以上即可。
// 保存图片
saveImage() {
wx.canvasToTempFilePath({
x: 0,
y: 0,
width: 150,
height: 218,
destWidth: 150 * 20, // width*屏幕像素密度
destHeight: 218 * 20, // height*屏幕像素密度
canvasId: 'canvasId',
quality: 1,
success: (res) => {
// 在H5平台下,tempFilePath 为 base64
console.log(res.tempFilePath)
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
this.$u.toast('保存成功!')
}
})
}
})
}