Concentric Hexagon Background

In this example below you will see how to do a Concentric Hexagon Background with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by 007design, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright 007design ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Concentric Hexagon Background</title>
  
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <html data-ng-app="app">

  <head>
    <link rel="stylesheet" href="style.css">
  </head>

  <body>
    <div class="container">
      <div class="hexagon-tile-background"></div>
    </div>
    
    <script data-require="jquery@2.1.1" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    
    <script data-require="angular.js@1.5.6" data-semver="1.5.6" src="https://code.angularjs.org/1.5.6/angular.min.js"></script>
    <script data-require="angular.js@1.5.6" data-semver="1.5.6" src="https://code.angularjs.org/1.5.6/angular-animate.min.js"></script>
    <script src="script.js"></script>
  </body>

</html>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/007design/concentric-hexagon-background-RRRKmz */
.container {
  position: absolute;
  overflow: hidden;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 80%;
  width: 80%;
}

.hexagon-tile-background {
  position: absolute;
  width: 100%;
  height: 100%;
}
.hexagon-tile-background .concentric-hex {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 60px;
  height: 100px;
  background-color: #ddd;
  text-align: center;
  display: inline-block;
  margin-right: 10px;
}
.hexagon-tile-background .concentric-hex .before {
  width: 0;
  height: 0;
  border-right: 30px solid #ddd;
  border-top: 50px solid transparent;
  border-bottom: 50px solid transparent;
  position: absolute;
  left: -30px;
  top: 0;
  z-index: 1;
}
.hexagon-tile-background .concentric-hex .after {
  width: 0;
  position: absolute;
  right: -30px;
  top: 0;
  border-left: 30px solid #ddd;
  border-top: 50px solid transparent;
  border-bottom: 50px solid transparent;
  z-index: 1;
}
.hexagon-tile-background .concentric-hex.even {
  background-color: #bbb;
}
.hexagon-tile-background .concentric-hex.even .before, .hexagon-tile-background .concentric-hex.even .after {
  border-left-color: #bbb;
  border-right-color: #bbb;
}


/*Downloaded from https://www.codeseek.co/007design/concentric-hexagon-background-RRRKmz */
/* global angular */
"use strict";

angular.module('app', [])

.directive('hexagonTileBackground', [function() {
  return {
    restrict: 'C',
    compile: function(tElem) {
      tElem.append('<span class="concentric-hex" data-ng-repeat="x in repeatX track by $index" data-ng-class-even="\'even\'" data-ng-style="{height:hexHeight($index),width:hexWidth($index)}"><span class="before"></span><span class="after"></span></span>');

      return function(scope, elem) {
        var hexSize = 50;

        var elemWidth = 0;
        var elemHeight = 0;
        
        scope.hexHeight = function(index) {
          var max = elemWidth>elemHeight ? elemWidth : elemHeight;
          return (max - hexSize * index);
        };
        
        scope.hexWidth = function(index) {
          var max = elemWidth>elemHeight ? elemWidth : elemHeight;
          return (max - hexSize * index) *.6;
        };

        function calculateRepeats() {
          var max = elemWidth>elemHeight ? elemWidth : elemHeight;
          var repeats = Math.ceil((max/2) / (hexSize/2));
          scope.repeatX = scope.hexWidth(repeats) < hexSize ? new Array(repeats-1) : new Array(repeats);
        }

        scope.$watch(function() {
          var hex = $('.concentric-hex').eq(0);
          var styles = hex.attr('style');
          console.log(styles)
          hex.removeAttr('style');
          var h = hex.height();
          console.log(h)
          hex.attr('style', styles);
          return h;
        }, function(h) {
          hexSize = h || 1;
          calculateRepeats();
        });

        scope.$watch(function() {
          return $(elem).parent().height();
        }, function(h) {
          elemHeight = h;
          calculateRepeats();
        });

        scope.$watch(function() {
          return $(elem).parent().width();
        }, function(w) {
          elemWidth = w;
          calculateRepeats();
        });

        $(window).on('resize',scope.$apply);
      };
    }
  }
}])

.directive('concentricHex', [function() {
  return {
    restrict: 'C',
    link: function(scope, elem) {
      scope.$watch(function() {
        return $(elem).height();
      }, function(w) {
        $(elem).find('.before').css({
          borderRightWidth: Math.round(w*.3) +'px',
          borderTopWidth: w/2 +'px',
          borderBottomWidth: w/2 +'px',
          left: Math.round((w*.3)*-1) + 'px'
        });
        $(elem).find('.after').css({
          borderLeftWidth: Math.round(w*.3) +'px',
          borderTopWidth: w/2 +'px',
          borderBottomWidth: w/2 +'px',
          right: Math.round((w*.3)*-1) + 'px'
        });
      });
    }
  };
}]);

Comments