<n-polygon> | SVG polygon React-component

In this example below you will see how to do a <n-polygon> | SVG polygon React-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 ⁣ ©

Technologies

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

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

  
</head>

<body>

  

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

  
  <div id="container"></div>
  
  <h2>Usage</h2>
  <pre>
&lt;Polygon vertices="3" size="50"/&gt;
&lt;Polygon vertices="5" size="50" 
         className="my-custom-class"/&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/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/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/react/0.13.0/react.min.js'></script>

  

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




</body>

</html>

/*Downloaded from https://www.codeseek.co/pixelass/andltn-polygonandgt-orandxa0svg-polygon-react-component-dogVjy */
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-react-component-dogVjy */
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"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Polygon = function (_React$Component) {
  _inherits(Polygon, _React$Component);

  function Polygon(props) {
    _classCallCheck(this, Polygon);

    var _this = _possibleConstructorReturn(this, (Polygon.__proto__ || Object.getPrototypeOf(Polygon)).call(this));

    _this.createPolygon = _this.createPolygon.bind(_this);
    return _this;
  }

  _createClass(Polygon, [{
    key: 'createPolygon',
    value: function createPolygon() {
      var points = '',
          x,
          y;
      var vertices = this.props.vertices;
      var size = this.props.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;
      }
      return points;
    }
  }, {
    key: 'render',
    value: function render() {
      var classes = 'n-polygon ' + this.props.className;
      return React.createElement(
        'div',
        { className: classes, style: { display: 'inline-block' } },
        React.createElement(
          'svg',
          { version: '1.1',
            xmlns: 'http://www.w3.org/2000/svg',
            x: '0px',
            y: '0px',
            height: this.props.size + 'px',
            width: this.props.size + 'px',
            viewBox: '0 0 ' + this.props.size + ' ' + this.props.size,
            style: { overflow: 'visible' } },
          React.createElement('polygon', { points: this.createPolygon(), 'stroke-linecap': 'round' })
        )
      );
    }
  }]);

  return Polygon;
}(React.Component);

var container = document.getElementById('container');
React.render(React.createElement(
  'div',
  null,
  React.createElement(Polygon, { vertices: '3', size: '50', className: 'reddish' }),
  React.createElement(Polygon, { vertices: '16', size: '100', className: 'yehellow' }),
  React.createElement(Polygon, { vertices: '4', size: '100', className: 'babablue' }),
  React.createElement(Polygon, { vertices: '5', size: '100', className: 'greenhorn' }),
  React.createElement(Polygon, { vertices: '6', size: '40' })
), container);

Comments