<n-polygon> | SVG polygon Polymer

In this example below you will see how to do a <n-polygon> | SVG polygon Polymer 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
<!DOCTYPE html>
<html lang="en" >

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

  
</head>

<body>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/polymer/0.5.6/polymer.min.js"></script>

<polymer-element name="n-polygon" attributes="vertices size">
<template>
  <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 stroke-linecap="round" points="{{ points }}"></polygon>
  </svg>
  <style>
    :host {
      display: inline-block;
    }
    svg {
      overflow: visible;
    }
  </style>

</template>
  <script>
    Polymer('n-polygon', {
      vertices: '3',
      size: '100',
      created: function() {
        this.vertices = this.getAttribute('vertices');
        this.size = this.getAttribute('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
        }
        this.points = points
      },
    });
                                  
  </script>
</polymer-element>

<div class="card">
  <h1>Polymer 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/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/GJYMQo"  target="_blank">Angular 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/dogVjy" target="_blank">React version</a></li>
     <li><a href="https://codepen.io/pixelass/pen/XbxzqJ" target="_blank">Custom version</a></li>
 </ul>
</div>
  
  

</body>

</html>

/*Downloaded from https://www.codeseek.co/pixelass/andltn-polygonandgt-orandxa0svg-polygon-polymer-bdmoOr */
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;
}

Comments