xxxxxxxxxx
56
// Get your own API Key @http://developer.nytimes.com
let allWords = [];
let ts = 16;
let i = 0;
function preload() {
let q = "trump";
let apikey = "defd39c05bfd4c6bbe5070dd58e62b6b";
let url = "https://api.nytimes.com/svc/search/v2/articlesearch.json?q=" + q + "&api-key=" + apikey;
loadJSON(url, processSnippets); //load data url
}
function setup() {
createCanvas(800, 800);
fill(0);
}
function draw() {
background(255, 5); //Set background with low opacity so the words fade away on the canvas
ts++; //Text size increases
ts %= 48; //Text size increases until 48pt then it starts from 1pt
if (allWords.length > 0) { //Make sure there is something in the allWords array
i += 1;
i %= allWords.length; //Index number for the allWords array increases by 1 until the last object in the array then it starts from 1
textSize(ts); //Set the text size to be 16pt
let word = allWords[floor(i)];
text(allWords[floor(i)], random(width), random(height));
}
}
function processSnippets(data) {
let docs = data.response.docs; //store the docs into an array
console.log(data);
let putins = ["Putin", "Vladi", "Vlad", "Vova"];
let trumps = ["Trump", "president", "President"];
for (let doc of docs) {
let words = splitTokens(doc.snippet); //split the words in snippet and store them into the words array
for (let w in words) {
let word = words[w];
for (let trump of trumps) {
if (match(word, trump)) { //check if the objects in trumps array match the word objects
words[w] = putins[floor(random(putins.length))]; //switch matched word objects to random objects in putins array
break; //break the matching loop
}
}
shuffle(words, true); //shuffle the words in the words array so that the order is random
}
allWords = concat(allWords, words); //concatenate the words array into allWords array
}
}