xxxxxxxxxx
76
let myPoem;
let currentChar = 0;
let lines = [];
let dataPoems;
let poemPrinted = false;
let magazineFont;
let fancyPrinted = false;
let magazineLine;
function preload() {
myPoem = loadStrings('poem.txt');
dataPoems = loadStrings('poems.txt'); //database of poems
magazineFont = loadFont('Magazine-Cutouts-Font.ttf');
}
function setup() {
createCanvas(600, 600);
//importing font
textFont('Courier New');
lines = myPoem;//perserving the line by line
//version of my poem for random word selection
myPoem = myPoem.join('\n'); //making one big string for
//substring to work
}
function draw() {
if (!poemPrinted) { //
frameRate(12); //looks like typing speed
background(250); //reset each time so letters dont get thick
let textHeight = 50;
let currentString = myPoem.substring(0, currentChar);
//adding one leter at a time to printed poem string
text(currentString, 50, textHeight, width, height);
currentChar++; //moving through each char in megastring
if (currentChar > myPoem.length) {
//poem printing complete
//without this, random database poem gets generated
//a bunch of times
poemPrinted = true;
randomWord();
}
} else if (magazineLine !== '') {
background(250);
textFont(magazineFont, 25);
//textAlign(CENTER, CENTER);
text(magazineLine, 50, 250, width-40, height);
}
}
function randomWord() {
let randLine = lines[int(random(0,23))]; //choose a line from my poem
let words = split(randLine, ' '); //make each word
//an element in an array
let chosenWord = words[int(random(0, words.length))];
//choosing a random word from the random line
wordInData(chosenWord); //find it in the data set
}
function wordInData(chosenWord) {
let attempts = 0; //preventing max stack call error
while (attempts < 1000) {
let randomDataLine = dataPoems[int(random(0,dataPoems.length))];
let words = split(randomDataLine, ' ');
//doing the same thing from my poem
if (words.includes(chosenWord)) { //if random word is found
magazineLine = randomDataLine;
return; //stops the function from continue
}
attempts++;
}
// If no match found after maxAttempts, choose a random line
// = random(dataPoems);
randomWord();
}
function mouseClicked(){ //minimal interactivity
if (poemPrinted) { //so it doesnt interrupt first poem
randomWord();
}
}