-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanets_script.js
197 lines (174 loc) · 6.86 KB
/
planets_script.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const planet_container=document.getElementById("planet_container");
const planet_name = document.getElementById("planet_name");
const planet_climate = document.getElementById("planet_climate");
const planet_diameter = document.getElementById("planet_diameter");
const planet_orbital_p= document.getElementById("planet_orbital_p");
const planet_rotation_p= document.getElementById("planet_rotation_p");
const planet_population= document.getElementById("planet_pop");
const planet_distance = document.getElementById("planet_distance");
const planet_globe = document.getElementById("planet_globe");
const arrows = document.getElementsByClassName("slick-arrow");
const window_stars = document.getElementById("window");
const red_button = document.getElementById("high_speed_button");
const tableau_de_bord = document.getElementById("tableau_de_bord");
const screen_text = document.getElementById("screen_text");
// Get the root element
var r = document.querySelector(':root');
let planet_id = 1;
function getPlanet(id){
return new Promise((resolve,reject)=>{
const url = 'https://swapi.dev/api/planets/'+id;
const options={
method : 'GET',
headers : {
Accept: 'application/json',
'Content-type' : 'application/json'}
}
fetch(url, options)
.then(reponse => reponse.json())
.then(data=>{
planet_name.textContent=JSON.stringify(data.name).replaceAll('"','');
planet_climate.textContent=JSON.stringify(data.climate).replaceAll('"','');
planet_diameter.textContent=JSON.stringify(data.diameter).replaceAll('"','');
planet_orbital_p.textContent=JSON.stringify(data.orbital_period).replaceAll('"','');
planet_rotation_p.textContent=JSON.stringify(data.rotation_period).replaceAll('"','');
planet_population.textContent=JSON.stringify(data.population).replaceAll('"','');
planet_distance.textContent=randomLightYears();
resolve();
})
.catch(err =>{
console.log("Il y a erreur", err);
screen_text.textContent="Something is wrong Captain...";
})
.catch(err =>console.log("Il y a erreur json", err))
});
}
function randomLightYears(){
return Math.floor(Math.random() * 5000) + 1;
}
//PREVIOUS ARROW
arrows[0].addEventListener("click", function(){
if(planet_id==1){
planet_id=60;
}
else{
planet_id-=1;
}
changePlanet(planet_id);
});
//NEXT ARROW
arrows[1].addEventListener("click", function(){
if(planet_id==60){
planet_id=1;
}
else{
planet_id+=1;
}
changePlanet(planet_id);
});
async function changePlanet(planet_id){
planet_container.classList.add('disappear');
await getPlanet(planet_id);
changePlanetColor(planet_id);
planet_container.classList.remove('disappear');
planet_container.classList.add('appear');
setTimeout(function(){
planet_container.classList.remove('appear');
}, 1500);
}
function changePlanetColor(){
switch(planet_id){
case 1: //TATOOINE
r.style.setProperty('--first_globe_color', '#bd592b');
r.style.setProperty('--second_globe_color', '#b89455');
r.style.setProperty('--third_globe_color', '#db821b');
break;
case 2: //ALDERAAN
r.style.setProperty('--first_globe_color', '#3ec462');
r.style.setProperty('--second_globe_color', '#30abff');
r.style.setProperty('--third_globe_color', '#ffffff');
break;
case 3: //YAVIN IV
r.style.setProperty('--first_globe_color', '#3ec462');
r.style.setProperty('--second_globe_color', '#30abff');
r.style.setProperty('--third_globe_color', '#ffffff');
break;
case 4: // HOTH
r.style.setProperty('--first_globe_color', '#00ffeb');
r.style.setProperty('--second_globe_color', '#30d8ff');
r.style.setProperty('--third_globe_color', '#ffffff');
break;
case 5: // DAGOBAH
r.style.setProperty('--first_globe_color', '#a9c4a3');
r.style.setProperty('--second_globe_color', '#188409');
r.style.setProperty('--third_globe_color', '#f5d3d3');
break;
case 6: // BESPIN
r.style.setProperty('--first_globe_color', '#debb48');
r.style.setProperty('--second_globe_color', '#ffed1e');
r.style.setProperty('--third_globe_color', '#a3691d');
break;
case 7: // ENDOR
r.style.setProperty('--first_globe_color', '#a9c4a3');
r.style.setProperty('--second_globe_color', '#188409');
r.style.setProperty('--third_globe_color', '#f5d3d3');
break;
case 8: // NABOO
r.style.setProperty('--first_globe_color', '#3ec462');
r.style.setProperty('--second_globe_color', '#30abff');
r.style.setProperty('--third_globe_color', '#ffffff');
break;
case 9: // CORUSCANT
r.style.setProperty('--first_globe_color', '#2c240e');
r.style.setProperty('--second_globe_color', '#494232');
r.style.setProperty('--third_globe_color', '#9e6922');
break;
case 10: // KAMINO
r.style.setProperty('--first_globe_color', '#0630d0');
r.style.setProperty('--second_globe_color', '#1881d4');
r.style.setProperty('--third_globe_color', '#0630d0');
break;
default:
r.style.setProperty('--first_globe_color', randomColor());
r.style.setProperty('--second_globe_color', randomColor());
r.style.setProperty('--third_globe_color', randomColor());
break;
}
}
function randomColor(){
let color = '#';
for (let i = 0; i < 6; i++){
let random = Math.random();
let bit = (random * 16) | 0;
color += (bit).toString(16);
};
return color;
};
function letsHyperdrive() {
setTimeout(function(){
location.reload();
}, 6000);
tableau_de_bord.classList.add('disappear');
document.querySelector("h1").classList.add('disappear');
document.querySelector("body").style.setProperty('overflow', 'hidden');
for(let i=0;i<mystars.length;i++){
mystars[i].style.setProperty('display', 'none');
}
setTimeout(function(){
window_stars.classList.add('hyperdrive_animation');
document.querySelector("body").removeChild(tableau_de_bord);
}, 1500);
//window_stars.removeChild(document.querySelector("h1"));
}
red_button.addEventListener("click", function(){
letsHyperdrive();
});
//STARS IN WINDOW
const mystars = document.getElementsByClassName("star");
for(let i=0;i<mystars.length;i++){
let top=Math.floor(Math.random() * (98));
let left=Math.floor(Math.random() * (90));
mystars[i].style.setProperty('top', top+"%");
mystars[i].style.setProperty('left', left+"%");
}
getPlanet(planet_id);