css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="stage"></div>
            
          
!
            
              html, body {
  height: 100%;
  overflow: hidden;
  margin: 0;
}

@keyframes zoom-out {
  from {
    transform: translateZ(350px);
  }
}

@keyframes rotate {
  to {
    transform: rotate(360deg);
  }
}

@keyframes scale {
  from {
    transform: scale(0);
  }
}

.stage {
  position: absolute;
  overflow: hidden;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  perspective: 1000px;
}

.container {
  position: absolute;
  overflow: hidden;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  will-change: transform;
  animation: zoom-out 60s ease-out both;
}

.positioner {
  position: absolute;
  top: 0;
  left: 0;
}

circle {
  fill: none;
  stroke: rgba(255, 255, 255, 0.1);
}

rect {
  fill: rgba(255, 255, 255, 0.2);
}

.scaler {
  will-change: transform;
  animation: scale 3s ease-out both;
}

.rotator {
  display: block;
  animation: rotate 5s infinite linear;
}

.sunny {
  background: linear-gradient(to bottom, #22c1c3, #fdbb2d);
}

.ocean {
  background: linear-gradient(to bottom, #000046, #1CB5E0)
}

.day {
  background: linear-gradient(to bottom, #36d1dc, #5b86e5);
}

.swamp {
  background: linear-gradient(to top, #43c6ac, #191654);
}

.dawn {
  background: linear-gradient(to top, #3B4371, #F3904F);
}

.love {
  background: linear-gradient(to top, #89253e, #3a6186);
}

            
          
!
            
              const stage = document.querySelector('.stage');

const bodyRange = document.createRange();
bodyRange.setStart(document.body, 0);

function parse(str) {
  const frag = bodyRange.createContextualFragment(str);
  return frag.firstElementChild;
}

function generateCircles() {
  const container = parse(`<div class="container"></div>`);
  const { width, height } = stage.getBoundingClientRect();
  const maxRadius = 0.2 * Math.max(width, height);
  const strokeWidth = Math.min(20, Math.min(width, height) / 50);
  const minSpace = strokeWidth * 2;
  const circles = [];

  createCircles: while (true) {
    let x; // circle center x.
    let y; // circle center y.
    let r; // circle radius.
    let miss = 0; // Number of times we've failed to place a circle.

    randomCircle: while (true) {
      // Create random position
      x = Math.random() * width;
      y = Math.random() * height;
      r = maxRadius;

      // Go through existing circles, and reduce the size of this circle so it
      // doesn't intersect others.
      for (const circle of circles) {
        // Calculate distance to this circle
        const xDiff = circle.x - x;
        const yDiff = circle.y - y;
        const centerDistance = Math.sqrt((xDiff ** 2) + (yDiff ** 2));

        // It looks a bit crap if a circle appears centered within another.
        if (centerDistance < 20) continue randomCircle;

        const space = centerDistance - circle.r;

        if (space > -minSpace && space < minSpace) {
          // Circle is too close to another
          miss++;

          // If we've failed to create 200 circles, probably best that we stop trying.
          // lol yeah this is a massive hack.
          // Don't look at me like that, I threw this together quickly.
          // C'mon, like you haven't written hackier code.
          // Yeah I'm sure there's some smart mathsy way to do this.
          // My school failed ofstead give me a break.
          // Look, if you have a smarter way of doing this, I'm genuinely interested.
          // Anyway…
          if (miss > 200) {
            break createCircles;
          }

          // Try again.
          continue randomCircle;
        }

        // Reduce the radius of this circle so it doesn't intersect the other.
        r = Math.min(Math.abs(space) - strokeWidth, r);
      }

      break;
    }

    // Add our circle.
    circles.push({ x, y, r });
  }

  // Right, we're done defining our circles.
  // Now to make the elements.

  // Order the circles by radius.
  circles.sort((a, b) => {
    if (a.r < b.r) return 1;
    return -1;
  });

  for (const [i, circle] of circles.entries()) {
    const boxSize = strokeWidth;

    // Create SVG for the circle.
    // This is also the element that rotates.
    const rotator = parse(`
      <svg width="${circle.r * 2 + strokeWidth}" height="${circle.r * 2 + strokeWidth}" class="rotator">
        <circle cx="${circle.r + strokeWidth / 2}" cy="${circle.r + strokeWidth / 2}" r="${circle.r}" stroke-width="${strokeWidth}" />
        <rect x="${circle.r + strokeWidth / 2 - boxSize / 2}" y="${strokeWidth / 2 - boxSize / 2}" width="${boxSize}" height="${boxSize}" />
      </svg>
    `);

    // We need an element to scale, to grow the circle.
    const scaler = parse(`<div class="scaler"></div>`);
    scaler.appendChild(rotator);

    // Delay each circle appearing a little more.
    scaler.style.animationDelay = `${i*200}ms`;

    // We need an element to position the circle.
    const positioner = parse(`<div class="positioner"></div>`);
    positioner.appendChild(scaler);

    // Give it its position & a random rotation.
    positioner.style.transform = `translate(${circle.x - circle.r - strokeWidth / 2}px, ${circle.y - circle.r - strokeWidth / 2}px) rotate(${Math.random() * 360}deg)`;

    // Add the element to the stage.
    container.appendChild(positioner);
  }

  stage.appendChild(container);
}

const backgrounds = ['sunny', 'ocean', 'day', 'swamp', 'dawn', 'love'];

function randomBackground() {
  const currentClass = stage.classList[1];
  const otherBackgrounds = backgrounds.filter(b => b !== currentClass);
  const randomClass = otherBackgrounds[Math.floor(Math.random() * otherBackgrounds.length)];
  stage.classList.remove(currentClass);
  stage.classList.add(randomClass);
}

function clear() {
  stage.innerHTML = '';
}

requestAnimationFrame(() => {
  randomBackground();
  generateCircles();
});

window.addEventListener('resize', () => {
  console.log('resize');
  clear();
  generateCircles();
});

document.addEventListener('click', () => {
  clear();
  randomBackground();
  generateCircles();
});

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console