wx.createSelectorQuery()
.select('#secondCanvas')
.fields({ node: true, size: true })
.exec(
(res) => {
console.log('1111111111111111111')
console.log(res)
// Canvas 对象
const canvas = res[0].node
// Canvas 画布的实际绘制宽高
const renderWidth = res[0].width
const renderHeight = res[0].height
// Canvas 绘制上下文
const ctx = canvas.getContext('2d')
// 初始化画布大小
const dpr = wx.getSystemInfoSync().pixelRatio
canvas.width = renderWidth * dpr
canvas.height = renderHeight * dpr
ctx.scale(dpr, dpr)
// 若干绘制调用
// 这里绘制完成
console.log('draw done')
const img = canvas.createImage()
img.onload = () => {
ctx.drawImage(
img,0,0)
}
img.src='https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon64_wx_logo.png'
wx.downloadFile({
url: 'https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon64_wx_logo.png',
success: function(res) {
// 下载成功后将文件转为临时路径
if (res.statusCode === 200) {
wx.setStorageSync('tempFilePath', res.tempFilePath);
}
}
});
this.setData({
img: wx.getStorageSync('tempFilePath'),
canvasHeight: canvas.height,
canvasWidth: canvas.width,
});
const { canvasWidth, canvasHeight, deviceId, threshold } = this.data;
const pix =res[0].node;
const opt = {
deviceId,
...this.character,
onProgress: (percentage) => {
console.log('percentage', percentage);
this.setData({
percentage,
});
},
lasterSuccess: () => {
console.log('laster success');
this.setData({
printing: false,
percentage: 0,
});
wx.showModal({
title: '提示',
content: '数据已发送完,请检查打印的内容是否正常',
showCancel: false,
confirmText: '好的',
});
},
};
this.setData({
printing: true,
});
console.log(threshold[0])
console.log(pix)
console.log(opt)
//打印图片
printImage(
opt,
overwriteImageData({
threshold: threshold[0],
imageData: pix,
width: canvasWidth,
height: canvasHeight,
}),
);
}
楼主找到解决办法了吗?我也碰到了,解决了给我个demo,谢谢