收藏
回答

离屏2d canvas canvasToTempFilePath报错?

drawCanvas(mapName, fullSteps) {
    // wx.createSelectorQuery().select('#cert').fields({
    //   node: true,
    //   size: true
    // }).exec(res => {
    const dpr = wx.getSystemInfoSync().pixelRatio;
    const widthRatio = wx.getSystemInfoSync().windowWidth / 375;
    const width = wx.getSystemInfoSync().windowWidth * dpr;
    const height = wx.getSystemInfoSync().windowHeight * dpr;
    const canvas = wx.createOffscreenCanvas({
      type: '2d',
      width: wx.getSystemInfoSync().windowWidth * dpr,
      height: wx.getSystemInfoSync().windowHeight * dpr
    });
    const ctx = canvas.getContext('2d');
    ctx.scale(dpr, dpr);
    ctx.scale(widthRatio, widthRatio);
    let bg = canvas.createImage();
    bg.src = '../../../images/cert-bg.jpg';
    bg.onload = () => {
      // 绘制背景图
      ctx.drawImage(bg, 0, 0, canvas.width / dpr, canvas.height / dpr);
      // 头部文字
      ctx.fillStyle = "#777873";
      ctx.font = "14px 'PingFang SC'";
      ctx.fillText('健行四季|悦动人生', 123, 146);
      ctx.font = "16px 'PingFang SC'";
      ctx.fillText('2021年中国人寿“700健行”活动', 73, 163);
      ctx.font = "36px 'PingFang SC'";
      ctx.fillStyle = "#0B3836";
      ctx.fillText('风景线路完成证书', 44, 206);

      // 地图信息文字
      ctx.font = "bold 18px 'PingFang SC'";
      ctx.textAlign = 'center';
      ctx.fillStyle = "#0B3836";
      ctx.fillText(`${mapName}风景线 全程${fullSteps}步`, 375 / 2, 346);

      // 详细信息背景
      ctx.beginPath();
      ctx.moveTo(47, 385);
      ctx.lineTo(47, 547);
      ctx.arc(57, 547, 10, Math.PI, Math.PI / 2, true);
      ctx.lineTo(318, 557);
      ctx.arc(318, 547, 10, Math.PI / 2, 0, true);
      ctx.lineTo(328, 385);
      ctx.arc(318, 385, 10, 0, 1.5 * Math.PI, true);
      ctx.lineTo(57, 375);
      ctx.arc(57, 385, 10, 1.5 * Math.PI, Math.PI, true);
      ctx.fillStyle = "#CDDDDA";
      ctx.fill();
      ctx.closePath();

      // 详细信息
      ctx.fillStyle = "#9AACAB";
      ctx.textAlign = 'left';
      ctx.font = "bold 14px 'PingFang SC'";
      ctx.fillText('完成者:', 64, 402);
      ctx.fillText('完成时间:', 64, 432);
      ctx.fillText('完成时长:', 64, 462);
      ctx.fillStyle = "#316C6A";
      ctx.fillText(wx.getStorageSync('userInfo').nickName, 140, 402);
      ctx.fillText('2021.06.21', 140, 432);
      ctx.fillText('04:36:22', 140, 462);
      ctx.fillText(`11.00 km    ${fullSteps}步`, 64, 502);
      ctx.fillText(`${mapName}风景线`, 64, 532);

      // 完成图片
      const finish = canvas.createImage();
      finish.src = '../../../images/finished.png';
      finish.onload = () => {
        ctx.drawImage(finish, 224, 456, 136, 136);
      }

      // 公司logo
      const logo = canvas.createImage();
      logo.src = '../../../images/chinalife-logo.png';
      logo.onload = () => {
        ctx.drawImage(logo, 108, 94, 160, 28);
      }

      // 用户头像
      const avatar = canvas.createImage();
      avatar.src = wx.getStorageSync('userInfo').avatarUrl;
      avatar.onload = () => {
        ctx.save();
        ctx.arc(188, 276, 36, 0, 2 * Math.PI);
        ctx.clip();
        ctx.drawImage(avatar, 152, 240, 72, 72);
        ctx.restore();
        console.log(canvas);
        wx.canvasToTempFilePath({
          canvas: canvas,
          x: 0,
          y: 0,
          width,
          height,
          destWidth: width,
          destHeight: height,
          success: res => {
            console.log(res);
          }
        }, this)
      }
    }

以上是代码片段,报错信息如下

这里的意思是需要我传入离屏canvas 但是我传入了依旧报错

回答关注问题邀请回答
收藏

2 个回答

  • 武曲心
    武曲心
    2021-05-19

    这个要求2.16.1的,自己检查一下

    2021-05-19
    有用 1
    回复 3
    • Natural Force
      Natural Force
      发表于移动端
      2021-05-19
      我改成了2.16.0也可以 之前是2.17.0一直报错
      2021-05-19
      1
      回复
    • Natural Force
      Natural Force
      2021-05-19
      不对 我现在是把画布渲染后隐藏掉了 貌似离屏的2d不能导出图片
      2021-05-19
      回复
    • 武曲心
      武曲心
      2021-05-20回复Natural Force
      做一个普通的canvas,然后把它用fixed扔到屏幕以外渲染,这个基础库太高,现在用不合适
      2021-05-20
      1
      回复
  • Cjiang
    Cjiang
    2021-05-19

    你好,麻烦提供出现问题的具体机型、微信版本号、系统版本号,基础库版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html

    2021-05-19
    有用
    回复
登录 后发表内容