WeChat Mini Programs custom components, canvas components have no effect

the custom component in WeChat Mini Programs, but the canvas component has no effect. Has anyone ever encountered this problem?

  ready(){
    const ctx = wx.createCanvasContext("back-canvas")
    ctx.setFillStyle("red")
    ctx.fillRect(10, 10, 150, 75)
    ctx.draw();
  },
<canvas class="ca" canvas-id="back-canvas" style="width:200px;height:200px;"></canvas>

createCanvasContext has two parameters. You need to pass this in a This, component in the page page by default.

const ctx = wx.createCanvasContext ('myCanvas',this);


just write it in the onLoad function or onShow function


the second parameter, just add this.
const ctx = wx.createCanvasContext ('back-canvas',this)


clipboard.png


the width and height of canvas does not need to be written in style. Try


I haven't found any reason either. In the form of page, you can


Note: ID selector, property selector, and signature selector should not be used in component wxss.
Is this the reason for

?


have you solved it?


what is the reason why canvas is not displayed on the real machine? Open debugging and see

Menu