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#canvas
              
            
!

CSS

              
                html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  padding: 0;
}
              
            
!

JS

              
                const canvas = document.getElementById('canvas'),
      ctx = canvas.getContext('2d');

let h = {},
    /** 
      The colors had to be put into global variables because Babel doesn't transpile Object.assign() and some browsers still don't have it!
    */
    colBG = '0, 0, 0',
    colFill = '123, 0, 123',
    colStroke = '123, 0, 123';

function rgba (rgb, a) {
  console.log(rgb);
  console.log(a);
  return 'rgba(' + rgb + ',' + a + ')'
}

/**
  A factory function to create the main object to hold the hexagon canvas and whatnot.
  Terribly convoluted for something so simple :D
*/
function createHexagonCanvas () {
  let c = {},
      sides = 6,
      angle = (Math.PI * 2) / sides;
  
  c.canvas = document.createElement('canvas');
  c.ctx = c.canvas.getContext('2d');
  c.size = canvas.width / 8;

  c.canvas.width = c.size * 2;
  c.canvas.height = ((Math.sqrt(3) / 2) * c.size) * 6;
  
  c.points = createPoints();
  
  drawTop();

  /**
    Draw the 'sides' at the front of the hexagon.
  */
  //right
  drawSide(0, 1, colFill);
  //front
  drawSide(1, 2);
  //left
  drawSide(2, 3);
  
  return c;
  
  /**
    Creates the 6 points for the top of the hexagon (and for the top of the sides).
  */
  function createPoints () {
    var points = [];
    for (let p = 0; p < sides; p += 1) {
      points.push({
        x: (c.canvas.width / 2) + c.size * Math.cos(p * angle),
        y: (c.canvas.height / 2) + c.size * Math.sin(p * angle) * .333,
      });
    }
    return points;
  }
  
  function drawTop () {
    c.ctx.lineWidth = 2;
    c.ctx.linecap = 'butt';
    //top
    c.ctx.beginPath();
    c.points.forEach((p, i) => {
      if (i === 0) {
        c.ctx.moveTo(p.x, p.y);
      }
      c.ctx.lineTo(p.x, p.y);
    });
    c.ctx.closePath();
    c.ctx.fillStyle = rgba(colBG, 1);
    c.ctx.fill();
    c.ctx.fillStyle = rgba(colFill, .555);
    c.ctx.fill();
    c.ctx.strokeStyle = rgba(colStroke, 1);
    c.ctx.stroke();    
  }

  function drawSide (from, to, col) {
    c.ctx.save();
    c.ctx.beginPath();
    c.ctx.moveTo(c.points[from].x, c.points[from].y);
    c.ctx.lineTo(c.points[from].x, c.points[from].y + c.size * 3);
    c.ctx.lineTo(c.points[to].x, c.points[to].y + c.size * 3);
    c.ctx.lineTo(c.points[to].x, c.points[to].y);
    c.ctx.closePath();
    c.ctx.fillStyle = rgba(colBG, 1);
    c.ctx.fill();
    c.ctx.fillStyle = rgba(colFill, .777 - (1 / to));
    c.ctx.fill();
    c.ctx.strokeStyle = rgba(colStroke, 1);
    c.ctx.stroke();
    c.ctx.restore();
  }
}

/**
  Creates the points for where each hexagon will be drawn.
*/
function populateHexagons () {
  let hexagons = [],
      tempArr = [];
  
  for (let j = -6; j < (canvas.height / h.canvas.height) * 6; j += 1) {
    tempArr = [];
    for (let i = -1; i < (canvas.width / h.canvas.width); i += 1) {
      tempArr.push({
        x: (j % 2 === 0) ? (i * (h.canvas.width * 1.5)) + 3 * (h.canvas.width / 4) : i * (h.canvas.width * 1.5), 
        y: j * (h.canvas.height / 6),
      });
    }
    hexagons.push(tempArr);
  }

  return hexagons;
}

let counter = 0;

/**
  The 'draw loop' which draws the hexagon block canvas in rows across, and columns down, the main canvas.
*/
function loop () {
  ctx.save();
  /**
    No need to draw a background as the hexagon block canvas' cover the entire main canvas
  */
  //ctx.fillStyle = rgba(colBG, 1);
  //ctx.fillRect(0, 0, canvas.width, canvas.height);
  
  //draw
  h.hexList.forEach((arr, i) => {
    arr.forEach((hex, j) => {
      /**
        Draws the hexagon block canvas to the points held by the main object.
        The 'y' (vertical point) fluctuates via a cosine wave which is generated by a combination of which horizontal row the block is on, and by a non-local 'counter' variable.
        Including the row index 'i', ensures that each row is at a different point on the cosine wave. It is subtracted from the counter so that the wave of blocks appears to move down the screen.
      */
      ctx.drawImage(h.canvas, hex.x, hex.y + (h.size / 2 * Math.cos(counter - i)));
    });
  });
  
  counter += 0.01;
  
  requestAnimationFrame(loop);
}

/**
  IIFE which sizes the canvas to the body, creates and assigns the main object and begins the loop.
*/
void (function init () {
  let bodyRect = document.body.getBoundingClientRect();
  
  canvas.width = bodyRect.width;
  canvas.height = bodyRect.height;
  
  ctx.fillRect(0, 0, canvas.width, canvas.height);

  //Object.assign doesnt work on current android chrome / webkit mobile browsers
  //h = Object.assign(h, createHexagonCanvas());
  h = createHexagonCanvas();

  h.hexList = populateHexagons();
  
  loop();
})();
              
            
!
999px

Console