canvas 绘制的文字如何换行?

drawText("Hello, World!What a nice day.",0,30,110);

function drawText(t,x,y,w){
    
    var chr = t.split("");
    var temp = "";                
    var row = [];
    
    context.font = "20px Arial";
    context.fillStyle = "black";
    context.textBaseline = "middle";
    
    for(var a = 0; a < chr.length; a++){
        if( context.measureText(temp).width < w ){
            ;
        }
        else{
            row.push(temp);
            temp = "";
        }
        temp += chr[a];
    }
    
    row.push(temp);
    
    for(var b = 0; b < row.length; b++){
        context.fillText(row[b],x,y+(b+1)*20);
    }
}

转:

https://blog.csdn.net/m8705/article/details/52995099

.

原文地址:https://www.cnblogs.com/xiangsj/p/12691207.html