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

3

6

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

Motorboat answered 16/5, 2013 at 19:8 Comment(5)
It's probably outputting HTML, so use <br /> instead of \n. Otherwise, your first example should work.Purity
Thanks for the suggestion but that doesn't work, it's not outputting html. Also when I use \n it doesn't output any text at all.Motorboat
Are you using this library?: github.com/ericdrowell/KineticJSPurity
Yes I am using that @PurityMotorboat
I have used \n in my work... so the error must be somewhere else. Try to set the breakpoints and dig inParham
O
9

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.

Outworn answered 16/5, 2013 at 19:13 Comment(1)
\n doesn't work nor does <br />. Sorry I forgot to mention I'm using KineticJS.Motorboat
C
0

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',

Collect answered 16/5, 2013 at 19:14 Comment(3)
It doesn't say to use \n\n. It has that for 2 line breaks. Notice the extra line in the output...Purity
Thanks for the suggestion, unfortunately that doesn't work either. I will update my question.Motorboat
Thanks @Purity I did not see that.Collect
P
0

Using html() instead of 'text' and 'val'

.html('using some -br- simple text here');

Result: using some
simple text here

Premises answered 10/3, 2020 at 20:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.