时间:2019-04-30 来源:小程序工厂
Draw回调加延时换成setTimeout就好了,代码如下
const context = wx.createCanvasContext('xx_canvas');
context.setFillStyle('red')
context.fillRect(10, 10, 150, 100)
console.log("1");
context.draw(false, setTimeout(function(e) {
console.log("2");
// do something...
wx.canvasToTempFilePath({
x: 0,
y: 0,
width: 650,
height: 960,
canvasId: 'xx_canvas',
fileType: 'png',
success: function (res) {
let pic = res.tempFilePath;
console.log(pic);
wx.previewImage({
urls: [res.tempFilePath],
})
}
});
},300))
draw的callback函数外面包一层等待canvas渲染的时间,不同设备有部分差异,200毫秒基本都能满足
const context = wx.createCanvasContext('xx_canvas', this)
wx.canvasToTempFilePath 也要加上第二个参数 this
再读一遍API~
https://developers.weixin.qq.com/miniprogram/dev/api/wx.createCanvasContext.html