<n-polygon> | SVG polygon Handlebars

In this example below you will see how to do a <n-polygon> | SVG polygon Handlebars 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 Handlebars</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  

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

  <div class="nPolygon"></div>
  <script id="nPolygon" type="text/x-handlebars-template">

  {{>nPolygon vertices="3" size="50" class="reddish"}}
  {{>nPolygon vertices="16" size="100" class="yehellow"}}
  {{>nPolygon vertices="4" size="100" class="babablue"}}
  {{>nPolygon vertices="5" size="100" class="greenhorn"}}
  {{>nPolygon vertices="6" size="40"}}
  </script>
  <h2>Usage</h2>
  <pre>
{{>nPolygon vertices="3" size="50"}}
{{>nPolygon vertices="5" size="50" 
            class="my-custom-class"}}
  </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/GJYMQo" target="_blank">Angular 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/XbxzqJ" target="_blank">Custom version</a></li>
 </ul>
</div>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.0/handlebars.min.js'></script>

  

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




</body>

</html>

/*Downloaded from https://www.codeseek.co/pixelass/andltn-polygonandgt-orandxa0svg-polygon-handlebars-BNqmzJ */
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-handlebars-BNqmzJ */
Handlebars.registerPartial('nPolygon', '<n-polygon class="{{ class }}"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" height="{{ size }}px" width="{{ size }}px" viewBox="0 0 {{ size }} {{ size }}" xml:space="preserve"><polygon points="{{ points }}" stroke-linecap="round"></polygon></svg><style>n-polygon{display:inline-block;}n-polygon svg{overflow:visible;}</style></n-polygon>');
var context = {
  points: function points() {
    this.vertices = this.vertices;
    this.size = this.size;
    var points = '',
        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;
  }
};

//Retreive the template data from the HTML .
var template = document.querySelector('#nPolygon').innerHTML;

//Compile the template data into a function
var templateScript = Handlebars.compile(template);

var html = templateScript(context);
//html = 'My name is Ritesh Kumar . I am a developer.'

document.querySelector('.nPolygon').innerHTML = html;

Comments