怎么在自定义组件中使用canvas2d呢 ?一直报错node为空

发布于 8 年前作者 yejuan11034 次浏览最后编辑 8 年前来自 ask
Ready() {
    const query = this.createSelectorQuery()
    query.select('#myCanvas')
      .fields({ node: true, size: true })
      .exec((res) => {
        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)

        ctx.fillRect(0, 0, 100, 100)
      })
1 回复
ming63
ming631 楼6 年前

谢邀:

你wxml里有id为'myCanvas'的canvas组件了吗?设置type为2d了吗