Hi
I have a string of text that contains linebreaks (\n
). I want to make one word at a time appear every time I press the mouse. I can do that, however, I have an issue maintaining the linebreaks as they seem to get lost when I split the string. How might I do that?
Here is the sketch.js code
let writtenText = "Lorem ipsum. \ndolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \nUt enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
let myWords = [];
let wordIndex = 0;
let displayText = "";
function setup() {
createCanvas(400, 400);
myWords = trim(writtenText.split(" "));
}
function draw() {
background(220);
}
function mousePressed() {
document.getElementById("poem").innerHTML+=myWords[wordIndex] + " ";
wordIndex++;
if (wordIndex > myWords.length - 1) wordIndex = 0;
}
And here is a link to the sketch:
https://editor.p5js.org/AndreasRef/sketches/KzmoLzlbT