wxml:


javascript:
onReady: function () {
    var that = this
        setTimeout(() => {
        this.drawCanvas(that)
    },500)
}
drawCanvas: function (that) {
        var context = wx.createCanvasContext('firstCanvas')

        context.setFillStyle('#F2F6FC')
        context.fillRect(0, 0, 750, 600)

        context.setFillStyle('#303133')
        context.setTextBaseline('middle')

        context.setFontSize(24)
        that.drawText(context, '锡盟信息万事通', 20, 30, 330, 330)

        context.setFontSize(18)
        that.drawText(context, that.data.content, 20, 70, 330, 330)

        context.draw()
}
 //文本换行 参数:1、canvas对象,2、文本 3、距离左侧的距离 4、距离顶部的距离 5、6、文本的宽度
 drawText: function (ctx, str, leftWidth, initHeight, titleHeight, canvasWidth) {
        var lineWidth = 0;
        var lastSubStrIndex = 0; //每次开始截取的字符串的索引
        for (let i = 0; i < str.length; i++) {
            lineWidth += ctx.measureText(str[i]).width;
            if (lineWidth > canvasWidth) {
                ctx.fillText(str.substring(lastSubStrIndex, i), leftWidth, initHeight); //绘制截取部分
                initHeight += 22; //22为字体的高度
                lineWidth = 0;
                lastSubStrIndex = i;
                titleHeight += 30;
            }
            if (i == str.length - 1) { //绘制剩余部分
                ctx.fillText(str.substring(lastSubStrIndex, i + 1), leftWidth, initHeight);
            }
        }
        // 标题border-bottom 线距顶部距离
        titleHeight = titleHeight + 10;
        return titleHeight
    }