小程序二维码海报生成

准备 canvas 元素

<canvas type="2d" id="canvas" style="witdh: 630rpx; height: 920rpx"></canvas>

生成方法 (注 调用的时候需要通过定时器)

   async createBgImg() {
   
      const _this = this;
      // 创建 SelectorQuery 实例
      const query = wx.createSelectorQuery().in(_this);
      // 获取canvas 节点
      query
        .select('#canvas')
        .fields({
   
          node: true,
          size: true,
        })
        .exec(async function (res) {
   
          const canvas = res[0].node;
          // 用于保存二维码海报
           _this.canvasObj = canvas;
          // 设置 canvas 绘图上下文类型
          const ctx = canvas.getContext('2d');
		  // 设置默认 背景色
          ctx.fillStyle = '#e1e4e6';
          // 创建背景图片 对象 
          const img = canvas.createImage();
          // 生成 主背景 
          // _this.shareBallBit 为主背景图 具体应为 一个图片连接
          const bgImg = await _this.getImageInfo(_this.shareBallBit);
          canvas.width = bgImg.width; //重点 图片模糊的解决方法
          canvas.height = bgImg.height;
          const canvasWidth = bgImg.width; // /重点 图片模糊的解决方法
          const canvasHeight = bgImg.height; //重点 图片模糊的解决方法
          _this.style.width = canvasWidth  // 增加 对部分机型保存canvas到相册 图片会截取的bug
          _this.style.height= canvasHeight 
		  // 小程序 协议支持
          img.src = _this.shareBallBit; //.replace(/^http:/, "https:");
          // 绘图
          await _this.drawCanvasImg(ctx, img, 0, 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值