xxxxxxxxxx
70
//Created on Date: 10/2/2022
//This code shows the implementation of the use of text functions to come up with generative text output. In addition to this, in this code I read data from a csv file and based on this data I was able to produce the output that you see on the screen
//I saved some variables as constants to be able to access them more easily from the csv file
const textWord=0;
const wordSize=1;
const colorR=2;
const colorG=3;
const colorB=4;
let words=[];
//I added preload function to easily load the csv file when running the program
function preload(){
words=loadStrings("words.csv")
}
function setup() {
createCanvas(400, 400);
background(240)
//I created a rectange with press here word to make the user interact with the drawing
rect(300,50,50)
text("press here",295,40)
}
let singleRow;
let i=1
let j=25
function draw() {
//split the csv file into separate lines and store each word as an element in singleRow array
singleRow=split(words[int(random(1,words.length))],",");
//translate the canvas by 10 each time
translate(j,j)
j+=10;
push()
//rotate around the new translated canvas to place the words are the canvas
rotate(radians(360*(i/4)))
//access different elements in the string array
let word1=singleRow[textWord]
let wordRcolor=singleRow[colorR]
let wordGcolor=singleRow[colorG]
let wordBcolor=singleRow[colorB]
singleRow=split(words[int(random(1,words.length))],",");
let word1size=int(singleRow[wordSize])
//add a random size to each word
print(word1size)
textFont("Courier New",word1size);
//fill positive words with green color and negative words with red color
fill(wordRcolor,wordGcolor,wordBcolor)
//add the text
text(word1,20,20)
pop()
i+=1
noLoop()
}
//do not loop unless mouse is inside the square
function mousePressed(){
if(mouseX>=300 && mouseX<=350 && mouseY>=50 &&mouseY<=100){
loop()
}
}