Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                .container
              
            
!

CSS

              
                


$base: hsla(160, 10%, 70%, 1);

$white: hsla(60, 20%, 95%, 1);
$bright: lighten($base, 20);
$light: lighten($base, 10);
$shade: desaturate(darken($base, 30), 5);
$dark: desaturate(darken($base, 50), 5);
$black: desaturate(darken($base, 60), 5);

body,
html,
.container {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: $black;
}

svg{
  position:absolute;
  left:50%;
  top:50%;
  transform:translate(-50%, -50%);
  min-width: 720px;
  overflow:visible;
  fill: $dark;
}
              
            
!

JS

              
                var select = function(s) { return document.querySelector(s); };
var selectAll = function(s) { return document.querySelectorAll(s); };
var container = select(".container");

function makeBg(side, gutter, rows) {
  var rSide = side; // Ratio of rect side
  var rGutt = gutter; // Ratio of gutter
  var rowNum = rows; // Number of rows
  var unitNum = Math.pow(rowNum, 2); // Total number of units
  var rowBreak = rowNum; // Store row break point
  var rSpace = rSide+rGutt; // Total space for one rect
  var vbWidth = rowNum*rSide + (rowNum-1)*rGutt; // ViewBox width
  var vbHeight = vbWidth; // ViewBox height
  
  // Create the background SVG element
  var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");

  // Add the background SVG element to the DOM
  container.appendChild(svg);

  // Give the background SVG element attributes
  svg.setAttribute("id", "bg");
  svg.setAttribute("xmlns", "http://www.w3.org/2000/svg");
  svg.setAttribute("xlinkns", "http://www.w3.org/1999/xlink");
  svg.setAttribute("width", "100%");
  svg.setAttribute("viewBox", "0 0 " + vbWidth +" "+ vbHeight);
  
  // Populate SVG with rects
  for (var i = 0; i<unitNum; i++) {
    
    // Create a rect
    var r = document.createElementNS("http://www.w3.org/2000/svg", "rect");
    
    // Add each rect to the SVG
    svg.appendChild(r);
    
    // Give each rect dimensions
    r.setAttribute("width", rSide);
    r.setAttribute("height", rSide);
    r.setAttribute("class", "rectangle rectangle" +i);

    // Give each rect a unique position
    if(i < rowBreak ) {
      r.setAttribute("x", rSpace*(i-(rowBreak-rowNum)) );
      r.setAttribute("y", rSpace*((rowBreak/rowNum)-1));
      if (i+1 == rowBreak) {
        rowBreak += rowNum;
      }
    } 
  }
}
makeBg(5, 1, 4); // Side, gutter, rows




// Function to keep background centered on resize
window.addEventListener("resize", centerBg);
function centerBg() {
    TweenMax.set(bg, { x: "-50%", y: "-50%" });
};

// Function to return random number between given range
function randomBetween(min,max){
    return Math.floor(Math.random()*(max-min+1)+min);
};



var rects = selectAll(".rectangle"); // Array of rects
var mainTl = new TimelineMax(); // Main timeline

// Loop through the array and output a timeline for each rect in the array
function makeTls(parentTL) {
//   for(var i = 0; i<rects.length; i++){
//     var tl = new TimelineMax(); // Create a sub-timeline
//     var ranDur = randomBetween(0.5, 3); // Generate a random duration
    
//     tl.from(rects[i], ranDur, { scale: 0 }); // Specify unique tween
    
//     return tl; // Return a unique timeline 
//   }
  
  rects.reduce(function(parent, rect) {
    var tl = new TimelineMax();
    tl.from(rect, randomBetween(0.5, 3), { scale: 0 });
    return parent.add(tl);
  }, parentTL);
}

// mainTl.add( makeTls() ); // How to add a dynamic number of tls to the main tl?

// Convert to array 
rects = Array.from(rects); // Modern browsers
// Array.prototype.slice.call(rects) // Old browsers

makeTls(mainTl);







              
            
!
999px

Console