Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                <canvas id="canvas"></canvas>
              
            
!

CSS

              
                body {
  overflow: hidden;
  background: #000;
}

#canvas {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);  
  background: #222;
}
              
            
!

JS

              
                

//
// ES6 CLASS
// ===========================================================================
class Tile1 {
  
  constructor(x, y, width, height, fill) {
    
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.fill = fill;
  }
  
  draw(context) {
    
    context.fillStyle = this.fill;
    context.fillRect(this.x, this.y, this.width, this.height);
  }
}

//
// CONSTRUCTOR FUNCTION
// ===========================================================================
function Tile2(x, y, width, height, fill) {
  
  this.x = x;
  this.y = y;
  this.width = width;
  this.height = height;
  this.fill = fill;
}

Tile2.prototype.draw = function(context) {
  
  context.fillStyle = this.fill;
  context.fillRect(this.x, this.y, this.width, this.height);
};


//
// FACTORY
// ===========================================================================
function createTile(x, y, width, height, fill) {  
  
  var tile = {
    x: x,
    y: y,
    width: width,
    height: height,
    fill: fill,
    draw: function(context) {
      
      context.fillStyle = this.fill;
      context.fillRect(this.x, this.y, this.width, this.height);
    }
  };
  
  return tile;
}


//
// DEMO
// ===========================================================================
var canvas = document.querySelector("#canvas");
var context = canvas.getContext("2d");

canvas.width = 800;
canvas.height = 600;

var rows = 3;
var cols = 6;
var tileWidth = 100;
var tileHeight = 80;
var gutter = 10;
var padding = 10;

var row1 = [];
var row2 = [];
var row3 = [];

var x = padding;
var y = padding;

canvas.width = (padding * 2) + (tileWidth * cols) + (gutter * (cols - 1));
canvas.height = (padding * 2) + (tileHeight * rows) + (gutter * (rows - 1));


// Row1 using ES6 classes
for (var col = 0; col < cols; col++, x += tileWidth + gutter) {
    
  var tile = new Tile1(x, y, tileWidth, tileHeight, "red");
  row1.push(tile);
}

x = padding;
y += tileHeight + gutter;

// Row2 using constructor function
for (var col = 0; col < cols; col++, x += tileWidth + gutter) {
    
  var tile = new Tile2(x, y, tileWidth, tileHeight, "orange");
  
  // unshift adds to front of array
  row2.unshift(tile);
}

x = padding;
y += tileHeight + gutter;

// Row3 using factory function
for (var col = 0; col < cols; col++, x += tileWidth + gutter) {
    
  var tile = createTile(x, y, tileWidth, tileHeight, "blue");
  row3.push(tile);  
}

// Combine row arrays into sinele array
var tiles = [].concat(row1, row2, row3);

var tl = new TimelineMax({
    repeat: -1
  })
  .staggerTo(tiles, 1, {
    colorProps: { fill: "#eee" },
    x: "+=5",
    y: "+=5",
    width: "-=10",
    height: "-=10",
    repeat: 1,
    yoyo: true
  }, 0.1);


TweenLite.ticker.addEventListener("tick", render);

function render() {
  
  context.clearRect(0, 0, canvas.width, canvas.height);
  
  for (var i = 0; i < tiles.length; i++) {
    
    var tile = tiles[i];
    tile.draw(context);
  }
}

              
            
!
999px

Console