cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              @import "compass/css3";

html {
  background: #222;
  padding: 30px;
}

#grid {
  position: absolute;
  left: 50%;
  margin-left: -200px;
  @include transition(all 500ms);
}

#grid div {
  position: absolute;
  background: #feffe7;
  box-sizing: border-box;
  @include transition(all);
}
            
          
!
            
              function Block (i) {
    this.w = Math.round(Math.random() * 3) || 1;
    this.h = Math.round(Math.random() * 3) || 1;
    this.dom = $('<div>').css({
        width : this.w * 10 - 2,
        height : this.h * 10 - 2,
        transition : 'all 300ms ' + i + 'ms'
    });
    $('#grid').append(this.dom);
}

Block.prototype = {
    setPosition : function (x, y) {
        this.dom.css({
            top : y * 10,
            left : x * 10
        });
    }
}


function Grid () {
    this.blocks = [];
    this.grid = [];
}

Grid.prototype = {
    columns : 40,

    add : function (block) {
        this.blocks.push(block);
        this.findPlace(block);
    },

    clear : function () {
        var x, y;
        for (y = 0; y < this.grid.length; y++) {
            for (x = 0; x < this.grid[y].length; x++) {
                this.grid[y][x] = false;
            }
        }
        this.firstEmpty = 0;
    },

    firstEmpty : 0,
    findPlace : function (block) {
        var x, y,
            maxX = this.columns - block.w,
            maxY = this.grid.length + block.h;

        for (y = this.firstEmpty; y < maxY; y++) {
            for (x = 0; x <= maxX; x++) {
                if (this.place(block, x, y)) {
                    return;
                }
            }
        }
    },

    place : function (block, x, y) {
        var i, j,
            w = block.w,
            h = block.h;
        // check if we can place it
        for (i = 0; i < h; i++) {
            if (!this.grid[y + i]) {
                this.grid[y + i] = [];
            }
            for (j = 0; j < w; j++) {
                if (this.grid[y + i][x + j]) {
                    return false;
                }
            }
        }
        // we can place it, do so now
        for (i = 0; i < h; i++) {
            for (j = 0; j < w; j++) {
                this.grid[y + i][x + j] = true;
            }
        }
        block.setPosition(x, y);
        this.updateFirstEmpty();
        // return true so we know we placed the block
        return true;
    },

    updateFirstEmpty : function () {
        var x, y, hasEmpty;
        for (y = this.firstEmpty; y < this.grid.length; y++) {
            hasEmpty = false;
            for (x = 0; x < this.columns; x++) {
                if (!this.grid[y][x]) {
                    hasEmpty = true;
                }
            }
            if (hasEmpty) {
                this.firstEmpty = y;
                return;
            }
        }
    },

    setColumns : function (columns) {
        var i, block;
        if (columns === this.columns) {
            return;
        }
        $('#grid').css('margin-left', -columns * 5);
        this.columns = columns;
        this.clear();
        for (i = 0; i < this.blocks.length; i++) {
            this.findPlace(this.blocks[i], i);
        }
    }
}

var grid = new Grid();

for (var i = 0; i < 300; i++) {
    grid.add(new Block(i));
}


// change column count every second

var lastChange = +new Date();
function changeColumns() {
    var now = +new Date();
    var columns; 
    requestAnimationFrame(changeColumns);
    if (now - lastChange > 1000) {
        lastChange = now;
        columns = 10 + Math.round(Math.random() * 60);
        if (grid.columns === columns) {
            columns ++;
        }
        grid.setColumns(columns);
    }
}

changeColumns();
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console