Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <meta charset="utf-8">
- <style>
- body {font-size: 120%}
- select {font-size: 100%}
- </style>
- <script>
- //Symulacja ruchu planet w Układzie Słonecznym
- //Autor: Robert Kopias
- window.onload = function() {
- window.requestAnimFrame = (function(){
- return window.requestAnimationFrame ||
- window.webkitRequestAnimationFrame ||
- window.mozRequestAnimationFrame ||
- window.oRequestAnimationFrame ||
- window.msRequestAnimationFrame ||
- function(callback, element){
- window.setTimeout(callback, 1000 / 60);
- };
- })();
- window.addEventListener('resize', resize);
- var G = 6.67385e-11;
- var krok = 100; //[s]
- var coIteracjeDostepne = [10, 50, 100, 500, 1000, 5000, 10000];
- var coIteracje = 2;
- var czasOdPoczatku = 0;
- var chodzi = true;
- var Punkt = function(x, y) {
- this.x = x;
- this.y = y;
- };
- var CialoNiebieskie = function(nazwa, masa, polozenie, predkosc, liczyc, kolor, promien) {
- this.nazwa = nazwa;
- this.masa = masa;
- this.polozenie = polozenie;
- this.predkosc = predkosc;
- this.liczyc = liczyc;
- this.przesuniecie = new Punkt(0, 0);
- this.kolor = kolor;
- this.promien = promien;
- };
- var Widok = function(kanwa, ciala, maxWart) {
- this.kanwa = kanwa;
- this.ciala = ciala;
- this.kontekst = kanwa.getContext('2d');
- this.maxWart = maxWart;
- }
- var systemPlanet = new Array();
- systemPlanet.push(new CialoNiebieskie('Sun', 1.9891e30, new Punkt(0, 0), new Punkt(0, 0), false, 'yellow', 5));
- systemPlanet.push(new CialoNiebieskie('Mercury', 3.3302e23, new Punkt(46.001272e9, 0), new Punkt(0, 58.98e3), true, 'black', 0.5));
- systemPlanet.push(new CialoNiebieskie('Venus', 4.8685e24, new Punkt(107.476002e9, 0), new Punkt(0, 35.259e3), true, 'gray', 1));
- systemPlanet.push(new CialoNiebieskie('Earth', 5.9736e24, new Punkt(147.098291e9, 0), new Punkt(0, 30.29e3), true, 'blue', 1));
- systemPlanet.push(new CialoNiebieskie('Mars', 6.4185e23, new Punkt(206.644545e9, 0), new Punkt(0, 26.50e3), true, 'red', 0.5));
- systemPlanet.push(new CialoNiebieskie('Jupiter', 1.8986e27, new Punkt(740.7426e9, 0), new Punkt(0, 13.72e3), true, 'maroon', 5));
- systemPlanet.push(new CialoNiebieskie('Saturn', 5.6846e26, new Punkt(1.349467375e12, 0), new Punkt(0, 10.182e3), true, 'lightblue', 5));
- systemPlanet.push(new CialoNiebieskie('Uran', 8.6816e25, new Punkt(2.735555035e12, 0), new Punkt(0, 7.11e3), true, 'cyan', 3));
- systemPlanet.push(new CialoNiebieskie('Neptun', 1.0244e26, new Punkt(4.45963e12, 0), new Punkt(0, 5.478e3), true, 'darkblue', 2));
- systemPlanet.push(new CialoNiebieskie('Moon', 7.347673e22, new Punkt(147.098291e9 + 0.363104e9, 0), new Punkt(0, 30.29e3 + 1.082e3), true, 'silver', 0.3));
- var widoki = new Array();
- widoki.push(new Widok(document.getElementById('widok1'), [0, 1, 2, 3, 4, 5, 6, 7, 8], 5e12));
- widoki.push(new Widok(document.getElementById('widok2'), [3, 9], 0.45e9));
- var doPlanety = document.getElementById('doPlanety');
- for(var i = 1; i < systemPlanet.length - 1; i++) {
- var option = document.createElement('option');
- option.text = systemPlanet[i].nazwa;
- option.value = i;
- doPlanety.add(option);
- }
- //doPlanety.value = systemPlanet.length - 2;
- doPlanety.value = 4;
- widoki[0].maxWart = systemPlanet[doPlanety.value].polozenie.x * 1.2;
- doPlanety.onchange=function() {
- widoki[0].maxWart = Math.sqrt(systemPlanet[doPlanety.value].polozenie.x * systemPlanet[doPlanety.value].polozenie.x +
- systemPlanet[doPlanety.value].polozenie.y * systemPlanet[doPlanety.value].polozenie.y) * 1.2;
- };
- var legenda = document.getElementById('legenda');
- legenda.innerHTML = '';
- for(var i = 0; i < systemPlanet.length; i++)
- legenda.innerHTML = legenda.innerHTML +
- '<div style="color:' + systemPlanet[i].kolor + '">' + systemPlanet[i].nazwa + '</div>';
- function zmienCoIteracje(kierunek) {
- if(kierunek < 0 && coIteracje > 0)
- --coIteracje;
- if(kierunek > 0 && coIteracje < (coIteracjeDostepne.length - 1))
- ++coIteracje;
- if(coIteracje == 0)
- document.getElementById('coIteracjeMinus').disabled = true;
- else
- document.getElementById('coIteracjeMinus').disabled = false;
- if(coIteracje == (coIteracjeDostepne.length - 1))
- document.getElementById('coIteracjePlus').disabled = true;
- else
- document.getElementById('coIteracjePlus').disabled = false;
- document.getElementById('coIteracje').innerHTML = coIteracjeDostepne[coIteracje];
- }
- document.getElementById('coIteracjeMinus').onclick = function() {zmienCoIteracje(-1)};
- document.getElementById('coIteracjePlus').onclick = function() {zmienCoIteracje(1)};
- document.getElementById('pauseStart').onclick = function() {
- if(chodzi) {
- document.getElementById('pauseStart').innerHTML ='►';
- chodzi = false;
- } else {
- document.getElementById('pauseStart').innerHTML ='▐ ▌';
- chodzi = true;
- animacja();
- }
- };
- document.getElementById('schowajKsiezyc').onclick = function() {
- var widok2 = document.getElementById('widok2');
- if(widok2.style.display) {
- widok2.style.display = '';
- this.innerHTML = '< hide Moon';
- } else {
- widok2.style.display = 'none';
- this.innerHTML = '> show Moon';
- }
- }
- function resize() {
- var rozmiar = Math.floor(Math.min((window.innerWidth - 300) / 2, window.innerHeight - 80));
- for(var i = 0; i < widoki.length; i++) {
- widoki[i].kanwa.width = rozmiar;
- widoki[i].kanwa.height = rozmiar;
- }
- };
- function wyliczStanSystemu(deltaT) {
- for(var i = 0; i < systemPlanet.length; i++)
- if(systemPlanet[i].liczyc) {
- var sumaSil = new Punkt(0, 0),
- predkoscOdGrawitacji;
- for(var j = 0; j < systemPlanet.length; j++)
- if(i != j) {
- var kierunekSily = new Punkt(0, 0), odleglosc, dzielnik,
- wartoscSily, wektorSily = new Punkt(0, 0);
- kierunekSily.x = systemPlanet[j].polozenie.x - systemPlanet[i].polozenie.x;
- kierunekSily.y = systemPlanet[j].polozenie.y - systemPlanet[i].polozenie.y;
- odleglosc = Math.sqrt(kierunekSily.x * kierunekSily.x + kierunekSily.y * kierunekSily.y);
- wartoscSily = G * systemPlanet[i].masa * systemPlanet[j].masa / (odleglosc * odleglosc);
- dzielnik = odleglosc / wartoscSily;
- wektorSily.x = kierunekSily.x / dzielnik;
- wektorSily.y = kierunekSily.y / dzielnik;
- sumaSil.x += wektorSily.x;
- sumaSil.y += wektorSily.y;
- }
- predkoscOdGrawitacji = new Punkt((sumaSil.x / systemPlanet[i].masa) * deltaT,
- (sumaSil.y / systemPlanet[i].masa) * deltaT);
- //do obliczeń pozycji weźmiemy prędkość średnią - albo nie
- systemPlanet[i].przesuniecie = new Punkt((systemPlanet[i].predkosc.x + (predkoscOdGrawitacji.x / 1)) * deltaT,
- (systemPlanet[i].predkosc.y + (predkoscOdGrawitacji.y / 1)) * deltaT);
- systemPlanet[i].predkosc.x += predkoscOdGrawitacji.x;
- systemPlanet[i].predkosc.y += predkoscOdGrawitacji.y;
- }
- //teraz poprawimy pozycję
- for(var i = 0; i < systemPlanet.length; i++)
- if(systemPlanet[i].liczyc) {
- systemPlanet[i].polozenie.x += systemPlanet[i].przesuniecie.x;
- systemPlanet[i].polozenie.y += systemPlanet[i].przesuniecie.y;
- }
- }
- function animacja() {
- if(!chodzi)
- return;
- requestAnimFrame(animacja);
- wyliczStanSystemu(krok);
- czasOdPoczatku += krok;
- for(var i = 1; i < coIteracjeDostepne[coIteracje]; i++) {
- wyliczStanSystemu(krok);
- czasOdPoczatku += krok;
- }
- for(var i = 0; i < widoki.length; i++) {
- widoki[i].kanwa.width = widoki[i].kanwa.width;
- var skala = (widoki[i].kanwa.width / 2) / widoki[i].maxWart;
- var skalaSrednica = skala * 1e9;
- if(skalaSrednica * systemPlanet[widoki[i].ciala[0]].promien > 30)
- skalaSrednica /= skalaSrednica * systemPlanet[widoki[i].ciala[0]].promien / 30;
- for(var j = 0; j < widoki[i].ciala.length; j++) {
- widoki[i].kontekst.fillStyle = systemPlanet[widoki[i].ciala[j]].kolor;
- widoki[i].kontekst.beginPath();
- widoki[i].kontekst.arc(Math.floor((systemPlanet[widoki[i].ciala[j]].polozenie.x - systemPlanet[widoki[i].ciala[0]].polozenie.x) * skala + widoki[i].kanwa.width / 2),
- Math.floor((systemPlanet[widoki[i].ciala[j]].polozenie.y - systemPlanet[widoki[i].ciala[0]].polozenie.y) * skala + widoki[i].kanwa.width / 2),
- Math.floor(Math.max(systemPlanet[widoki[i].ciala[j]].promien * skalaSrednica, 3)), 0, Math.PI * 2, true);
- widoki[i].kontekst.closePath();
- widoki[i].kontekst.fill();
- }
- }
- document.getElementById('czas').textContent = Math.floor(czasOdPoczatku / 31536000) + ' years, ' + Math.floor((czasOdPoczatku % 31536000) / 86400) + ' days';
- }
- resize();
- zmienCoIteracje(0);
- animacja();
- };
- </script>
- </head>
- <body>
- <canvas id="widok1" style="float:left;border: 1px solid;"></canvas>
- <canvas id="widok2" style="float:left;border: 1px solid;"></canvas>
- <div style="float:left;margin-left:10px;">
- <button id="schowajKsiezyc" style="width:15em;">< hide Moon</button>
- <div> </div>
- <div> </div>
- <div>Zoom In</div>
- <select id="doPlanety"></select>
- <div> </div>
- <button id="pauseStart" style="width:5em;height:4ex">▐ ▌</button>
- <div> </div>
- <div>View every ... iteration:</div>
- <div>
- <button id="coIteracjeMinus" style="float:left;"><</button>
- <div id="coIteracje" style="float:left; width: 5em; text-align: center;"></div>
- <button id="coIteracjePlus" style="float:left;">></button>
- <div style="clear:both;"></div>
- </div>
- <div> </div>
- <div>Legenda:</div>
- <div id="legenda"></div>
- </div>
- <div style="clear:both;" id="czas"></div>
- <p>Made by Robert Kopias</p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement