· 6 years ago · Jan 13, 2020, 09:00 PM
1/*
2Einführung JSON
3Zugriff auf API
4Auslesen von Werten
5Current
6https://api.weatherstack.com/current?access_key=2580abcd35aaf3945f989be3f68aa54c&query=Rotkreuz
7Forecast
8https://api.weatherstack.com/forecast?access_key=2580abcd35aaf3945f989be3f68aa54c&query=New York&forecast_days=7
9
10Dokumentation
11https://weatherstack.com/documentation
12 */
13
14let key='2580abcd35aaf3945f989be3f68aa54c'; // digital ideation key. persönlicher Gebrauch! 3 Monate gültig
15
16//for the sun function
17let angleA = 4;
18let speedA = 3;
19let speedB = 0;
20let radius = 10;
21let angle = 0;
22
23//for the rain function
24let swarm = [];
25let run = [];
26let w=0;
27let amount = 590;
28
29
30let tempNoiseX = 25;
31let tempNoiseY = 2;
32let tempTilesNumber = 25;
33let spining = 0;
34
35let winddir;
36let temperature = 0;
37
38let tempState = 1;
39let cloudcover;
40let rainIntensity;
41let sunnyweather;
42
43let dataLoaded = false;
44
45function preload(){
46 let url = 'https://api.weatherstack.com/current?access_key='+key+'&query=Rotkreuz';
47 loadJSON(url, gotWeather);
48}
49
50function setup() {
51 createCanvas(700, 700);
52//background(random(150,200));
53background(178,178,220,100)
54drawTemperature(tempNoiseX, tempNoiseY, tempTilesNumber, getColor(temperature));
55//drawTemperature(tempNoiseX, tempNoiseY, tempTilesNumber, getColor(30));
56
57//for the rain
58for (let i = 0; i < amount; i++) {
59 swarm[i] = random(0, 700);
60 run[i] = random(4,6);
61 }
62
63
64}
65
66function draw() {
67 //background(random(150,200));
68 //drawTemperature(tempNoiseX, tempNoiseY, tempTilesNumber, getColor(temperature));
69
70 if (!dataLoaded)
71 return;
72
73
74 if (rainIntensity > 0.2)
75 {
76 drawTemperature(tempNoiseX, tempNoiseY, tempTilesNumber, getColor(temperature));
77 getRainIntensity()
78 }
79
80 if (weather_descriptions.includes("Sunny"))
81 {
82 sunnyWeather()
83 }
84
85 if (weather_descriptions.includes("Snow"))
86 {
87 drawTemperature(tempNoiseX, tempNoiseY, tempTilesNumber, getColor(temperature));
88 getSnow()
89 }
90
91 if (weather_descriptions.includes("is_day:"no""))
92 {
93 isDay()
94 }
95
96 drawWind(getWindDir(winddir));
97 //drawWind(getWindDir("E"));
98
99 drawCloud({x : width / 2, y : height / 2});
100
101}
102
103function getWindDir(winddir)
104{
105 let direction = {x : 0, y : 0};
106 if (winddir.includes("N"))
107 {
108 direction.x = width/2;
109 direction.y = 40;
110 }
111 else if (winddir.includes("S"))
112 {
113 direction.x = width/2;
114 direction.y = 660;
115 }
116
117 if (winddir.includes("W"))
118 {
119 direction.x = 40;
120 direction.y = 350;
121 }
122 else if (winddir.includes("E"))
123 {
124 direction.y = width/2;
125 direction.x = 660;
126 }
127 return direction;
128}
129
130/*
131Return an array corresponding to the values rgb depending on the specified temperature
132*/
133function getColor(temperature)
134{
135 return temperature > 30 ? [201, 46, 98] :
136 temperature > 25 ? [65, 167, 166] :
137 temperature > 20 ? [253, 188,180] :
138 temperature > 15 ? [162, 143, 151] :
139 temperature > 10 ? [236, 232, 228] :
140 temperature > 5 ? [150, 200, 217] :
141 temperature > 0 ? [139, 139, 175] :
142
143 [139, 139, 175];
144
145 // if (temperature > 30)
146 // {
147 // return [255, 17, 0];
148 // }
149 // else
150 // {
151 // if (temperature > 20)
152 // {
153 // return [255, 98, 0];
154 // }
155 // else
156 // {
157 // if (temperature > 10)
158 // {
159 // return [219, 186, 75];
160 // }
161 // else {
162 // return [95, 102, 237];
163 // }
164 // }
165 // }
166}
167
168function drawTemperature(noiseX, noiseY, tiles, color) {
169 ellipseMode(CENTER);
170
171 let xOff = noiseX;
172 let yOff = noiseY;
173 //Deux dimensions avec deux arguments
174 for(let j=0.5; j<tiles;j++){
175 for(let i=0.5; i<tiles;i++){
176 let colorNoise = noise(yOff, xOff)*100;
177 fill(color[0] + colorNoise, color[1] + colorNoise, color[2] + colorNoise);
178
179 //color[3] + colorNoise), color[4] + colorNoise, color[5] + colorNoise,
180 // color[6] + colorNoise, color[7] + colorNoise;
181 noStroke();
182
183 rect(i*width/tiles,j*height/tiles,width/tiles, height/tiles);
184 //ellipse(i*width/10,j*height/30,width/tiles, height/0.2);
185
186 ellipse(i*width/tiles,j*height/tiles,width/tiles, height/tiles);
187
188 fill(color[0] + colorNoise-10, color[1] + colorNoise-10, color[2] + colorNoise)
189 //fill(255,200);
190 ellipse(i*width/tiles,j*height/tiles,(width/tiles)/1.5, (height/tiles)/1.5);
191
192 fill(color[0] + colorNoise-30, color[1] + colorNoise-30, color[2] + colorNoise)
193
194 ellipse(i*width/tiles,j*height/tiles,(width/tiles)/3, (height/tiles)/3);
195
196 xOff+=0.6;
197
198
199 }
200 }
201
202
203
204};
205
206function drawWind(direction)
207{
208 let center = {x : 0, y : 0};
209 //let length = 200;
210 //strokeWeight(10);
211 //stroke(206, 27, 27);
212 //line (center.x, center.y, center.x + direction.x * length, center.y + direction.y * length);
213noStroke();
214 fill(255);
215 //rect (center.x + direction.x, center.y + direction.y, 20,20);
216ellipse (center.x + direction.x, center.y + direction.y, 30,30);
217 //drawCloud({x: center.x + direction.x, y: center.y + direction.y}, getRainIntensity(rainIntensity));
218 //* length, center.y + direction.y * length);
219}
220
221/*function getRainIntensity(rainIntensity)
222{
223 let max = 3;
224 return Math.min(rainIntensity, max);
225} */
226
227function getRainIntensity(rainIntensity)
228{
229 push();
230translate(width/2-85,height/2-20)
231 randomSeed(300);
232 for (let i = 0; i < swarm.length; i++) {
233 let x = random(0, width-450);
234
235 fill(random(20,255),110,random(100,230));
236 circle(x, swarm[i], random(1,6));
237 swarm[i] += run[i];
238 if (swarm[i] - w > height+100) {
239 swarm[i] = - w;
240
241 }
242 }
243 pop();
244 }
245
246
247 function getSnow()
248 {
249
250push();
251 randomSeed(300);
252 for (let i = 0; i < swarm.length; i++) {
253 let x = random(0, width);
254
255 fill(255);
256 circle(x, swarm[i], random(1,6));
257 swarm[i] += run[i];
258 if (swarm[i] - w > height+100) {
259 swarm[i] = - w;
260
261 }
262 }
263pop();
264 }
265
266
267function sunnyWeather()
268{
269 let a = map(sin(radians(angle)), -10, 1, 0, 255);
270let Ay = radius+100 * sin(radians(angleA)+10);
271 let Ax = radius-200 * cos(radians(angleA)+10);
272
273push();
274 translate(width/2, height/2);
275
276 // Point
277
278 fill(255,247,123,100);
279 angleA += speedA;
280
281 //rotate(PI / 3.5);
282 rotate(angleA);
283
284 stroke(255);
285 strokeWeight(2);
286 //ellipse (Ax, Ay, Ax, Ay);
287// triangle(Ax, 30, Ay, Ax, 6, Ax-10);
288 triangle(Ax, Ay, 30, 10, Ax+20, Ay+20);
289
290 fill(253,188,250,100);
291 stroke(150,200,217);
292 strokeWeight(2);
293 //ellipse (Ax, Ay, Ax, Ay);
294// triangle(Ax, 30, Ay, Ax, 6, Ax-10);
295 triangle(Ax-10, Ay-10, 30, 10, Ax-30, Ay-30);
296
297fill(256,219,184,100);
298 rect(0,0,0.3,50);
299pop();
300 }
301
302 function isDay()
303 {
304 //Black above the canva
305 fill(0,150);
306 noStroke();
307rect(0, 0, 700, 700);
308
309 for (let a = random(700); a < width; a += 700) {
310 for (let b = random(700); b < height; b += 700) {
311
312 // stars
313
314fill(random(255,100));
315 ellipse(a, b, (random(5)), 5);
316
317}
318}
319 }
320
321
322
323function gotWeather(weather) {
324 console.log(weather);
325 winddir=weather.current.wind_dir;
326 temperature=weather.current.temperature;
327 cloudcover=weather.current.cloudcover;
328 rainIntensity = weather.current.precip;
329 dataLoaded = true;
330 weather_descriptions = weather.current.weather_descriptions[0].split(" ");
331
332}