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

 

 

 

.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM