收藏
回答

canvas 调用,wx.createSelectorQuery(),获到不到节点?

async onReady() {

    // await this.initCanvas();


    await this.initCanvasContext();

  },


  async initCanvasContext() {

    try {

      const query = this.createSelectorQuery(); // .in(this)

      console.log("query",query)

      const res = await query.select('#screenshotCanvas')

        .fields({

          node: true,

          size: true

        })

        .exec();


      // 空值校验

      if (!res || res.length === 0 || !res[0].node{

        console.log("res--", res)

        throw new Error('Canvas 节点未找到');

      }


      // 获取 Canvas 实例

      const canvas = res[0].node;

      const ctx = canvas.getContext('2d');


      // 分辨率适配

      const dpr = wx.getSystemInfoSync().pixelRatio;

      canvas.width = res[0].width * dpr;

      canvas.height = res[0].height * dpr;

      ctx.scale(dpr, dpr);


      console.log("Canvas 初始化完成");

      this.setData({

        canvas

      })

    } catch (err{

      console.error("初始化失败:", err);

      wx.showToast({

        title: '初始化失败',

        icon: 'none'

      });

    }

  },





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

3 个回答

  • 社区技术运营专员--Demons
    社区技术运营专员--Demons
    04-09

    请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    04-09
    有用
    回复
  • showms
    showms
    04-09

    是在自定义组件里面调这个api?

    04-09
    有用
    回复
  • 启年
    启年
    04-08

    做个代码片段看看

    04-08
    有用
    回复
登录 后发表内容