-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch.js
68 lines (49 loc) · 1.41 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const Engine = Matter.Engine;
const World= Matter.World;
const Bodies = Matter.Bodies;
const Constraint = Matter.Constraint;
var engine, world;
var backgroundImg;
var hour;
var bg = "sunrise.png";
function preload() {
getBackgroundImg();
}
function setup(){
var canvas = createCanvas(1200,700);
engine = Engine.create();
world = engine.world;
}
function draw(){
if(backgroundImg)
background(backgroundImg);
Engine.update(engine);
fill("black");
textSize(30);
if(hour>=12){
text("Time : "+ hour%12 + " PM", 50,100);
}else if(hour==0){
text("Time : 12 AM",100,100);
}else{
text("Time : "+ hour%12 + " AM", 50,100);
}
}
async function getBackgroundImg(){
// write code to fetch time from API
var response = await fetch("http:https://worldtimeapi.org/api/timezone/Asia/Kolkata");
//change the data in JSON format and store it in variable responseJSON
var responseJSON = await response.json();
console.log(responseJSON);
//fetch datetime from responseJSON
var datetime = responseJSON.datetime;
// slice the datetime to extract hour
var hour = datetime.slice(11,13);
console.log(hour);
if(hour>=0 && hour<18 ){
bg = "sunrise.png";
}
else{
bg="sunset.png"
}
backgroundImg = loadImage(bg);
}