How do you add a line break inside of a javascript variable using KineticJS?

只谈情不闲聊 提交于 2019-12-12 11:34:06

问题


I have a variable that looks like this:

var simpleText = new Kinetic.Text({
    text: 'where text goes',
    fontSize: 24
  });

How do I insert a line break where the text goes. I've tried:

text: 'where text goes \n where text goes',

text: 'where text goes \n\n where text goes',

text: 'where text goes \r\n where text goes',

text: 'where text goes <br /> where text goes',

text: 'where text goes \n' + 'where text goes \n',

text: 'where text goes ' + 'where text goes ',

text: 'where text goes ' 
+ 'where text goes ',

text: 'where text goes 
where text goes',

Error on my end. \n works


回答1:


If you are outputting to HTML, such as with innerHTML, just use <br />.

If, however, you are outputting to text in an HTML context (such as nodeValue or innerText, you will have to use \n and ensure that the container has the CSS white-space property set to something that doesn't condence whitespace, such as pre-wrap.




回答2:


I've never heard of Kinetic text but based on this sample: http://www.html5canvastutorials.com/kineticjs/html5-canvas-kineticjs-text-tutorial/ you should do:

text: 'where text goes\n\nwhere text goes',



来源:https://stackoverflow.com/questions/16595483/how-do-you-add-a-line-break-inside-of-a-javascript-variable-using-kineticjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!