this.$nextTick(()=> {
const image = cvs.createImage()
image.onload = () => {
// 绘制图片
ctx.drawImage(image, x, y)
// 取像素数据
const imgData = ctx.getImageData(0, 0, maxLen, maxLen)
const imgDataLen = imgData.width * imgData.height
// 扣透明的逻辑
for (let i = 0; i < imgDataLen; i++) {
const red = imgData.data[i * 4]
const green = imgData.data[i * 4 + 1]
const blue = imgData.data[i * 4 + 2]
if ((red >= 150 && green >= 150 && blue >= 150) || (red <= 20 && green <= 20 && blue <= 20)) {
imgData.data[i * 4] = 0
imgData.data[i * 4 + 1] = 0
imgData.data[i * 4 + 2] = 0
imgData.data[i * 4 + 3] = 0
}
}
// 存像素数据,下面这个方法 putImageData 在ios系统未生效
ctx.putImageData(imgData, 0, 0)
// 生成临时图片路径
wx.canvasToTempFilePath({
canvas: cvs,
quality: 1, // 图片质量
fileType: 'png',
x: 0,
y: 0,
width: maxLen, // cvs的宽高
height: maxLen,
destWidth: maxLens, // 图片的宽高
destHeight: maxLens,
success: (res) => {
resolve(res.tempFilePath)
},
})
}
image.src = imgUrl
})