xxxxxxxxxx
62
// This p5.js program generates an array of nonsense "poems",
// and then exports these poems as objects in a JSON file.
// Golan Levin, November 2018
var myPoemsArray = [];
// A "Poem" object consists of a title and a body text
class Poem {
constructor(title, text) {
this.title = title;
this.text = text;
}
}
function setup() {
createCanvas(300, 100);
background(200);
var words1 = ["Foamy", "Fancy", "Funky", "Freaky", "Flappy", "Forky"];
var words2 = ["Bagel", "Booger", "Bunting", "Bumper", "Banter", "Blip"];
var vowels = "aeiou";
var consonants = "bcdfgjklmnprstvxz";
// Generate some random-ass poems.
var nPoems = 16;
var nIters = 20;
for (var i = 0; i < nPoems; i++) {
var aTitle = ""; // Generate the title from two random words.
aTitle += words1 [floor(random(6))] + " ";
aTitle += words2 [floor(random(6))];
var aText = ""; // Generate the text from random characters.
for (var j = 0; j < nIters; j++) {
aText += consonants.charAt(floor(random() * consonants.length));
if (j == 0) {
aText = aText.toUpperCase();
}
aText += vowels.charAt(floor(random() * vowels.length));
if (j == (nIters - 1)) {
aText += ".";
} else if (random() < 0.30) {
aText += " ";
}
}
// Construct the i'th Poem object, and add it to the Poem array.
var aPoem = new Poem(aTitle, aText);
myPoemsArray[i] = aPoem;
}
// Create a JSON Object, fill it with the Poems.
var myJsonObject = {};
myJsonObject.poems = myPoemsArray;
// Make a button. When you press it, it will save the JSON.
createButton('SAVE POEMS BUTTON')
.position(10, 10)
.mousePressed(function() {
saveJSON(myJsonObject, 'myPoems.json');
});
}