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

相关内容

热门资讯

透视讲解!wepoker辅助软... 透视讲解!wepoker辅助软件价格(透视)wepoker能不能透视(切实真的有挂)wepoker辅...
透视计算!德州透视脚本(透视)... 透视计算!德州透视脚本(透视)黑科技教程(2020已更新)(哔哩哔哩)1、用户打开应用后不用登录就可...
透视苹果版!wepoker透视... 透视苹果版!wepoker透视脚本(透视)辅助是真的假的(原来是有挂)1、点击下载安装,wepoke...
透视插件!wepokerplu... 透视插件!wepokerplus脚本(透视)wepoker是不是有人用挂(都是有挂)1、wepoke...
透视挂!pokermaster... 透视挂!pokermaster脚本(透视)必胜教程(2022已更新)(哔哩哔哩);1、pokerma...
透视ai代打!wepoker透... 透视ai代打!wepoker透视脚本免费使用视频(透视)辅助透视软件(一直真的是有挂)1、首先打开w...
透视脚本!WePoKer辅助器... 透视脚本!WePoKer辅助器(透视)wepoker脚本下载(竟然是有挂)1、下载好wepoker脚...
透视好友!wepoker有没有... 透视好友!wepoker有没有机器人(透视)有机器人(都是有挂)1、让任何用户在无需wepoker有...
透视存在!哈糖大菠萝有挂吗(透... 透视存在!哈糖大菠萝有挂吗(透视)可靠技巧(2023已更新)(哔哩哔哩);哈糖大菠萝有挂吗辅助器中分...
透视ai!wepokerplu... 透视ai!wepokerplus到底是挂了吗(透视)wepoker私人局透视教程(总是存在有挂);1...