위챗 애플릿 canvas 문자 자동 줄 바꾸기

3852 단어
Page({

    drawCanvas: function(ctx) {//  
        ctx.setFontSize(14)
        ctx.setFillStyle('#9E7240')
        ctx.textAlign="center";
        const txHash = this.data.info.txHash ? this.data.info.txHash : ' '
        this.drawText(ctx,txHash,187, 517 , 232,232) //  
       
        ctx.draw()
        ctx.save()
    },//  
    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 += 16; //16 
                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
    },

    
})

좋은 웹페이지 즐겨찾기