Canvas2D在onLoad中设置画布大小无效?
发布于 5 年前 作者 qiangqiao 10381 次浏览 来自 问答

因为需要根据机型动态设置画布大小,发现在onLoad中设置无效,请问这种情况我需要怎么设置呢

wxml

<canvas bind:start="start" id="canvas" graph="{{graph}}" width="{{canvasWidth}}" height="{{canvasheight}}" enableUndo="{{true}}"></canvas>

js

data: {
        canvasWidth: "650",
        canvasheight: "1170",
      },
onLoad: function (options) {
        const sysInfo = wx.getSystemInfoSync();
        const screenHeight = sysInfo.screenHeight

        if (screenHeight < 750) {
            this.setData({
                canvasheight: 900,
                canvasWidth: 500
            })
        } else {
            this.setData({
                canvasheight: 1170,
                canvasWidth: 650
            })
        }
    },
      
      
1 回复

已解决,我是在自定义组件中创建的canvas,在获取上下文时在ready()中执行即可,

回到顶部