<svg
  xmlns="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  width="200"
  height="200"
  viewBox="0 0 1000 1000"
  style="background: #000;"
  role="img"
  aria-labelledby="solarSystemTitle"
  aria-describedby="solarSystemDescription"
>
  <title id="solarSystemTitle">A procedurally generated solar system</title>
  <desc id="solarSystemDescription">A 2D rendering of a solar system, with planets orbiting a central star.</desc>

  <!-- This element will store our graphics -->
  <g class="js-svg-wrapper"></g>
  
  <!-- This stores any global CSS styles -->
  <style>
    /* Storing values as custom properties will make them easier to change later */
    :root {
      --start-rotation: 0deg;
      --rotation-speed: 10s;
    }

    /* Set up an animation to rotate from 0 to 360 degrees */
    @keyframes orbit {
      from {
        transform: rotate(var(--start-rotation));
      }
      to {
        transform: rotate(calc(var(--start-rotation) + 360deg));
      }
    }
    .planet {
      /* Apply our animation to the planets */
      animation: orbit var(--rotation-speed) infinite linear;
      /* Within an SVG, the transform-origin is set relative to the SVG */
      /* This ensures our orbit will rotate around the center of our star */
      transform-origin: 50% 50%;
    }
  </style>
</svg>

<button class="js-refresh-button">Refresh</button>

/**
 * Code related to setting up the codepen, not the solar systems
 */

* {
  margin: 0;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
}

svg {
  width: min(100vw, calc(100vh - 4.5em));
  height: auto;
}

button {
  margin-top: 1em;
  appearance: none;
}

:root {
  --hue: 200;
}

button {
  /* Text Colors */
  --text-saturation: 90%;
  --text-lightness: 40%;
  
  --text-saturation-hover: calc(var(--text-saturation) + 10%);
  --text-lightness-hover: calc(var(--text-lightness) - 5%); ;
  
  --text-saturation-active: var(--text-saturation-hover);
  --text-lightness-active: calc(var(--text-lightness) - 10%); ;
  
  --text-saturation-disabled: calc(var(--text-saturation) - 60%);
  --text-lightness-disabled: calc(var(--text-lightness) + 10%);
  
  /* Background Colors */
  --background-saturation: 0%;
  --background-lightness: 100%;
  
  --background-saturation-hover: calc(var(--background-saturation) + 80%);
  --background-lightness-hover: calc(var(--background-lightness) - 5%);
  
  --background-saturation-active: var(--background-saturation-hover);
  --background-lightness-active: calc(var(--background-lightness) - 10%);
  
  --background-saturation-disabled: calc(var(--background-saturation) + 30%);
  --background-lightness-disabled: calc(var(--background-lightness) - 10%);
  
  /* Border Colors */
  --border-saturation: 90%;
  --border-lightness: 60%;
  
  --border-saturation-hover: calc(var(--border-saturation) + 10%);
  --border-lightness-hover: calc(var(--border-lightness) - 10%);
  
  --border-saturation-active: var(--border-saturation-hover);
  --border-lightness-active: calc(var(--border-lightness) - 20%);
  
  --border-saturation-disabled: calc(var(--border-saturation) - 60%);
  --border-lightness-disabled: calc(var(--border-lightness) + 20%);
  
  /* Focus shadow styles */
  --shadow-saturation-focus: 100%;
  --shadow-lightness-focus: 85%;
  
  /* Color Styles */
  color: hsl(var(--hue), var(--text-saturation), var(--text-lightness));
  background-color: hsl(var(--hue), var(--background-saturation), var(--background-lightness)); 
  border:0.1em solid hsl(var(--hue), var(--border-saturation), var(--border-lightness)); 
  
  /* Misc. Styles */
  border-radius: 0.25em;
  cursor: pointer;
  display: inline-block;
  font-size: 1em;
  padding: 0.5em 1em;
  transition-property: box-shadow, background-color, border-color, color;
  transition-timing-function: ease-out;
  transition-duration: 0.2s;
}

button:hover {
  color: hsl(
    var(--hue), 
    var(--text-saturation-hover), 
    var(--text-lightness-hover)
  );
  
  background-color: hsl(
    var(--hue), 
    var(--background-saturation-hover), 
    var(--background-lightness-hover)
  );
  
  border-color: hsl(
    var(--hue), 
    var(--border-saturation-hover), 
    var(--border-lightness-hover)
  );
}

button:active {
  color: hsl(
    var(--hue), 
    var(--text-saturation-active), 
    var(--text-lightness-active)
  );
  
  background-color: hsl(
    var(--hue), 
    var(--background-saturation-active), 
    var(--background-lightness-active)
  );
  
  border-color: hsl(
    var(--hue), 
    var(--border-saturation-active), 
    var(--border-lightness-active)
  );
}

button:focus {
  outline: none;
  box-shadow: 0 0 0 0.25em hsl(
    var(--hue), 
    var(--shadow-saturation-focus),
    var(--shadow-lightness-focus)
  );
}

