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

前端 未结 17 1087
甜味超标
甜味超标 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 happened across this due to having the same problem. I'm working with variable font size, so this takes that into account:

    var texts=($(this).find('.noteContent').html()).split("
    "); for (var k in texts) { ctx.fillText(texts[k], left, (top+((parseInt(ctx.font)+2)*k))); }

    where .noteContent is the contenteditable div the user edited (this is nested in a jQuery each function), and ctx.font is "14px Arial" (notice that the pixel size comes first)

提交回复
热议问题