xxxxxxxxxx
89
const icons = {
sun: "☀️",
moon: "🌑",
rain: "💧",
wind: "💨",
cloud: "☁️",
};
// API docs: https://docs.breezometer.com/api-documentation/pollen-api/v2/#daily-forecast
// explain the different parts of the URL; how to get the base URL
// https://api.breezometer.com/weather/v1/current-conditions?lat={latitude}&lon={longitude}&key=YOUR_API_KEY
let baseurl = "https://open-meteo.com/en/docs#latitude=40.6501&longitude=-73.9496¤t=temperature_2m,relativehumidity_2m,is_day,precipitation,rain,showers,cloudcover,windspeed_10m,winddirection_10m&hourly=&temperature_unit=fahrenheit&windspeed_unit=ms&precipitation_unit=inch&timezone=America%2FNew_York&forecast_days=1";
let key = "48c3721db6b64e1282f5a96709c872b0";
let lat;
let lon;
let days = 1;
let data;
let windDir;
function preload() {
// get location on preload
/* geolocation is available */
if ("geolocation" in navigator) {
// lat = "49.2827";
// lon = "123.1207";
navigator.geolocation.getCurrentPosition(
(position) => {
lat = position.coords.latitude;
lon = position.coords.longitude;
},
(error) => {
print(error);
},
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0,
}
);
} else {
/* geolocation IS NOT available */
text("Location is not enabled for this site.", 150, 150);
}
}
function setup() {
createCanvas(windowWidth, windowHeight);
if (lat && lon) {
// go key-value pair by pair, debug in network tab
loadJSON(baseurl + `?lat=${lat}&lon=${lon}&key=${key}`, parseData);
}
function parseData(d) {
if (!d) return;
data = d.data;
}
}
function draw() {
if (!data) return;
if (data.is_day_time) {
background('#C4ECFF');
textSize(50);
text(icons.sun, random(0, width), random(0, height/2));
} else {
background('#1C1C1C');
textSize(50);
text(icons.moon, random(0, width), random(0, height/2));
}
for (let i = 0; i < data.cloud_cover; i++) {
textSize(random(40,50));
text(icons.cloud, random(0, width), random(0, height/2));
}
for (let j = 0; j < data.relative_humidity; j++) {
push();
textSize(random(10,15));
translate(random(0, width), random(0, height));
angleMode(DEGREES);
rotate(data.wind.direction);
text(icons.rain, 0, 0);
pop();
}
frameRate(0.5);
}