Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<div id="gui">
<label for="speed">Simulation Speed
  <input type="range" min="1" max="100" value="50" id="speed" />
</label>
<button id="fullscreen"><svg width="12" height="12" viewBox="-8 10 16 12">
  <path d="M13,11H18L16.5,9.5L17.92,8.08L21.84,12L17.92,15.92L16.5,14.5L18,13H13V18L14.5,16.5L15.92,17.92L12,21.84L8.08,17.92L9.5,16.5L11,18V13H6L7.5,14.5L6.08,15.92L2.16,12L6.08,8.08L7.5,9.5L6,11H11V6L9.5,7.5L8.08,6.08L12,2.16L15.92,6.08L14.5,7.5L13,6V11Z" transform="rotate(45)"></path>
</svg> Fullscreen</button>
<span id="copyright">Copyright © 2020 <a href="http://zasenko.name" target="_blank">Sergey Zasenko</a></span>
</div>

<div id="error"></div>
<!--
<textarea id="input" ><x=-4, y=3, z=15>
<x=-11, y=-10, z=13>
<x=2, y=2, z=18>
<x=7, y=-1, z=0>
</textarea>
<button id="start">Start simulation</button> -->
              
            
!

CSS

              
                body { margin: 0;
  background-color: #1a1a2a; 
  color: #ebebeb;
}

canvas { width: 100%; height: 100% }

#error {
  color: red;
  text-align: center;
  font-size: 2em;
}

#gui {
  display: flex;
  flex-direction: row;
}

#copyright {
  margin-left: auto;
  margin-right: 10px;
}
              
            
!

JS

              
                /*  Copyright © 2020 Sergey Zasenko

    The JavaScript code in this file is free software: you can
    redistribute it and/or modify it under the terms of the GNU General
    Public License (GNU GPL) as published by the Free Software Foundation,
    either version 3 of the License, or (at your option) any later
    version.  The code is distributed WITHOUT ANY WARRANTY; without even
    the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    PURPOSE.  See the GNU GPL for more details.

    As additional permission under GNU GPL version 3 section 7, you may
    distribute non-source (e.g., minimized or compacted) forms of that
    code without the copy of the GNU GPL normally required by section 4,
    provided you include this license notice and a URL through which
    recipients can access the Corresponding Source. */

function simulate(moons) {
    for (let i = 0; i < moons.length - 1; i++) {
        for (let j = i+1; j < moons.length; j++) {
            for (let k = 0; k < 3; k++) {
                let mp = [moons[i], moons[j]];
                let c = mp[0][0][k] - mp[1][0][k]

                if (c == 0)
                    continue;
                if (c > 0)
                    mp = mp.reverse();

                ++mp[0][1][k];
                --mp[1][1][k];
            }
        }
    }

    for (let i = 0; i < moons.length; i++)
            for (let k = 0; k < 3; k++) {
                let v = moons[i][1][k]
                moons[i][0][k] += moons[i][1][k]
            }
}

window.addEventListener( 'resize', () => {
  camera.aspect = window.innerWidth / window.innerHeight;
	camera.updateProjectionMatrix();
	renderer.setSize( window.innerWidth, window.innerHeight );
}, false );
      
let simulationSpeed = 1;
let speedEl = document.getElementById("speed");
function getSimulationSpeed() {
  simulationSpeed = (speedEl.value / (speedEl.max - speedEl.min + 1)) / 100;
};

function setFullscreenHandler(el) {
  let rfs = el.webkitRequestFullScreen ?
     () => { el.webkitRequestFullScreen() }
     : () => { el.mozRequestFullScreen() };

  document.getElementById("fullscreen")
    .onclick = rfs;
};

speedEl.onchange = getSimulationSpeed;
getSimulationSpeed();

var scene = new THREE.Scene();

var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000);
      
function random(max) {
  return Math.floor(Math.random() * Math.floor(max));
}

