xxxxxxxxxx
34
let img;
let queries = ["moon", "sun", "earth", "jupiter", "saturn", "venus", "mars"];
function setup() {
createCanvas(windowWidth, windowHeight);
textAlign(CENTER, CENTER);
textSize(width/20);
}
function draw() {
background(220);
if(img){
image(img, 0, 0, width, height);
} else {
text('Click to get an image', width/2, height/2);
}
}
async function getImage() {
let query = queries[floor(random(queries.length))];
let url = "https://images-api.nasa.gov/search?q=" + query;
let response = await fetch(url);
let data = await response.json();
let images = data.collection.items;
let randy = Math.round(Math.random() * images.length);
img = loadImage(images[randy].links[0].href);
}
function mousePressed() {
getImage();
}