<n-polygon> | SVG polygon Angular

In this example below you will see how to do a <n-polygon> | SVG polygon Angular with some HTML / CSS and Javascript

Create a component for polygons with different tools. The goal is to have a component to easily create polygons that can be styled with CSS. Let's have a look how different tools can help us create these shapes.

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

Technologies

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

<head>
  <meta charset="UTF-8">
  <title><n-polygon> | SVG polygon Angular</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  

<div class="card" ng-app="App">
  <h1>Angular directive for SVG polygons</h1>

  
  <n-polygon vertices="3" size="50" class="reddish"></n-polygon>
  <n-polygon vertices="16" size="100" class="yehellow"></n-polygon>
  <n-polygon vertices="4" size="100" class="babablue"></n-polygon>
  <n-polygon vertices="5" size="100" class="greenhorn"></n-polygon>
  <n-polygon vertices="6" size="40"></n-polygon>
  
  <h2>Usage</h2>
  <pre>
&lt;n-polygon vertices="3" size="50"&gt;&lt;/n-polygon&gt;
&lt;n-polygon vertices="5" size="50" class="my-custom-class"&gt;&lt;/n-polygon&gt;
  </pre>
  
  <pre>
.my-custom-class svg {
  fill: red;
  stroke: blue;
  stroke-width: 3;
}
  </pre>
   <ul>
    <li><a href="https://codepen.io/collection/XRzjEb/" 
           target="_blank">All in one collection</a></li>
     <li><a href="https://codepen.io/pixelass/pen/zGmqoJ" target="_blank">Jade version</a></li>
     <li><a href="https://codepen.io/pixelass/pen/aORLpR" target="_blank">web-component version</a></li>
     <li><a href="https://codepen.io/pixelass/pen/dogVjy" target="_blank">React version</a></li>
     <li><a href="https://codepen.io/pixelass/pen/bdmoOr" target="_blank">Polymer version</a></li>
    <li><a href="https://codepen.io/pixelass/pen/BNqmzJ" target="_blank">Handlebars version</a></li>
    <li><a href="https://codepen.io/pixelass/pen/XbxzqJ" target="_blank">Custom version</a></li>
 </ul>
</div>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js'></script>

  

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




</body>

</html>

/*Downloaded from https://www.codeseek.co/pixelass/andltn-polygonandgt-orandxa0svg-polygon-angular-GJYMQo */
body {
  margin: 0;
  font-family: sans-serif;
  background: #982649;
  display: flex;
  justify-content: center;
  align-items: center;
}

* {
  box-sizing: border-box;
}

.reddish svg, .reddish /deep/ svg {
  fill: rgba(255, 100, 20, 0.4);
  stroke: #ff6414;
  stroke-width: 4;
}

.yehellow svg, .yehellow /deep/ svg {
  fill: rgba(255, 200, 20, 0.4);
  stroke: #ffc814;
  stroke-width: 3;
}

.babablue svg, .babablue /deep/ svg {
  fill: rgba(15, 45, 255, 0.2);
  stroke: rgba(15, 45, 255, 0.8);
}

.greenhorn svg, .greenhorn /deep/ svg {
  fill: rgba(105, 205, 55, 0.4);
  stroke: #69cd37;
  stroke-width: 3;
}

h1 {
  font-size: 25px;
  font-weight: lighter;
}

.card {
  max-width: calc(600px);
  min-width: calc(300px);
  width: calc(100% - 20px);
  padding: 24px 16px;
  background: #fff;
  box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12), 0 2px 4px -1px rgba(0, 0, 0, 0.4);
  margin: 30px auto;
}

pre {
  padding: 10px;
  border: 1px solid #ddd;
  border-left: 5px solid #86B155;
}

a {
  color: #FF5A5F;
}


/*Downloaded from https://www.codeseek.co/pixelass/andltn-polygonandgt-orandxa0svg-polygon-angular-GJYMQo */
angular.module('App', []).directive('nPolygon', function () {
  return {
    restrict: 'E',
    scope: {
      size: '@',
      vertices: '@'
    },
    template: '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" ng-attr-height="{{ size }}px" ng-attr-width="{{ size }}px" ng-attr-viewBox="0 0 {{ size }} {{ size }}" xml:space="preserve"><polygon ng-attr-points="{{ points }}" stroke-linecap="round"></polygon></svg><style>n-polygon{display:inline-block;}n-polygon svg{overflow:visible;}</style>',
    link: function link(scope, element, attrs) {
      var points = '',
          x,
          y;
      var vertices = attrs.vertices;
      var size = attrs.size;
      for (var i = 0; i <= vertices; i++) {
        x = Math.cos(360 / vertices * i * Math.PI / 180 + (180 / vertices + 90) * Math.PI / 180) * size / 2 + size / 2;
        y = Math.sin(360 / vertices * i * Math.PI / 180 + (180 / vertices + 90) * Math.PI / 180) * size / 2 + size / 2;
        points += ' ' + x + ' ' + y;
        scope.points = points;
      }
    }
  };
});

Comments