body {
  font-family: -apple-system, BlinkMacSystemFont, avenir next, avenir, helvetica neue, helvetica, Ubuntu, roboto, noto, segoe ui, arial, sans-serif; 
}
// Define a couple variables about our SVG grid
const width = 1000;
const height = 1000;

// Define some helper functions to randomize plant size and orbit distance
let randomPlanetSize = () => randomInt(10, 50);
let randomOrbitDistance = () => randomInt(100, 120);

function draw() {
  let starSize = randomInt(70, 120);
  let markup = drawStar(starSize) + addPlanets(starSize);
  
  document.querySelector(".js-svg-wrapper").innerHTML = markup;
}

function addPlanets(starSize) {
  let count = 0;
  let markup = '';

  // Set up our first planet
  let planetSize = randomPlanetSize();
  let orbitDistance = starSize + randomOrbitDistance();

  // Keep adding planets until a planet's orbital distance and size would lead
  // to it extending past our canvas
  while (orbitDistance + planetSize < 500) {
    // Add our new planet and its orbit path to our markup
    markup += drawOrbit(orbitDistance) + drawPlanet(planetSize, orbitDistance, count);

    // Prep our next planet so the while loop can check whether it's in bounds
    planetSize = randomPlanetSize();
    orbitDistance += randomOrbitDistance();
    count ++;
  }

  return markup;
}

function drawStar(size) {
  // First, let's pick a color range
  const hueRange = randomItemInArray(["red", "yellow", "blue"]);
  // Then we can determine the hue based on our range
  let hue;
  if (hueRange === "red") {
    hue = randomInt(0, 60);
    // We allow 0-30 and then 330-360
    // If our number is above 30, lets increase it
    // so it lands in the 330-360 range.
    if (hue > 30) {
      hue += 300;
    }
  } else if (hueRange === "yellow") {
    hue = randomInt(40, 60);
  } else {
    hue = randomInt(190, 240);
  }

  // We'll use higher saturation and lightness values for our star
  // than our planets.
  const saturation = randomInt(90, 100);
  const lightness = randomInt(60, 80);
  const color = `hsl(${hue}, ${saturation}%, ${lightness}%)`;

  return `
    <circle 
      cx="${width / 2}" 
      cy="${height / 2}" 
      r="${size}" 
      fill="${color}"
    />
  `;
}

function drawPlanet(size, distance, count) {
  const hue = randomInt(0, 360);
  const saturation = randomInt(70, 100);
  const lightness = randomInt(50, 70);
  const color = `hsl(${hue}, ${saturation}%, ${lightness}%)`;
  const cx =  width/2 + distance;
  const cy =  height/2;

  // We'll use the current planet number to create unique IDs for our filters
  const id = `planet-${count}`;
  
  // We'll generate some random values for our turbulence
  const turbulenceType = randomBool() ? 'fractalNoise' : 'turbulence';
  const baseFrequencyX = random(0.5, 2) / size;
  const baseFrequencyY = random(2, 4) / size;
  const numOctaves = randomInt(3, 10);
  const seed = Math.random();

  // And some random values for our lighting
  const elevation = randomInt(30, 100);
  const surfaceScale = randomInt(5, 10);

  // We'll use those random valus to create our filter:
  const filter = `
    <filter id="${id}-texture">
      <feTurbulence
        type="${turbulenceType}"
        baseFrequency="${baseFrequencyX} ${baseFrequencyY}"
        seed="${seed}"
        numOctaves="${numOctaves}"
      />
      <feDiffuseLighting lighting-color="${color}" surfaceScale="${surfaceScale}">
        <feDistantLight elevation="${elevation}" />
      </feDiffuseLighting>
      <feComposite operator="in" in2="SourceGraphic"/>
    </filter>
  `;

  // And apply the filter to our planet:
  const planet = `
    <circle
      class="planet"
      style="
        --start-rotation:${randomInt(0, 360)}deg;
        --rotation-speed:${distance * randomInt(40, 70)}ms;
      "
      r="${size}" 
      cx="${cx}" 
      cy="${cy}"
      fill="#000"
      filter="url(#${id}-texture)"
    />
  `;

  return filter + planet;
}

function drawOrbit(distance) {
  // The orbit is centered and has a radius equal to our current distance
  return `
    <circle 
      cx="${width/2}" 
      cy="${height/2}" 
      r="${distance}" 
      stroke="#ccc"
      fill="none"
    />
  `;
}


/**
 * Initialize our art piece
 */
draw();

document.querySelector(".js-refresh-button").addEventListener("click", draw);

//
// Randomization Functions
//

// Return a number between two values.
function random(min, max) {
  const difference = max - min;
  return min + difference * Math.random();
}

// Returns a random integer between two values
function randomInt(min, max) {
  return Math.round(random(min, max));
}

// Returns true or false. By default the chance is 50/50 but you can pass in
// a custom probability between 0 and 1. (Higher values are more likely to
// return true.)
function randomBool(probability = 0.5) {
  return Math.random() > probability;
}

// Returns a random item from an array
function randomItemInArray(array) {
  return array[randomInt(0, array.length - 1)];
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.