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

相关内容

热门资讯

2027版开挂!乐乐川南字牌开... 2027版开挂!乐乐川南字牌开挂多功能AI软件,乐乐川南字牌好像真的有开挂软件1、乐乐川南字牌破解器...
2027版透视器!紫峰麻将透视... 2027版透视器!紫峰麻将透视器多功能AI攻略,紫峰麻将切实存在有透视器攻略1、紫峰麻将透视辅助软件...
2027版透视挂!一起温州麻将... 您好,一起温州麻将这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在...
2027版开挂!乐吧游戏开挂多... 2027版开挂!乐吧游戏开挂多功能AI脚本,乐吧游戏确实存在有开挂脚本1、点击下载安装,乐吧游戏脚本...
2027版开挂!威信茶馆开挂多... 2027版开挂!威信茶馆开挂多功能AI插件,威信茶馆果然有开挂插件1、许多玩家不知道威信茶馆辅助怎么...
2027版开挂!鸿运会开挂多功... 2027版开挂!鸿运会开挂多功能AI方法,鸿运会切实有开挂方法1、全新机制【鸿运会ai辅助工具激活码...
2027版辅助!微乐常州麻将辅... 2027版辅助!微乐常州麻将辅助多功能AI方法,微乐常州麻将确实真的是有辅助方法1、上手简单,内置详...
2027版透视挂!呼伦贝尔麻将... 2027版透视挂!呼伦贝尔麻将透视挂多功能AI软件,呼伦贝尔麻将果然有透视挂软件1、下载好呼伦贝尔麻...
2027版开挂!二丫麻将开挂多... 2027版开挂!二丫麻将开挂多功能AI技巧,二丫麻将都是真的有开挂技巧1、完成二丫麻将辅助器v3.3...
2027版辅助器!雀神麻将辅助... 2027版辅助器!雀神麻将辅助器多功能AI工具,雀神麻将都是有辅助器工具1、雀神麻将辅助器安装包、雀...