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

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

    Maybe coming to this party a bit late, but I found the following tutorial for wrapping text on a canvas perfect.

    http://www.html5canvastutorials.com/tutorials/html5-canvas-wrap-text-tutorial/

    From that I was able to think get multi lines working (sorry Ramirez, yours didn't work for me!). My complete code to wrap text in a canvas is as follows:

    
    

    Where c is the ID of my canvas and text is the ID of my textbox.

    As you can probably see am using a non-standard font. You can use @font-face as long as you have used the font on some text PRIOR to manipulating the canvas - otherwise the canvas won't pick up the font.

    Hope this helps someone.

提交回复
热议问题