drawKernel(ctx, img_x, windowWidth, imgHeight) {
        var that = this;
        ctx.font = '16px PingFangSC-Semibod, sans-serif';
        ctx.setTextAlign('center');
        ctx.setFillStyle("#fff");
        // ctx.fillText(this.data.newCourseRecommend.title, img_x, imgHeight + 306);

        var str = this.data.newCourseRecommend.title;
        var height = imgHeight + 306;
        var lineWidth = 0;
        var lastIndex = 0; //每次开始截取的字符串的索引

        for (let i = 0; i < str.length; i++) {
            lineWidth += ctx.measureText(str[i]).width;
            if (lineWidth > 320) {
                that.drawFont(ctx, str.substring(lastIndex, i), height, img_x);
                height += that.data.lineHeight;
                lineWidth = 0;
                lastIndex = i;
                that.setData({
                    kernelHeight: height
                })
            }
            if (i == str.length - 1) { //绘制剩余部分
                that.drawFont(ctx, str.substring(lastIndex, i + 1), height, img_x);
            }
        }

    },
   drawFont: function(ctx, content, height, img_x) {
        ctx.fillText(content, img_x, height);
    },

小程序canvas生成海报如果绘制文字过长换行

相关文章:

  • 2021-11-13
  • 2021-12-14
  • 2021-10-13
  • 2021-10-13
  • 2021-07-27
  • 2022-01-19
猜你喜欢
  • 2021-12-09
  • 2021-12-19
  • 2021-11-15
  • 2021-07-11
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
相关资源
相似解决方案