微信小程序使用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私人局辅助,分享教程(有挂工具)1、游戏颠覆性...
手段辅助!poker红龙辅助,... 手段辅助!poker红龙辅助,德扑之心免费透视,有挂教程(有挂辅助)1、玩家可以在德扑之心免费透视透...
机巧辅助!wepoker透视最... 机巧辅助!wepoker透视最简单三个步骤,wepoker代打辅助,教你教程(有挂攻略)1、下载好w...
模板辅助!WePOker有没有... 模板辅助!WePOker有没有透视方法,菠萝德普辅助器免费版在哪里,了解教程(有挂实锤)1、首先打开...
技法辅助!wepoker透视脚... 技法辅助!wepoker透视脚本苹果版,wepoker私人局有透视吗,必备教程(新版有挂)1、金币登...
妙计辅助!哈糖大菠萝可以开挂吗... 妙计辅助!哈糖大菠萝可以开挂吗,hhpoker德州挂真的有吗,详细教程(果真有挂)1、哈糖大菠萝可以...
秘籍辅助!wepoker免费辅... 秘籍辅助!wepoker免费辅助器,德普之星透视辅助软件是真的吗,总结教程(有挂技巧)1、许多玩家不...
教材辅助!wepoker透视辅... 教材辅助!wepoker透视辅助下载,we poker游戏下,有挂教程(有挂细节)1、教材辅助!we...
绝活辅助!拱趴大菠萝十三水作弊... 绝活辅助!拱趴大菠萝十三水作弊,wepoker有没有机器人,专业教程(有挂讲解)1、任何拱趴大菠萝十...
讲义辅助!aapoker能控制... 讲义辅助!aapoker能控制牌吗,约局吧能不能开挂,必备教程(存在有挂)1、实时约局吧能不能开挂透...