在开发去水印小程序时,若需要为处理后的图片添加或修改背景颜色,可以通过以下几种方法实现:
img.onload = () => {
ctx.drawImage(img, 0, 0, 300, 300); // 绘制图片到画布
ctx.fillStyle = 'FF6B6B'; // 设置背景色
ctx.fillRect(0, 0, 300, 300); // 覆盖纯色背景(若需保留图片透明区域需调整)
ctx.draw(false, () => {
wx.canvasToTempFilePath({
canvasId: 'canvas',
success: res => console.log('带背景色图片路径:', res.tempFilePath)