{
  let width = window.innerWidth;
  let height = window.innerHeight;
  const near = 1;
  const far = 100;
  const color = 0x1a1a2a;
  scene.fog = new THREE.Fog(color, near, far);
  scene.background = new THREE.Color(color);
  let size = width * height;
  
  let data = new Uint8Array( 3 * size ).fill(0);

   const backgroundMaxColor = 255 * 0.1
  for (let stars = 0; stars < size * 0.010; stars++) {
	  var stride = random(size/3) * 3;
	  data[ stride ] = random(backgroundMaxColor);
	  data[ stride + 1 ] = random(backgroundMaxColor);
	  data[ stride + 2 ] = random(backgroundMaxColor);
  }
  // used the buffer to create a DataTexture
  let texture = new THREE.DataTexture( data, width/2, height/2, THREE.RGBFormat );
  scene.background = texture;
}      

var renderer;

try {
  renderer = new THREE.WebGLRenderer({antialiasing: true});
} catch (error) {
  document.getElementById("error").innerText = error;
}

if (renderer != null) {
  renderer.setSize( window.innerWidth, window.innerHeight );
  document.body.appendChild( renderer.domElement );
  setFullscreenHandler(renderer.domElement);
}

const planetRadius = 1;
const radialSegments = 16;
var sphereGeometry = new THREE.SphereGeometry( planetRadius, radialSegments, 16);

var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );

let moons = [ 
  [ [ -4, 3, 15 ], [ 0, 0, 0 ] ],
  [ [ -11, -10, 13 ], [ 0, 0, 0 ] ],
  [ [ 2, 2, 18 ], [ 0, 0, 0 ] ],
  [ [ 7, -1, 0 ], [ 0, 0, 0 ] ] 
]

let coordinates = ['x', 'y', 'z'];

const colors = [0x968AA2, 0xBA590A, 0xEFA18D, 0x456494];

var moonsMaterial = colors.map(color => new THREE.MeshBasicMaterial({color: new THREE.Color(color)}));

var moonsObjects = 
  moons.map((moon, i) => {
    let moonObj = new THREE.Mesh(sphereGeometry, moonsMaterial[i])
    
    moonObj.position.set(...moon[0]);

    return moonObj;
  });

const orbitLength = 8;
var moonOrbits = moons.map((moon, i) => 
   new Array(orbitLength).fill(null).map(() => {
      geometry = new THREE.SphereGeometry( planetRadius);
    
      let mesh = new THREE.Mesh( geometry, moonsMaterial[i].clone());
      mesh.position.set(...moon[0]);
      mesh.material.transparent = true;
      mesh.material.opacity = 0;
      scene.add(mesh);
      return mesh;
    })
);

moonsObjects.forEach(moon => scene.add(moon));

camera.position.set(20, -60, 30);
camera.lookAt(0, 0, 0);

let simulations = [];
function prepareSimulations() {
  while (simulations.length < 3) {
    simulate(moons);
    simulations.push(moons.map(moon => moon[0].slice()));
  }
}

prepareSimulations();

let previousTimestamp = window.performance.now();
function animate(timestamp) {
  let fraction = (timestamp - previousTimestamp) * simulationSpeed;

  //console.log(fraction);
  if (fraction > 1) {
    --fraction;
    simulations.shift();
    previousTimestamp = timestamp;
    
    moonOrbits.forEach((orbits, i) => {
      orbits.unshift(orbits.pop());
      moonOrbits[i][0].position.set(...simulations[0][i]);
      orbits.forEach((orbit, i) => 
           orbit.material.opacity = (1-i/(orbits.length-1))/2.5);
                
    });
  }
  prepareSimulations();

  for (let i = 0; i < moons.length; i++) {
    let position = simulations[0][i].slice();

    for (let j = 0; j < 3; j++)
      position[j] += (simulations[1][i][j] - position[j]) * fraction;

    moonsObjects[i].position.set(...position);
    // Set orbit!!!
  }

	requestAnimationFrame( animate );
	renderer.render( scene, camera );
}

if (renderer != null)
  animate();
              
            
!
999px

Console