微信小程序使用canvas
创始人
2024-12-01 00:34:10
0

引用:https://blog.csdn.net/skyblacktoday/article/details/83214546

小程序不能直接将网络图片画到canvas上,,需要使用wx.getImageInfo将图片存到本地,,然后再画入canvas ,

  
 generateCard(){      const {canvasWidth} = this.data     const ctx = wx.createCanvasContext('canvas')     ctx.width = canvasWidth     ctx.drawImage("../../assets/recommend_scene/certificate.jpg",0,0,canvasWidth,300)     ctx.setFontSize(24)     ctx.setFillStyle("#000")     ctx.fillText(this.data.userinfo.username,170,218)        // 绘制圆角矩形路径并裁剪      const imgX = 190; // 图片的X坐标      const imgY = 156; // 图片的Y坐标      const imgWidth = 40; // 图片的宽度      const imgHeight = 40; // 图片的高度      const borderRadius = 20; // 圆角半径        ctx.save(); // 保存当前的绘图上下文       // 创建圆角矩形路径       ctx.beginPath();       ctx.moveTo(imgX + borderRadius, imgY);       ctx.arcTo(imgX + imgWidth, imgY, imgX + imgWidth, imgY + imgHeight, borderRadius);       ctx.arcTo(imgX + imgWidth, imgY + imgHeight, imgX, imgY + imgHeight, borderRadius);       ctx.arcTo(imgX, imgY + imgHeight, imgX, imgY, borderRadius);       ctx.arcTo(imgX, imgY, imgX + imgWidth, imgY, borderRadius);       ctx.closePath();       ctx.clip(); // 裁剪路径       wx.getImageInfo({         src: this.data.userinfo.avatar,         success:(res)=>{           console.log(res,"123")           ctx.drawImage(res.path,imgX,imgY,imgWidth,imgHeight)           ctx.restore(); // 恢复之前保存的绘图上下文           ctx.draw(false,(res)=>{             wx.canvasToTempFilePath({               canvasId:"canvas",               success:(res)=>{                 this.setData({                   imageData:res.tempFilePath                 })               },               fail:(err)=>{                 wx.showToast({                   title: '生成图片失败',                   icon:"none"                 })               }             })           })         }       })           }, 
  saveCard(){     const { imageData } = this.data     if(imageData){       wx.saveImageToPhotosAlbum({         filePath: imageData,         success:()=>{           wx.showToast({             title: '卡片已保存到相册',           })         },         fail:(err)=>{           wx.showToast({             title: '保存失败',             icon:"none"           })         }       })     }else{       wx.showToast({         title: '请先生成卡片',       })     }   }, 

相关内容

热门资讯

黑科技辅助(nzt德州)外挂软... 黑科技辅助(nzt德州)外挂软件透明挂智能ai代打辅助app(透视)必赢教程(2025已更新)(哔哩...
推荐一款“德扑之星有没有透视挂... 推荐一款“德扑之星有没有透视挂”(透视)详细开挂辅助教程技术的发展为玩家提供了更多的工具和策略来提升...
黑科技辅助(impoker)外... 黑科技辅助(impoker)外挂软件透明挂智能ai辅助神器(透视)德州论坛(2020已更新)(哔哩哔...
一分钟教会你“智星德州辅助”(... 一分钟教会你“智星德州辅助”(透视)详细开挂辅助技巧在中,检测透视功能可通过以下方法:监测玩家的行为...
黑科技辅助(德扑之星比赛)外挂... 黑科技辅助(德扑之星比赛)外挂软件透明挂智能ai代打辅助下载(透视)解密教程(2026已更新)(哔哩...
记者爆料“德扑之星能开挂吗”(... 记者爆料“德扑之星能开挂吗”(透视)详细开挂辅助方法牌局中的透视功能,其实质源自复杂的数据交互机制,...
黑科技辅助(wepoke线上)... 【福星临门,好运相随】;黑科技辅助(wepoke线上)外挂软件透明挂智能ai辅助下载(透视)分享教程...
避坑细节“wepoker有没有... 避坑细节“wepoker有没有作z弊挂”(透视)详细开挂辅助教程wepoker德州扑克透视工具提供增...
实操分享“we poker透视... 实操分享“we poker透视”(透视)详细开挂辅助教程即使拥有再好的“透视”能力没有扎实的基本功也...
黑科技辅助(德州app)外挂软... 黑科技辅助(德州app)外挂软件透明挂智能ai辅助软件(透视)可靠教程(2021已更新)(哔哩哔哩)...