canvas输出为图片
<canvas type="2d" id="myCanvas"></canvas>
Page({
onReady() {
const query = wx.createSelectorQuery()
query.select('#myCanvas')
.fields({ node: true, size: true })
.exec((res) => {
const canvas = res[0].node
const ctx = canvas.getContext('2d')
const dpr = wx.getSystemInfoSync().pixelRatio
canvas.width = res[0].width * dpr
canvas.height = res[0].height * dpr
ctx.scale(dpr, dpr)
ctx.fillRect(0, 0, 100, 100)
})
}
})
z针对于这种生成canvas的方式,canvas的标识为id,那么
wx.canvasToTempFilePath({
canvasId: "mycanvas",
success: function(res) {
var tempFilePath = res.tempFilePath;
that.setData({
loadImagePath: tempFilePath,
});
},
fail: function(res) {
console.log(res);
}
});
wx.canvasToTempFilePath将canvas生成为图片是根据canvasId,也就导致wx.canvasToTempFilePath获取不到canvas。这怎么办。
xin.qq.com/miniprogram/dev/component/canvas.html