微信小程序使用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: '请先生成卡片',       })     }   }, 

相关内容

热门资讯

透视苹果版!poker wor... 透视苹果版!poker world辅助器,福建天天开心辅助工具视频(原来真的有挂)福建天天开心辅助工...
透视软件!aapkoer德州辅... 透视软件!aapkoer德州辅助挂下载,hhpoker可以控制牌吗(详细辅助安装教程);人气非常高,...
透视app“wepoker透视... 透视app“wepoker透视脚本免费下载”一直是有挂(辅助挂)详细辅助AI教程1、透视辅助简单,软...
wpk辅助器安装!wepoke... wpk辅助器安装!wepoker分析,wepoker脚本下载(详细辅助实用技巧)1、不需要AI权限,...
透视透视!wepoker透视a... 透视透视!wepoker透视app下载,菠萝神辅助器app(真是是有挂);1、下载好菠萝神辅助器ap...
透视app!wpk辅助器,po... 1、透视app!wpk辅助器,pokemmo免费脚本(详细辅助微扑克教程)。2、pokemmo免费脚...
透视规律“wepoker透视脚... 透视规律“wepoker透视脚本下载”其实真的是有挂(辅助挂)详细辅助解密教程是一种具有地方特色的麻...
hhpoker透视脚本视频!w... hhpoker透视脚本视频!we poker免费辅助器,wpk辅助购买(详细辅助技巧教程),您好,h...
透视免费!德普之星私人局透视,... 透视免费!德普之星私人局透视,微乐小程序辅助工具(一贯是有挂)1、该软件可以轻松地帮助玩家将微乐小程...
透视辅助!wepoker透视脚... 透视辅助!wepoker透视脚本苹果版,wepoker有辅助器吗(详细辅助力荐教程)是一款可以让一直...