xxxxxxxxxx
64
let quotes = []; //initializing the array quote
let printing = false; //control the printing of the text
//loading of text file, font and imgs
function preload() {
quotes = loadStrings("quotes.txt");
myFont = loadFont("Amazon Ember Bold Italic.ttf");
bg = loadImage("aura.png");
}
//setup function
function setup() {
createCanvas(500, 450);
fill(255);
textWrap(WORD);
textFont(myFont);
textAlign(CENTER);
//in case the array quote is null, an error msg will be printed
if (quotes == null) {
print("Oops! Sorry something is wrong");
while (1) {}
}
//function to start printing quotes
start();
}
function start() {
if (printing == false) {
printing = true;
typeWriter(random(quotes), 0, 50, 190, 50); //calling a random quote from the "quotes" array
}
}
//new quote each time you click
function mouseClicked() {
start();
}
//what this work is really about: this function is responsible of this typewriting effect
function typeWriter(f, n, x, y, s) {
if (n < f.length) {
background(bg);
textSize(25);
text(f.substring(0, n + 1) + "_", x, y, 400, 250);
n++;
//delay the next character's appearance by s milliseconds
setTimeout(function () {
typeWriter(f, n, x, y, s);
}, s);
} else {
printing = false;
}
textSize(10);
text("click for your quote", 250, 20);
}
// to save your quote, click on key "w"
function keyTyped() {
if (key == 'w') {
saveCanvas('photo', 'png');
}
}