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

相关内容

热门资讯

透视辅助!多乐辅助下载够机(辅... 透视辅助!多乐辅助下载够机(辅助挂)一直有挂(详细辅助技巧教程)透视辅助!多乐辅助下载够机(辅助挂)...
wepokeai代打!德州线上... wepokeai代打!德州线上扑克辅助工具app,(wePoKe)竟然真的是有挂(有挂方法)1、很好...
透视辅助!越乡游破解(辅助挂)... 透视辅助!越乡游破解(辅助挂)好像是真的有挂(详细辅助总结教程)越乡游破解软件透明挂微扑克wpk插件...
wepoke辅助技巧!wepo... wepoke辅助技巧!wepower透视辅助,(Wepoke)原生真的有挂(有挂技巧)是一款可以让一...
透视辅助!微信小程序指尖四川辅... 透视辅助!微信小程序指尖四川辅助器(辅助挂)切实是真的有挂(详细辅助透明教程);1、微信小程序指尖四...
wepoke智能ai!德扑人工... wepoke智能ai!德扑人工智能,(WepOke)本然是有挂(有挂透明);wepoke智能ai是一...
透视辅助!西元文山攻略(辅助挂... 透视辅助!西元文山攻略(辅助挂)好像是有挂(详细辅助存在挂教程);西元文山攻略软件透明挂微扑克wpk...
wepoke一定有挂!wpk微... wepoke一定有挂!wpk微扑克系统发牌规律,(wePOKE)先前是有挂(有挂方法);致您一封信;...
透视辅助!多乐跑的快脚本(辅助... 透视辅助!多乐跑的快脚本(辅助挂)果然真的是有挂(详细辅助力荐教程);1、多乐跑的快脚本透视辅助简单...
wepoke有辅助挂!wpk德... wepoke有辅助挂!wpk德州ai,(wEpOke)确实有挂(有挂攻略);wepoke有辅助挂黑科...