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

前端 未结 17 1062
甜味超标
甜味超标 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:28

    I'm afraid it is a limitation of Canvas' fillText. There is no multi-line support. Whats worse, there's no built-in way to measure line height, only width, making doing it yourself even harder!

    A lot of people have written their own multi-line support, perhaps the most notable project that has is Mozilla Skywriter.

    The gist of what you'll need to do is multiple fillText calls while adding the height of the text to the y value each time. (measuring the width of M is what the skywriter people do to approximate text, I believe.)

提交回复
热议问题