<n-polygon> | SVG polygon web-component

In this example below you will see how to do a <n-polygon> | SVG polygon web-component 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 web-component</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">
    <polyline stroke-linecap="round"></polyline>
  </svg>
  <style>
    :host {
      display: inline-block;
    }
    svg {
      overflow: visible;
    }
  </style>
  <script>
  </script>
</template>


<div class="card">
  <h1>web-component 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 /deep/ svg {
  fill: red;
  stroke: blue;
  stroke-width: 3;
}
  </pre>
   <ul>
    <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">Custom version</a></li>
</ul>
</div>
  
  

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




</body>

</html>

/*Downloaded from https://www.codeseek.co/pixelass/andltn-polygonandgt-orandxa0svg-polygon-web-component-aORLpR */
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-web-component-aORLpR */
var owner = document.currentScript.ownerDocument;
var nPolygon = Object.create(HTMLElement.prototype);
nPolygon.createdCallback = function () {
  var template = owner.querySelector("#n-polygon");
  var content = template.content.cloneNode(true);
  this.shadow = this.createShadowRoot();
  this.shadow.appendChild(content);
  var svg = this.shadow.querySelector('svg');
  var polyline = svg.querySelector('polyline');
  var points = '',
      x,
      y;
  var vertices = this.getAttribute('vertices');
  var size = this.getAttribute('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;
  }
  svg.setAttribute('height', size);
  svg.setAttribute('width', size);
  svg.setAttribute('viewBox', '0 0 ' + size + ' ' + size);
  polyline.setAttribute('points', points);
};
owner.registerElement('n-polygon', { 'prototype': nPolygon });

Comments