项目作者: airtoxin

项目描述 :
hexagonal grids with react.js
高级语言: JavaScript
项目地址: git://github.com/airtoxin/react-hex.git
创建时间: 2016-04-11T22:38:33Z
项目社区:https://github.com/airtoxin/react-hex

开源协议:MIT License

下载


react-hex Build Status

draw svg hexagonal grid with react

Install

$ npm i react-hex

Example

  1. import React from 'react';
  2. import ReactDom from 'react-dom';
  3. import Hex, { gridPoints } from 'react-hex';
  4. const Hexes = () => {
  5. const hexes = gridPoints('pointy-topped', 100, 100, 10, 25, 25).map(({ props }) => (
  6. <Hex {...props} fill="white" stroke="black" ></Hex>
  7. ));
  8. return (
  9. <svg width="500" height="500">
  10. {hexes}
  11. </svg>
  12. );
  13. };
  14. ReactDom.render(<Hexes ></Hexes>); document.getElementById('example'));

hexes

More examples, see Storybook.

Documents

<Hex type={} x={} y={} size={} ></Hex> (default exported)

Main React component of hex.

name value type description
type PropTypes.oneOf([‘pointy-topped’, ‘flat-topped’]).isRequired hexagon type
x PropTypes.number.isRequired hexagon’s center coordinate x
y PropTypes.number.isRequired hexagon’s center coordinate y
size PropTypes.number.isRequired hexagon edge length
  1. import Hex from 'react-hex';
  2. <Hex type="pointy-topped" x={0} y={0} size={50} ></Hex>

gridPoint(oType, oX, oY, size, gridX, gridY)

return: { props: { type, x, y, size }, gridX, gridY }

Helper function to calculate hex location in grid.
props field in returning object of this function can use for props of Hex component.

(prefix o means original.)

name value type description
oType ‘pointy-topped’ or ‘flat-topped’ original hexagon type
oX number original hexagon’s center coordinate x
oY number original hexagon’s center coordinate y
size number hexagon edge length
gridX number coordinate x in hexagonal grid system
gridY number coordinate y in hexagonal grid system
  1. import Triangle, { gridPoint } from 'react-hex';
  2. const { props } = gridPoint('pointy-topped', 0, 0, 50, 3, 4);

gridPoints(oDirection, oX, oY, size, girdWidth, gridHeight)

return: [ { props: { type, x, y, size }, gridX, gridY }, ... ]

Helper function to bulk calculate hexes location of grid.

(prefix o means original.)

name value type description
oType ‘pointy-topped’ or ‘flat-topped’ original hexagon type
oX number original hexagon’s center coordinate x
oY number original hexagon’s center coordinate y
size number hexagon edge length
gridWidth number grid size of x
gridHeight number grid size of y
  1. import Hex, { gridPoints } from 'react-hex';
  2. const triangles = gridPoints('pointy-topped', 100, 100, 50, 5, 5).map(({ props, gridX, gridY }) = (
  3. <Hex key={`${gridX}-${gridY}`} {...props}></Hex>
  4. ));

pointy topped grid coordinate x,y

pointy-topped

flat topped grid coordinate x,y

flat-topped

License

MIT