越王够贱
越王够贱
  • 发布:2022-06-16 14:00
  • 更新:2022-06-16 14:19
  • 阅读:743

canvas循环生成图片只绘制最后一张

分类:uni-app

this.prizeList.forEach(function (item, index) {
_that.initMask(index);
});

uni.canvasToTempFilePath({
destWidth: this.higtCanvasSize / 2,
destHeight: this.higtCanvasSize / 2 ,
canvasId: this.maskId,
success: (res) => {
// 在 H5 平台下,tempFilePath 为 base64
//console.log(res.tempFilePath)
// this.handleMaskImg({
// ok: true,
// data: res.tempFilePath,
// msg: '画布导出生成图片成功'
// })
this.prizeList[index].maskImg = res.tempFilePath;
//console.log('mask:'+res.tempFilePath)
},

2022-06-16 14:00 负责人:无 分享
已邀请:
者行孙

者行孙

目测是没有把绘制任务队列化导致的,同时画在一个画布上肯定只有最后一张。

按理来说绘制任务应该用Promise封装一下,等待上一张绘制完后再绘制下一张。

const tasks = []; // 绘制任务参数数组  
const results = []; // 绘制结果数组  

for (const task of tasks) {  
    const result = await new Promise((resolve) => {  
         // 绘制代码  

         uni.canvasToTempFilePath({  
            // ...  
            success: resolve  
        })  
    })  
    results.push(result);  
}  

console.log(results)
  • 越王够贱 (作者)

    谢谢 用这方法解决了

    2022-06-18 12:55

要回复问题请先登录注册