xxxxxxxxxx
196
// Array of words
let words = [];
// Word count
let word_count = {};
//Array of abstracts
let abstracts = [];
let abstractsFiltered = [];
//nytimes API stuff
let urlStart = "https://api.nytimes.com/svc/search/v2/articlesearch.json?"
let q = "&q=climate%20change"
let apikey = "&api-key=6ufuxcvEFIqFKn4IIpHuvmG38dcjdriv";
let url = urlStart + q + apikey;
//keep words
let redWords = [];
let blackWords = [];
let fontSize;
let word;
function setup() {
background('white');
createCanvas(windowWidth, windowHeight);
textAlign(CENTER, CENTER);
//load API data
loadJSON(url, gotData);
background(45);
}
function draw() {
if (frameCount % 30 == 1) {
for (let word in word_count) {
textSize(word_count[word] * 30);
fontSize = word_count[word] * 30;
// console.log(word);
if (fontSize > 40) {
redWords.push(new redClimateText(word, random(10, width - 10), random(10, height - 10), fontSize, 255, 0, 0, 255));
} else {
blackWords.push(new blackClimateText(word, random(10, width - 10), random(10, height - 10), fontSize, 0, 0, 0, 255));
}
}
}
// redWords.display();
//console.log(redWords.length);
if(redWords.length > 0){
for(let rw = 0; rw < redWords.length; rw++){
redWords[rw].display();
// console.log(redWords[rw]);
}
}
if(blackWords.length > 0){
for(let bw = 0; bw < blackWords.length; bw++){
blackWords[bw].blackTextFade();
blackWords[bw].display();
}
}
// console.log(redWords);
//console.log(blackWords);
// noLoop();
}
function gotData(data) {
let articles = data.response.docs;
for (let i = 0; i < articles.length; i++) {
// createElement('h2', articles[i].headline.main);
// createElement('h3', articles[i].headline.kicker);
// // createElement('p',articles[i].snippet);
// createElement('p', articles[i].abstract);
abstracts.push(articles[i].abstract);
}
for (let d of abstracts) {
// Turn each line into an array of words
let line = splitTokens(d);
// Add it to 1 big array
words = words.concat(line);
}
// Clean up all the words
for (let w in words) {
let word = words[w];
// Remove punctuation
word = word.replace(/[\-_:;.,!?\"\(\)]/g, "");
// Make it all lowercase
word = word.toLowerCase();
// Get rid of whitespace around the word
word = word.trim();
// If nothing is left, get rid of it
if (word.length < 1) words.splice(w, 1);
// Otherwise put cleaned up word back in array
else words[w] = word;
}
for (let i = 0, l = words.length, w; i < l; i++) {
w = words[i]
if (w.length > 6)
abstractsFiltered.push(w)
}
//console.log(abstractsFiltered);
// Index the words
for (let word of abstractsFiltered) {
if (word in word_count) word_count[word]++;
else word_count[word] = 1;
}
// console.log(data.response.docs[1].headline.main);
}
class redClimateText {
constructor(word, textX, textY, size, textColorR, textColorG, textColorB, textColorA) {
this.word = word;
this.textX = textX;
this.textY = textY;
this.size = size;
this.textColorR = textColorR;
this.textColorG = textColorG;
this.textColorB = textColorB;
this.textColorA = textColorA;
}
display() {
fill(this.textColorR, this.textColorG, this.textColorB, this.textColorA);
textSize(this.size);
text(this.word, this.textX, this.textY);
}
}
class blackClimateText {
constructor(word, textX, textY, size, textColorR, textColorG, textColorB, textColorA) {
this.word = word;
this.textX = textX;
this.textY = textY;
this.size = size;
this.textColorR = textColorR;
this.textColorG = textColorG;
this.textColorB = textColorB;
this.textColorA = textColorA;
}
display() {
fill(this.textColorR, this.textColorG, this.textColorB, this.textColorA);
textSize(this.size);
text(this.word, this.textX, this.textY);
}
blackTextFade() {
this.textColorA--;
// console.log(this.textColorA);
}
}