小程序画布Canvas生成海报,分享、转发

26 篇文章 2 订阅
async share () {
   var that = this;
   wx.showLoading({
     title: '图片生成中...',
   })
   let photo = '' //图片地址
   let result = await that .getCanvas('companyName', 'name', 'position', photo) //生成图片
   wx.hideLoading()
   
 },
getCanvas(companyName, personName, position, photo) {
   return new Promise((resolve, reject) => {
     wx.getImageInfo({ //获取图片信息
       src: photo,
       success: (res) => {
         let pic = res.path
         const ctx = wx.createCanvasContext('shareCanvas')
         // 背景图
         ctx.fillStyle = "#fff";
         ctx.fillRect(0, 0, 320, 120);
         
         // ctx.fillStyle = "#469cf8";
         // ctx.fillRect(0, 20, 4, 20);
         // 标题
         ctx.font = "bold 10px arial";
         ctx.fillStyle = "#469cf8";
         ctx.fillText(companyName || '', 0, 10);
         
         ctx.font = "bold 14px arial";
         ctx.fillStyle = "#222";
         ctx.fillText(personName || '', 54, 40);

         ctx.font = "bold 10px arial";
         ctx.fillStyle = "#A8A8A8";
         ctx.fillText(position || '', 54, 60);
         ctx.drawImage(pic, 0, 24, 50, 50)
         ctx.draw(false, function (){ // 绘制画布回调函数
         //把当前画布指定区域的内容导出生成指定大小的图片。在 draw() 回调里调用该方法才能保证图片导出成功。
           wx.canvasToTempFilePath({ 
             x: 0,
             y: 0,
             canvasId: 'shareCanvas',
             success: (res) => {
               wx.saveFile({ //保存文件到本地
                 tempFilePath: res.tempFilePath,
                 success (res) {
                   resolve(res.savedFilePath)
                 },
                 fail (err) {
                   reject(err)
                 }
               }) 
             }
           });
         })
       }
     })
   })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值