uniapp Canvas使用

注意使用canvas 若是网络资源,要用getImageInfo转换成本地路径。

   getImageInfo(src) {
    return new Promise((resolve, reject) => {
      uni.getImageInfo({
        src,
        success: (res) => resolve(res),
        fail: (res) => reject(res),
      });
    });
  }


async img(userImage: string, userNickname: string) {
    if (!this.shareImage) {
      return uni.showToast({ title: "请稍后重试", icon: "error" });
    }

    let that = this;

    //this.getImageInfo将网络资源变成本地地址
    const res1: { [key: string]: any } = await this.getImageInfo(userImage);
    const res2: { [key: string]: any } = await this.getImageInfo(
      this.shareImage
    );
    

    const system = uni.getSystemInfoSync();
    var ctx = uni.createCanvasContext("firstCanvas"); // 使用画布创建上下文 图片

    // 设置图片坐标及大小,括号里面的分别是(图片路径,x坐标,y坐标,width,height)
    ctx.drawImage(
      res2.path,
      0,
      0,
      system.windowWidth * 0.8,
      system.windowHeight * 0.6
    );

    ctx.setFontSize(12); //设置字体大小,默认10

    ctx.setTextAlign("center"); //文本水平居中
    ctx.fillText(userNickname, 95, system.windowHeight * 0.6 - 35); //文字内容、x坐标,y坐标

    ctx.save(); // 对当前区域保存
    ctx.beginPath(); // 开始新的区域
    
    //画x轴圆心点 画y轴圆心点 画圆的半径
    ctx.arc(40, system.windowHeight * 0.6 - 40, 20, 0, Math.PI * 2, false);

    ctx.clip(); // 从画布上裁剪出这个圆形
    ctx.drawImage(res1.path, 20, system.windowHeight * 0.6 - 60, 40, 40);
    ctx.restore(); // 恢复
    ctx.save(); //保存
    ctx.draw(); //绘制

  }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值