cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              @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
🕑 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