收藏
回答

一个canvas的问题?

play_music:function(e){

        this.setData({'globalData.song_title':e.currentTarget.dataset.song_title})
        this.setData({'globalData.singer_name':e.currentTarget.dataset.singer_name})
        this.setData({'globalData.song_img':e.currentTarget.dataset.song_img})
 
        app.play_music(voice_url);
 
        var _that = this;


        wx.createSelectorQuery().select('#canvas').fields({ nodetruesizetrue }).exec((res) => {
            // Canvas 对象
            // console.log(res)
            
            const canvas = res[0].node
            _that.canvas = canvas
            // Canvas 画布的实际绘制宽高
            const width = res[0].width
            const height = res[0].height
    
            // 创建canvas渲染上下文
            const ctx = canvas.getContext('2d')
            const dpr = wx.getWindowInfo().pixelRatio
            // console.log('---dpr', dpr)
            // 手动改变canvas的宽和高
            canvas.width = width * dpr
            canvas.height = height * dpr
            ctx.scale(dpr, dpr)
            // 以上代码都是基础工作,给canvas写css样式时可以使用rpx单位。


            ctx.beginPath();
              ctx.strokeStyle = 'white';
              ctx.lineWidth = _that.rpx2px(3);
              ctx.arc(_that.rpx2px(25),_that.rpx2px(25),_that.rpx2px(23),0,1*Math.PI,false);
              ctx.stroke();


          })
        

    }


我在一个函数里,先用了this.setData,然后在canvas绘图,但是canvas画不出来。然后我把函数里的代码,原样放在onready方法里,canvas可以画出圆弧。

如果我把这个函数里的this.setData语句去掉,canvas也是能绘图的。

有大神知道什么原因吗

最后一次编辑于  2023-07-20
回答关注问题邀请回答
收藏

2 个回答

  • momo
    momo
    2023-07-20

    ctrl+A全选看见是颜色问题吗,文本和背景色一样?

    2023-07-20
    有用
    回复 1
    • C.K.
      C.K.
      发表于移动端
      2023-07-20
      可能是吧,我从微信工具里复制过来的,有的代码看不见了
      2023-07-20
      回复
  • C.K.
    C.K.
    2023-07-20

    代码里有些字符看不见,可以ctrl+A全选看见

    2023-07-20
    有用
    回复
登录 后发表内容