xxxxxxxxxx
126
//create an array of weather effect
var rain = [];
var sun = [];
var nDrops = 800;
//create variables for wind
var wSpeed =0.08;
var angle = 0.0;
var scalar = 8;
var imgWave;
var windspeed;
var humidity;
var temp;
var weather;
var icon;
var angle = 0.0;
var offset = 220;
var scalar = 8; //数量
var speed = 0.04;
let y;
//data variables
var api = ' https://api.openweathermap.org/data/2.5/weather?q=';
var apiKey = '&appid=c3769564cbbc64204e7b3b4bfdd5969a';
var units = '&units=';
var windspeed;
var humidity;
var temp;
var weather;
var icon;
//preload images for display
function preload() {
clouds = loadImage ("weather icons/cloud.png");
sunny = loadImage ("weather icons/sun.png");
}
function setup() {
createCanvas(400,600);
background (0);
// gather data from openweathermap
loadJSON('https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c3769564cbbc64204e7b3b4bfdd5969a', gotData,'jsonp');
var button = select ('#submit');
button.mousePressed(weatherAsk);
input = select ('#city');
// createCanvas(400, 600);
// loadJSON('https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c3769564cbbc64204e7b3b4bfdd5969a', gotData,'jsonp');
// var button = select ('#submit');
// button.mousePressed(weatherAsk);
// input = select ('#city');
//prepare rain effect
for (i = 0 ; i < nDrops; i ++ ) {
rain.push(new Rain(nDrops,wSpeed));
}
}
function weatherAsk () {
var url = api + input.value() + apiKey + units;
loadJSON(url,gotData);
}
function gotData(data) {
print(data);
weather = data;
print('inside gotData')
}
function draw() {
// print('helloworld')
background(0);
rain.forEach(function(d) {
d.drawAndDrop();
});
// //wind speed
// var imgWave = sin(angle)*scalar;
// angle += wSpeed;
// image(clouds,width/4,height*1/12-10+imgWave,width/2,height/2.2);
//wind speed
print(weather)
if (weather) {
print('inside weather')
//set wind speed based on API data
var wSpeed = weather.wind.speed*0.4;
angle += wSpeed;
var imgWave = sin(angle)*scalar;
//detect weather condition, find the information from the first array
var icon = weather.weather[0].main;
if (icon =="Clear") {
print('hhellow')
var weatherShow=image(sunny,width/7,height*1/12-10+imgWave,300,400);
} else if (icon == "Rain") {
rain.forEach(function(d) {
d.drawAndDrop();
});
image(clouds,width/4,height*1/12-10+imgWave,width/2,height/2.2);
} else {
rain.forEach(function(d) {
d.drawAndDrop();
});
}
print('after inside weather')
}
}