<n-polygon> | SVG polygon ES6-template

In this example below you will see how to do a <n-polygon> | SVG polygon ES6-template 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 ⁣ ©
  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

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

  
</head>

<body>

  
<template id="n-polygon">
  <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" xml:space="preserve">
    <polygon stroke-linecap="round"></polygon>
  </svg>
  <style>
    :host {
      display: inline-block;
    }
    svg {
      overflow: visible;
    }
  </style>
  <script>
  </script>
</template>


<div class="card">
  <h1>ES6 template 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/GJYMQo" 
           target="_blank">Angular 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">web-component version</a></li>
 </ul>
</div>
  
  

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




</body>

</html>

/*Downloaded from https://www.codeseek.co/pixelass/andltn-polygonandgt-orandxa0svg-polygon-es6-template-XbxzqJ */
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-es6-template-XbxzqJ */
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Polygon = function () {
  function Polygon(vertices, size, node) {
    _classCallCheck(this, Polygon);

    this.points = '';
    this.node = node || [];
    this.size = size || 100;
    this.vertices = vertices || 3;
    this.createPoints = this.createPoints.bind(this);
    this.render(node);
  }

  _createClass(Polygon, [{
    key: 'createPoints',
    value: function createPoints() {

      var points = '';
      var x, y;
      for (var i = 0; i <= this.vertices; i++) {
        x = Math.cos(360 / this.vertices * i * Math.PI / 180 + (180 / this.vertices + 90) * Math.PI / 180) * this.size / 2 + this.size / 2;
        y = Math.sin(360 / this.vertices * i * Math.PI / 180 + (180 / this.vertices + 90) * Math.PI / 180) * this.size / 2 + this.size / 2;
        points += ' ' + x + ' ' + y;
      }
      return points;
    }
  }, {
    key: 'template',
    value: function template() {
      var points = this.createPoints();
      return '\n    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" \n         xmlns:xlink="http://www.w3.org/1999/xlink"\n         x="0px" y="0px" \n         height="' + this.size + 'px" width="' + this.size + 'px" \n         viewBox="0 0 ' + this.size + ' ' + this.size + '" xml:space="preserve">\n      <polyline points="' + points + '" stroke-linecap="round"></polyline>\n   </svg>\n   <style>n-polygon{display:inline-block;}n-polygon svg{overflow:visible;}</style>';
    }
  }, {
    key: 'render',
    value: function render(node) {
      node.innerHTML = this.template();
    }
  }]);

  return Polygon;
}();

var nodes = document.querySelectorAll('n-polygon');

Array.prototype.forEach.call(nodes, function (item, index) {
  var polygon = new Polygon(item.getAttribute('vertices'), item.getAttribute('size'), item);
});

Comments