lottie-miniprogram icon indicating copy to clipboard operation
lottie-miniprogram copied to clipboard

uniapp 微信小程序找不到canvas

Open open-shuaijun opened this issue 1 year ago • 2 comments

this.$nextTick(() => { uni.createSelectorQuery().select('#canvas').node(res => { console.log(res) const canvas = res.node lottie.setup(canvas) }).exec() })

open-shuaijun avatar Oct 29 '24 06:10 open-shuaijun

报什么错?原生小程序代码能否复现

seasonhuang avatar Oct 31 '24 03:10 seasonhuang

代码修正

this.$nextTick(() => {
  // 使用uni.createSelectorQuery()获取canvas节点
  const query = uni.createSelectorQuery().in(this);
  query.select('#canvas')
    .fields({
      node: true,
      size: true
    })
    .exec((res) => {
      console.log(res[0]);
      if (res[0]) {
        const canvas = res[0].node;
        // 初始化canvas
        if (canvas) {
          const ctx = canvas.getContext('2d');
          // 后续canvas操作...
        }
      }
    });
});

canvas元素在template中正确定义,比如:

<canvas type="2d" id="canvas"></canvas>

在使用canvas 2d之前,确保在manifest.json中启用了相关配置:

    "app-plus": {
        "renderer": "native"
    }
}

yangyuan-zhen avatar Feb 22 '25 12:02 yangyuan-zhen