HTML5 canvas ctx.fillText won't do line breaks?

前端 未结 17 1058
甜味超标
甜味超标 2020-11-28 01:37

I can\'t seem to be able to add text to a canvas if the text includes \"\\n\". I mean, the line breaks do not show/work.

ctxPaint.fillText(\"s  ome \\n \\\\n         


        
17条回答
  •  一向
    一向 (楼主)
    2020-11-28 02:14

    If you only need two lines of text, you can split them into two different fillText calls and give each one a different baseline.

    ctx.textBaseline="bottom";
    ctx.fillText("First line", x-position, y-position);
    ctx.textBaseline="top";
    ctx.fillText("Second line", x-position, y-position);
    

提交回复
热议问题